skip to Main Content

I’m learning modularity and in one of my modules androidx.room.* is not recognised. Downgrading Room version helps only with @Database annotation. Also, module seems to not see Room.roomKtx implementation because quick help suggest adding this implementation to build.gradle. My kotlin version is 1.6.21

ProfileDatabase.kt

import androidx.room.Database
import androidx.room.RoomDatabase
import com.example.modules.Gift
import com.example.modules.Profile

@Database(
    entities = [Profile::class, Gift::class],
    version = 1
)
abstract class ProfileDatabase: RoomDatabase() {
    abstract val profileDao: ProfileDao

    companion object {
        const val DATABASE_NAME = "profile_db"
    }
}

build.gradle(:profileDatabase)

apply {
    from("$rootDir/library-build.gradle")
    plugin 'kotlin-kapt'
}

dependencies {
    implementation(project(':modules'))
    implementation Kotlinx.coroutinesCore

    implementation Room.runtime
    implementation Room.roomKtx
    kapt Room.compiler

}

Room.kt

object Room {
    private const val roomVersion = "2.4.2"
    const val runtime = "androidx.room:room-runtime:$roomVersion"
    const val compiler = "androidx.room:room-compiler:$roomVersion"
    const val roomKtx = "androidx.room:room-ktx:$roomVersion"
}

2

Answers


  1. Chosen as BEST ANSWER

    The problem was a wrong "module type". I used apply plugin:'java-library',
    but should use apply plugin:'com.android.library'.


  2. It looks correct to me. But are you adding the Room dependecy in the correct build.gradle-file? You are imporing the "modules" module in the same gradle file as you are importing Room, I suspect that you have put the DB implementation in the module that you are importing.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search