Android Studio:房间数据库实现中出现“试图分配较弱的访问权限”错误

6ie5vjzr  于 2022-10-22  发布在  Android
关注(0)|答案(6)|浏览(326)

我正在尝试实现房间数据库,我已经完成了Official Website和AppDatabase的步骤。java文件如下:

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

以及我用于房间的依赖项:

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

它在此处返回2个错误:

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

它在“Chipmunk”版本之前工作(在“Bumblebee”中工作),但它开始抛出这些错误。
这是怎么回事?

rkttyhzu

rkttyhzu1#

要修复Jetpack Compose和Paging 3的此错误,您只需要使用此库

//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'
qhhrdooz

qhhrdooz2#

通过Никандрова Елизавета's answer,我发现问题的根源是我从官方网站添加的可选实现之一。
这些依赖关系足以运行我的代码:

def room_version = "2.4.2"

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

bfrts1fy3#

嗯,我刚刚也遇到了这个问题,看来你是在抄袭官方指南中的代码。这似乎是“androidx.room:room paging:2.5.0-alpha02”的问题,因此解决方案是将其替换为最新的稳定版本(当前为2.4.2),或者只使用变量room_version。您可以在https://androidx.tech/artifacts/room/room-paging/上检查最新版本。因此,您只需将其替换成以下代码即可解决此问题

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

5m1hhzi44#

我试着逐一评论每一个依赖;每次逐个更改版本。

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"

我突然想到:

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

上述注解负责生成以下异常:
导致原因:java.lang.RuntimeException:找不到com.safehaven.data.di.AppDatabase的实现。AppDatabase_Impl不存在
因此,这个被注解的库负责生成此错误出现的文件AppDatabase_Impl
所以现在这个图书馆要处理了。
其次是RoomOpenHelper中使用的Delegate
具有以下代码:

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

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

显然两者都是公共的,而在用AppDatabase_Impl类中的抽象类示例化的SupportSQLiteOpenHelper.Callback _openCallback中,使用受保护的访问修饰符公开这些方法,这会降低被重写方法的可见性。
因此导致错误。
这应该是固定的。令人惊讶的是,我一周前就犯了这个错误。它是通过使用一个答案(在这里,在同一个问题中)作为方法(我不知道,它是如何做到的)来解决的,它再次浮出水面,现在不会消失。
出于这个原因,我至少建议自己不要使用房间数据库。我将像以前一样移动到ORMLite或SQLite数据库。

ftf50wuq

ftf50wuq5#

我有同样的错误,我可以在build.gradle(app)中解决以下问题。
在“插件”块中,添加了以下代码;

id 'kotlin-kapt'

在“依赖关系”块中,添加了以下代码;

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

u5i3ibmn6#

我删除了密码-

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

换成这个-

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

相关问题