带有 Realm 数据库的 Dagger Hilt
Dager Hilt with Realm database
有没有人在 Realm 数据库中使用过 Dagger Hilt。我遇到初始化问题。也许上述解决方案会奏效。
错误 ->
Modules that need to be instantiated by Hilt must have a visible, empty constructor.
[Hilt] Processing did not complete. See error above for details.
Execution failed for task ':app:kaptDebugKotlin'.
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
java.lang.reflect.InvocationTargetException (no error message)`
我的模块 ->
@Module
@InstallIn(SingletonComponent::class)
class LocalDataSourceModule(context: Context) {
init {
Realm.init(context)
}
@Provides
fun provideLocalDataSource(realm: Realm): LocalDataSource = LocalDataSourceImpl(realm)
@Singleton
@Provides
fun provideRealm(configuration: RealmConfiguration): Realm =
try {
Realm.getDefaultInstance()
} catch (e: Exception) {
Realm.getInstance(configuration)
}
@Singleton
@Provides
fun provideRealmConfiguration(): RealmConfiguration = RealmConfiguration.Builder().build()
}
我的应用程序 ->
@HiltAndroidApp
class BaseApplication : Application() {
lateinit var girlComponent: GirlComponent
private set
override fun onCreate() {
super.onCreate()
Timber.plant(Timber.DebugTree())
girlComponent = DaggerGirlComponent.builder().localDataSourceModule(
LocalDataSourceModule(applicationContext)
).build()
}
}
我认为这可能有效。 Hilt 可以注入应用程序上下文。查看
@ApplicationContext
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import io.realm.Realm
import io.realm.RealmConfiguration
import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
@Module
object RealmModule {
@Provides
@Singleton
fun providesRealmDatabase(
@ApplicationContext context: Context
): Realm {
Realm.init(context)
val realmConfiguration = RealmConfiguration
.Builder()
.name("Tsavo Project")
.build()
Realm.setDefaultConfiguration(realmConfiguration)
return Realm.getDefaultInstance()
}
}
@Singleton
@Provides
fun provideRealm(@ApplicationContext context: Context): Realm =
try {
Realm.init(context)
Realm.getDefaultInstance()
} catch (e: Exception) {
Realm.getInstance(provideRealmConfig())
}
private fun provideRealmConfig(): RealmConfiguration = RealmConfiguration.Builder().build()
有没有人在 Realm 数据库中使用过 Dagger Hilt。我遇到初始化问题。也许上述解决方案会奏效。
错误 ->
Modules that need to be instantiated by Hilt must have a visible, empty constructor. [Hilt] Processing did not complete. See error above for details. Execution failed for task ':app:kaptDebugKotlin'. A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution java.lang.reflect.InvocationTargetException (no error message)`
我的模块 ->
@Module
@InstallIn(SingletonComponent::class)
class LocalDataSourceModule(context: Context) {
init {
Realm.init(context)
}
@Provides
fun provideLocalDataSource(realm: Realm): LocalDataSource = LocalDataSourceImpl(realm)
@Singleton
@Provides
fun provideRealm(configuration: RealmConfiguration): Realm =
try {
Realm.getDefaultInstance()
} catch (e: Exception) {
Realm.getInstance(configuration)
}
@Singleton
@Provides
fun provideRealmConfiguration(): RealmConfiguration = RealmConfiguration.Builder().build()
}
我的应用程序 ->
@HiltAndroidApp
class BaseApplication : Application() {
lateinit var girlComponent: GirlComponent
private set
override fun onCreate() {
super.onCreate()
Timber.plant(Timber.DebugTree())
girlComponent = DaggerGirlComponent.builder().localDataSourceModule(
LocalDataSourceModule(applicationContext)
).build()
}
}
我认为这可能有效。 Hilt 可以注入应用程序上下文。查看 @ApplicationContext
import android.content.Context
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import io.realm.Realm
import io.realm.RealmConfiguration
import javax.inject.Singleton
@InstallIn(SingletonComponent::class)
@Module
object RealmModule {
@Provides
@Singleton
fun providesRealmDatabase(
@ApplicationContext context: Context
): Realm {
Realm.init(context)
val realmConfiguration = RealmConfiguration
.Builder()
.name("Tsavo Project")
.build()
Realm.setDefaultConfiguration(realmConfiguration)
return Realm.getDefaultInstance()
}
}
@Singleton
@Provides
fun provideRealm(@ApplicationContext context: Context): Realm =
try {
Realm.init(context)
Realm.getDefaultInstance()
} catch (e: Exception) {
Realm.getInstance(provideRealmConfig())
}
private fun provideRealmConfig(): RealmConfiguration = RealmConfiguration.Builder().build()