我正在尝试实现房间数据库,我已经完成了官方网站上的步骤,“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 的依赖项:
// 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
它在“花栗鼠”版本之前工作(在“大黄蜂”中工作),但它开始抛出这些错误。
这是怎么回事?
我收到此错误是因为我使用了
implementation 'androidx.work:work-runtime-ktx:2.8.0-rc01'
与
implementation 'androidx.room:room-runtime:2.4.3'
kapt 'androidx.room:room-compiler:2.4.3'
和图书馆
androidx.work:work-runtime-ktx:2.8.0-rc01
取决于
androidx.room:room-common:2.5.0-rc01
所以我完成了两个相同的库但版本不同
androidx.room:room-common:2.5.0-rc01
androidx.room:room-common:2.4.3
切换回后
androidx.work:work-runtime:2.7.1
错误消失了。
要修复 Jetpack Compose 和 Paging 3 的此错误,您只需使用这些库。导致错误的原因是 ktx 和运行时依赖项如果版本不匹配就会发生冲突。
//ROOM
implementation "androidx.room:room-runtime:2.6.1"
kapt "androidx.room:room-compiler:2.4.2"
implementation "androidx.room:room-ktx:2.6.1"
implementation "androidx.room:room-paging:2.6.1"
// Paging 3.0
implementation 'androidx.paging:paging-compose:1.0.0-alpha15'
通过Никандрова Елизавета的回答,我发现问题的根源是我从官方网站添加的可选实现之一。
这些依赖项足以运行我的代码:
def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
嗯,我刚刚也遇到了这个问题,看来你是从官方指导中复制代码。 这似乎是“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"
}
问题发生在我将
androidx.work:work-runtime
实现从版本 2.7.1
更改为版本 2.8.0
并具有以下依赖项
implementation "androidx.room:room-runtime:2.4.3"
annotationProcessor 'androidx.room:room-compiler:2.4.3'
我升级了房间版本到
2.5.0
问题解决了
我尝试一一注释每一个依赖项;一次一个地更改版本。
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
,这是出现此错误的地方。
所以现在要处理这个库。
其次是
Delegate
中使用的
RoomOpenHelper
类
有这些代码:
@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)
}
显然两者都是公共的,而在
SupportSQLiteOpenHelper.Callback _openCallback
类中使用此抽象类实例化的 AppDatabase_Impl
类中,使用 protected 访问修饰符公开这些方法,从而降低了重写方法的可见性。
因此导致错误。
这个问题应该得到解决。 令人惊讶的是我一周前收到了这个错误。它是通过使用答案之一(在这里,在同一问题中)作为方法解决的(我一无所知,它是如何做到的),它再次浮出水面,现在不会消失。
正是出于这个原因,我至少建议我自己不要使用房间数据库。我将像以前一样迁移到 ORMLite 或 SQLite 数据库。
增加房间版本为我解决了这个问题
implementation "androidx.room:room-runtime:2.5.0-rc01"
我遇到了同样的错误,我可以在 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"
我删除了代码-
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"