15

I am trying to implement room database, I have gone through steps on Official Website, and 'AppDatabase.java' file is like this:

import android.content.Context;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {

    public static AppDatabase instance;
    public static synchronized AppDatabase getInstance(Context context){
        if (instance==null){
            instance = Room.databaseBuilder(context.getApplicationContext(),
                    AppDatabase.class, "app_database").fallbackToDestructiveMigration().build();
        }
        return instance;
    }
}

And dependencies I have used for room:

    // Room Database
    def room_version = "2.4.2"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // optional - RxJava2 support for Room
    implementation "androidx.room:room-rxjava2:$room_version"

    // optional - RxJava3 support for Room
    implementation "androidx.room:room-rxjava3:$room_version"

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"

    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    // optional - Paging 3 Integration
    implementation "androidx.room:room-paging:2.5.0-alpha02"

    // Room Database

It returns 2 errors here:

onCreate(SupportSQLiteDatabase) in <anonymous com.example.testdb1.room.AppDatabase_Impl$1> cannot override onCreate(SupportSQLiteDatabase) in Delegate
attempting to assign weaker access privileges; was public
onValidateSchema(SupportSQLiteDatabase) in <anonymous com.example.testdb1.room.AppDatabase_Impl$1> cannot override onValidateSchema(SupportSQLiteDatabase) in Delegate
attempting to assign weaker access privileges; was public

It was working before the 'Chipmunk' version (was working in 'Bumblebee'), but it started throwing these errors.

What is going on here?

E_net4
  • 27,810
  • 13
  • 101
  • 139
Memduh Yılmaz
  • 325
  • 2
  • 8
  • 1
    i'm currently starting out with android programming and got the same problem. after removing the unnecessary optional dependencies and only keeping the core (runtime & compiler), everything worked fine for me. might worth a try for you – Akoya Jun 05 '22 at 21:52
  • They must have the same version – Unes Sep 15 '22 at 16:23

10 Answers10

10

To fix this error for Jetpack Compose and Paging 3 you only need to use only this libraries

//ROOM
implementation "androidx.room:room-runtime:2.4.2"
kapt "androidx.room:room-compiler:2.4.2"
implementation "androidx.room:room-ktx:2.4.2"
implementation "androidx.room:room-paging:2.4.2"

// Paging 3.0
implementation 'androidx.paging:paging-compose:1.0.0-alpha15'
  • 1
    Sorry, but none of this was asked for - just read the question once again. – Martin Zeitler Jun 06 '22 at 13:40
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 06 '22 at 14:16
  • it's just my example how I fixed this problem. If you have the same one you must delete or add one or more libraries for Room. I don't know what libraries you are using. – Elizaveta Zalyaeva Jun 06 '22 at 15:33
  • Hi Никандрова Елизавета, Thank you for your advise, I was using optional libraries on website, they were causing the problem I didn't think that dependencies may cause the problem, I would like you to edit your solution and write to not use optional dependencies so it can be more helpful to other people. Thanks.. – Memduh Yılmaz Jun 07 '22 at 12:23
  • 2
    Fixed it for me too. Just remove the optional dependencies. – HelloWorld Jun 24 '22 at 16:20
  • I reverted all the code in both my build.gradle project and build.gradle app. It worked and matches with your answer. Please write some description as well in your answer. – Abhinav Saxena Oct 12 '22 at 07:18
  • This error has surfaced again. It was working fine till yesterday, now again it is there, out of the thin air. This time it is not going away. – Abhinav Saxena Oct 18 '22 at 05:12
9

I got this error because I used

implementation 'androidx.work:work-runtime-ktx:2.8.0-rc01'

with

implementation 'androidx.room:room-runtime:2.4.3'
kapt 'androidx.room:room-compiler:2.4.3'

and library

androidx.work:work-runtime-ktx:2.8.0-rc01

is dependent on

androidx.room:room-common:2.5.0-rc01

so I finished with two same libraries but with different versions

androidx.room:room-common:2.5.0-rc01
androidx.room:room-common:2.4.3

after switching back to

androidx.work:work-runtime:2.7.1

error was gone.

Zoran
  • 1,502
  • 19
  • 22
  • Same for me. I use work-runtime and it works with androidx.work:work-runtime:2.8.0-alpha04 but not with androidx.work:work-runtime:2.8.0-beta01. – Branislav Kuliha Dec 15 '22 at 21:07
7

By Никандрова Елизавета's answer, I have found that the source of the problem was one of the optional implementations that I have added from official website.

These dependencies was enough to run my code:

    def room_version = "2.4.2"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
E_net4
  • 27,810
  • 13
  • 101
  • 139
Memduh Yılmaz
  • 325
  • 2
  • 8
5

Well, I have just encountered this problem too, seems that you are copying the code from the official guidance. It seems to be the problem of "androidx.room:room-paging:2.5.0-alpha02" so the solution is to replace it with the latest stable version(2.4.2 currently) or just use the variable room_version And you can check the latest version on: https://androidx.tech/artifacts/room/room-paging/ So you just need to replace it with the following code to solve this problem

