我有以下问题: 我使用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
因此我将注释处理器更改为kapt。但是现在我从数据绑定类中自动生成多个错误,而注释处理程序没有这些错误。 例如:
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() }
}
这怎么发生?
答案 0 :(得分:0)
确保在应用级build.gradle
文件中添加了 kapt插件:
apply plugin: 'kotlin-kapt'
答案 1 :(得分:0)
尝试single<YourDB>
:
val dbModule = module {
single<FairytaleDB> { Room.databaseBuilder(get(), FairytaleDB::class.java, "Fairytale.db").createFromAsset("ertak.db").build() }
single { get<FairytaleDB>().getDao() }
}
答案 2 :(得分: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()) }
}