0

我有以下问题:我将 Koin 用于 DI,将 Room 用于持久性。现在我的房间依赖项如下:

// Architecture Component - Room
implementation "android.arch.persistence.room:runtime:1.1.1"
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"

但是我得到了这里描述的错误: Android room persistent: AppDatabase_Impl does not exist

所以我把annotationProcessor改成了kapt。但是现在我从数据绑定类中自动生成了多个错误,而 annotationProcessor 不存在这些错误。例如:

app/build/generated/data_binding_base_class_source_out/debug/dataBindingGenBaseClassesDebug/out/databinding/FragmentBinding.java:26: error: cannot find symbol
  protected FragmentBinding(DataBindingComponent _bindingComponent, View _root,
                                     ^
  symbol:   class DataBindingComponent
  location: class FragmentBinding

Koin 模块定义:

val persistenceModule = module {
    single {
        Room.databaseBuilder(androidApplication(), Database::class.java, "database.db")
            .build()
    }
    single { get<Database>().dao() }
}

这怎么可能发生?

4

3 回答 3

1

尝试single<YourDB>

val dbModule = module {
    single<FairytaleDB> { Room.databaseBuilder(get(), FairytaleDB::class.java, "Fairytale.db").createFromAsset("ertak.db").build() }
    single { get<FairytaleDB>().getDao() }
}
于 2020-08-31T12:03:56.080 回答
0

这就是我们如何通过 Koin 提供 Room 的依赖,下面是代码

  fun providesDatabase(application: Application):UserDatabase =
        Room.databaseBuilder(application,UserDatabase::class.java,"userdatabase")
            .build()

fun providesDao(db:UserDatabase):UserDao = db.userDao()


val roomModule = module {

    single { providesDatabase(get()) }
    single { providesDao(get()) }
}
于 2021-06-12T12:50:55.507 回答
0

确保您在应用级别文件中添加了kapt 插件:build.gradle

apply plugin: 'kotlin-kapt'
于 2019-07-15T11:49:01.877 回答