1

I have the following issue: I am using Koin for DI and Room for persistence. Now my room dependencies are the following:

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

But I get the error described here: Android room persistent: AppDatabase_Impl does not exist

So I changed the annotationProcessor to kapt. But now I get multiple errors in auto-generated from data binding classes which were not there with the annotationProcessor. For example:

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 module defintion:

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

How can this happen?

FabASP
  • 581
  • 1
  • 6
  • 20

3 Answers3

1

try single<YourDB>:

val dbModule = module {
    single<FairytaleDB> { Room.databaseBuilder(get(), FairytaleDB::class.java, "Fairytale.db").createFromAsset("ertak.db").build() }
    single { get<FairytaleDB>().getDao() }
}
Mahmudxon
  • 25
  • 3
0

Make sure you have added kapt plugin in your app level build.gradle file:

apply plugin: 'kotlin-kapt'
Birju Vachhani
  • 6,072
  • 4
  • 21
  • 43
0

This is how we can provide the dependency of Room through Koin, Below is the code

  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()) }
}
Jayant Kumar
  • 775
  • 5
  • 12