Android Studio: "attempting to assign weaker access privileges" error on Room Database implementation

Viewed 2310

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.

Can anyone tell me what is going on here?

Note: please if you are going to write code write where I put the code wisely, thanks.

4 Answers

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'

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"

Thanks Никандрова Елизавета for quick respond and effort to help.

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

Good day!

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"

Hopefully this post is helpful for someone. Have a good one!

Related