我的app数据库类

@Database(entities = {Detail.class}, version = Constant.DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {

    private static AppDatabase INSTANCE;

    public abstract FavoritesDao favoritesDao();

    public static AppDatabase getAppDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE =
                    Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, Constant.DATABASE).allowMainThreadQueries().build();

                    //Room.inMemoryDatabaseBuilder(context.getApplicationContext(),AppDatabase.class).allowMainThreadQueries().build();
        }
        return INSTANCE;
    }

    public static void destroyInstance() {
        INSTANCE = null;
    }
}

Gradle自由:

 compile "android.arch.persistence.room:runtime:+"   
 annotationProcessor "android.arch.persistence.room:compiler:+"

当我请求实例时,它会给出这个错误,AppDatabase_Impl不存在 在我的应用课上

public class APp extends Application {

    private boolean appRunning = false;

    @Override
    public void onCreate() {
        super.onCreate();
        AppDatabase.getAppDatabase(this); //--AppDatabase_Impl does not exist

    }   

}

当前回答

针对Kotlin开发人员


如果你检查了Dao和Entity,也使用了Kapt,没有问题,我猜如果你使用kotlin 1.4及以上版本,你的kotlin版本有问题。 从这个链接更新房间到最新版本。


2.3.0-alpha03解决了我的问题。

其他回答

确保在build.gradle中为room添加正确的依赖项

ext {
   roomVersion = '2.1.0-alpha06'
}

// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation "androidx.room:room-ktx:$rootProject.roomVersion"
kapt "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

在上面这条线下面

apply plugin: 'kotlin-kapt'

在2023年1月,我在重构我的代码以使用ServiceLocator类后遇到了类似的问题。

为了解决这个问题,我在更衣室里玩了很多次。它适用于2.5.0-alpha02

Version_room = "2.5.0-alpha02" <——build。gradle(项目)

 //Room
implementation "androidx.room:room-runtime:$version_room"
kapt "androidx.room:room-compiler:$version_room"
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$version_room"
implementation("androidx.room:room-guava:$version_room")

我在Android Eel 2020.1.1上运行以下程序:

Version_kotlin = "1.7.21" Version_android_gradle_plugin = "4.0.1"

针对Kotlin开发人员

用这个:

implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"

并添加apply plugin: 'kotlin-kapt'到应用级别build.gradle的顶部。

Java开发人员须知

implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"

对我来说,只要你包含任何与Room数据库相关的导入,Android Studio就会自动更新依赖项。但根据https://developer.android.com/jetpack/androidx/releases/room#declaring_dependencies,你需要更新一些。下面是我的代码库的样子:

AppDatabase.kt

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

@Database(entities = arrayOf(MyEntity::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun myDAO(): MyDAO

    companion object {
        @Volatile private var instance: AppDatabase? = null
        private val LOCK = Any()

        operator fun invoke(context: Context)= instance ?: synchronized(LOCK){
            instance ?: buildDatabase(context).also { instance = it}
        }

        private fun buildDatabase(context: Context) = Room.databaseBuilder(context,
            AppDatabase::class.java, "db-name.db")
            .build()
    }
}

更新构建。Gradle在其中一个答案中指定:

apply plugin: 'kotlin-kapt' // this goes with other declared plugin at top
dependencies { // add/update the following in dependencies section
    implementation 'androidx.room:room-runtime:2.2.3'
//    annotationProcessor 'androidx.room:room-compiler:2.2.3' // remove this and use the following
    kapt "androidx.room:room-compiler:2.2.3"

}

同步gradle,你应该可以开始了。

在我的kotlin应用程序中,我只是在我的构建顶部添加了以下一行。Gradle文件:

apply plugin: 'kotlin-kapt'

以及依赖项部分的下面一行:

kapt "androidx.room:room-compiler:2.2.5"

我希望它能解决你的问题。