When I'm trying to build my app I got this compilation error:
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
com.example.persistence.AppDatabase, unresolved supertypes: androidx.room.RoomDatabase
Persistence setup is in separate Android module (persistence).
build.gradle
// Kotlin StdLib
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Room
implementation "androidx.room:room-runtime:$androidXRoom"
kapt "androidx.room:room-compiler:$androidXRoom"
implementation "androidx.room:room-rxjava2:$androidXRoom"
ext.androidXRoom = "2.1.0-alpha02"
I tried to change kotlin version, room version, back to Android Arch Room, but it's not working. I also tried cleaning project and invalidating cache of Android Studio. But it's not working.
edit: AppDatabase source
package com.example.persistence.db
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.persistence.post.PostDbDao
import com.example.persistence.post.PostDbEntity
@Database(entities = [PostDbEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun favoritePostsDao(): PostDbDao
companion object {
var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase? {
if(INSTANCE == null) {
synchronized(AppDatabase::class) {
INSTANCE = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "post_db").build()
}
}
return INSTANCE
}
fun destroy() {
INSTANCE = null
}
}
}
The problem more likely lays with how you're defining your dependencies, RoomDatabase
is part of the public API since your AppDatabase
extends it and you presumably use that class in your downstream dependencies. However RoomDatabase
is declared as a implementation-only dependency. This means that the class isn't normally available for the downstream dependencies during compilation.
Try changing "androidx.room:room-runtime:$androidXRoom"
to the api
configuration so it becomes part of the public API. That should probably resolve the error you're experiencing.
Change gradle dependencies like
REMOVE -> implementation "androidx.room:room-runtime:$androidXRoom"
REPLACE WITH -> api "androidx.room:room-runtime:$androidXRoom"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With