dependencies {
    def room_version = "2.4.2"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // optional - RxJava2 support for Room
    implementation "androidx.room:room-rxjava2:$room_version"

    // optional - RxJava3 support for Room
    implementation "androidx.room:room-rxjava3:$room_version"

    // optional - Guava support for Room, including Optional and ListenableFuture
    implementation "androidx.room:room-guava:$room_version"

    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

    // optional - Paging 3 Integration
    implementation "androidx.room:room-paging:$room_version"
}
hash070
  • 641
  • 1
  • 3
  • 8
2

The problem occured after I changed the androidx.work:work-runtime implementation from version 2.7.1 to version 2.8.0 while having the following dependencies

implementation "androidx.room:room-runtime:2.4.3"

annotationProcessor 'androidx.room:room-compiler:2.4.3'

I upgraded the room version to 2.5.0 and the problem is resolved

Ahmed Rajab
  • 603
  • 3
  • 10
  • 28
1

I tried to comment every dependency one by one; changed version one by one at a time.

    def roomVersion = "2.4.1"
//    //def roomVersion = "2.4.3"
//    //noinspection GradleDependency
//    kapt "androidx.room:room-compiler:$roomVersion"
//    //noinspection GradleDependency
    implementation "androidx.room:room-runtime:$roomVersion"
//    //noinspection GradleDependency
    implementation "androidx.room:room-ktx:$roomVersion"

It occurred to me that:

//    kapt "androidx.room:room-compiler:$roomVersion"

above comment was responsible to generate the following exception:

Caused by: java.lang.RuntimeException: Cannot find implementation for com.safehaven.data.di.AppDatabase. AppDatabase_Impl does not exist

Hence this library that is commented is responsible in the generation of this file AppDatabase_Impl where this error is coming.

So now this library is to be dealt with.

Secondly the Delegate class that is used in RoomOpenHelper

has these codes:

        @Suppress("DEPRECATION")
        open fun onValidateSchema(db: SupportSQLiteDatabase): ValidationResult {
            validateMigration(db)
            return ValidationResult(true, null)
        }

and

override fun onCreate(db: SupportSQLiteDatabase) {
    val isEmptyDatabase = hasEmptySchema(db)
    delegate.createAllTables(db)
    if (!isEmptyDatabase) {
        // A 0 version pre-populated database goes through the create path because the
        // framework's SQLiteOpenHelper thinks the database was just created from scratch. If we
        // find the database not to be empty, then it is a pre-populated, we must validate it to
        // see if its suitable for usage.
        val result = delegate.onValidateSchema(db)
        if (!result.isValid) {
            throw IllegalStateException(
                "Pre-packaged database has an invalid schema: ${result.expectedFoundMsg}"
            )
        }
    }
    updateIdentity(db)
    delegate.onCreate(db)
}

clearly both are public, while in SupportSQLiteOpenHelper.Callback _openCallback class that instantiates with this abstract class in AppDatabase_Impl class, exposes these methods with protected access modifier which lowers the visibility of the overridden methods.

Hence causing error.

This should be fixed. Surprisingly I got this error a week ago. It was resolved by using one of the answer (here, in the same question) as method (I am clueless, how it did), it has surfaced again and now not going away.

For this very reason I recommend at least myself, not to use Room Database. I will move to ORMLite or SQLite database as before.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Abhinav Saxena
  • 1,990
  • 2
  • 24
  • 55
  • Thanks `https://stackoverflow.com/users/1233251/e-net4-the-comment-flagger`, This edit worked and I started getting noticed. – Abhinav Saxena Oct 22 '22 at 09:24
1

I had same error after upgrading to compileSdk 33 using

implementation 'androidx.room:room-runtime:2.4.3'
kapt 'androidx.room:room-compiler:2.4.3'

I found that after the upgrade there is an implicit dependency to 2.5.0-beta01:

Dependencies in IDEA

so I just upgraded to 2.5.0-beta01

implementation 'androidx.room:room-runtime:2.5.0-beta01'
kapt 'androidx.room:room-compiler:2.5.0-beta01'

and the error was gone

oleh
  • 820
  • 2
  • 13
  • 28
1

Increasing room version fixed it for me

implementation "androidx.room:room-runtime:2.5.0-rc01"
Codelicious
  • 604
  • 10
  • 12
0

I had same errors and I could solve issues doing as below in build.gradle(app).

In 'plugins' block, below code was added;

id 'kotlin-kapt'

In 'dependencies' block, below codes were added;

def room_version = "2.4.2"
implementation "androidx.room:room-ktx:$room_version"
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
E_net4
  • 27,810
  • 13
  • 101
  • 139
D.Y Won
  • 245
  • 3
  • 5
0

I removed the code -

 def room_version = "2.4.3"
        implementation "androidx.room:room-ktx:$room_version"
        kapt "androidx.room:room-compiler:$room_version"

and replace with this one -

 def room_version = "2.4.3"
    implementation "androidx.room:room-runtime:$room_version"
    implementation "androidx.room:room-ktx:$room_version"
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
  • Sure, the build error is gone, but so is the Database_Impl: `java.lang.RuntimeException: Cannot find implementation for xxxxx.AppDatabase. AppDatabase_Impl does not exist` – oleh Dec 08 '22 at 11:20