android / architecture-components-samples

Samples for Android Architecture Components.
https://d.android.com/arch
Apache License 2.0
23.45k stars 8.28k forks source link

Room injection using KOIN not working #392

Closed Smedzlatko closed 6 years ago

Smedzlatko commented 6 years ago

Question about Architecture Components?

I am trying to use the Room persistence library in my application but I have trouble with injection using KOIN. Does someone have an experience with such integration? I followed this TUTORIAL but I ended up with the following exception:

org.koin.error.DependencyResolutionException: Cyclic call while resolving Bean[class=com.freyja.client.database.RecordDatabase]. Definition is already in resolution in current call

I am the following definitions:

@Database(entities = [Record::class], version = 1)
abstract class RecordDatabase: RoomDatabase() {

    abstract fun recordDao(): RecordDao

}

@Dao
interface RecordDao {

    @Query("SELECT * from record WHERE recordId= :recordId LIMIT 1")
    fun get(recordId: Int): Record

}

and my Koin modules definition contains:

bean { Room.databaseBuilder(get(), RecordDatabase::class.java, "record-db")
                .build() }
bean { get<RecordDatabase>().recordDao() }

dependencies:

def koinVersion = "0.9.3"
implementation "org.koin:koin-android:$koinVersion"
implementation "org.koin:koin-android-architecture:$koinVersion"
androidTestImplementation "org.koin:koin-test:$koinVersion"

def roomVersion = "1.1.0"
implementation "android.arch.persistence.room:runtime:$roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$roomVersion"
implementation "android.arch.persistence.room:rxjava2:$roomVersion"
testImplementation "android.arch.persistence.room:testing:$roomVersion"
Smedzlatko commented 6 years ago

I resolved my problem with using kapt "android.arch.persistence.room:compiler:$roomVersion" instead of annotationProcessor. It seems that it was a Koltin issue after all.