Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AndroidX Room unresolved supertypes RoomDatabase

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
        }
    }

}
like image 948
jarekbutek Avatar asked Nov 05 '18 10:11

jarekbutek


2 Answers

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.

like image 97
Kiskae Avatar answered Sep 19 '22 11:09

Kiskae


Change gradle dependencies like

   REMOVE -> implementation "androidx.room:room-runtime:$androidXRoom"
   REPLACE WITH -> api "androidx.room:room-runtime:$androidXRoom"

enter image description here

like image 40
shobhan Avatar answered Sep 18 '22 11:09

shobhan