Reputation: 615
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
}
}
}
Upvotes: 22
Views: 10519
Reputation: 11060
In addition to the answers. When I chose Module-> Android Library while creating a new module, the "api" process was successfully completed and I provided a solution.
Upvotes: 0
Reputation: 3593
The other answers would compile, but defeats the point of having a separate module for your database/persistence functions.
You should not be exposing your Room Entity or Room database outside of your persistence
module. Instead, you should write a function in your persistence
module that maps your PostEntity
@Entity into a simple Post
data class.
I wrote about it in more detail here: https://jacquessmuts.github.io/post/modularization_room/
Upvotes: 3
Reputation: 1518
Change gradle dependencies like
REMOVE -> implementation "androidx.room:room-runtime:$androidXRoom"
REPLACE WITH -> api "androidx.room:room-runtime:$androidXRoom"
Upvotes: 30
Reputation: 25573
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.
Upvotes: 54