我的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

    }   

}

当前回答

我也遇到了同样的现象。

implementation "android.arch.persistence.room:runtime:1.1.1"

添加会导致另一个构建错误,但会从日志中跟踪原因。

在我的例子中,SQL实现中有一个错误。 修复之后,构建是成功的。

因此,您可能希望检查整个房间库的实现,而不是查看崩溃的局部函数。

其他回答

对我来说,只要你包含任何与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,你应该可以开始了。

使用下面的gradle链接:

compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha9'

你需要创建一个不同的单例类,并从那里获取AppDatabase,如下所示:

RoomDB.java

public class RoomDB {

private static RoomDB INSTANCE;

public static AppDatabase getInstance(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;
}

App.java

public class App extends Application {

private boolean appRunning = false;

@Override
public void onCreate() {
    super.onCreate();
    RoomDB.getInstance(this); //This will provide AppDatabase Instance
}

我也遇到了同样的现象。

implementation "android.arch.persistence.room:runtime:1.1.1"

添加会导致另一个构建错误,但会从日志中跟踪原因。

在我的例子中,SQL实现中有一个错误。 修复之后,构建是成功的。

因此,您可能希望检查整个房间库的实现,而不是查看崩溃的局部函数。

我遇到了这个问题,因为我忘记了适当的依赖关系

implementation "android.arch.lifecycle:extensions:$archLifecycleVersion"
implementation "android.arch.persistence.room:runtime:$archRoomVersion"
annotationProcessor "android.arch.lifecycle:compiler:$archLifecycleVersion"
annotationProcessor "android.arch.persistence.room:compiler:$archRoomVersion"

在添加了annotationProcessor并重新构建之后,问题就解决了。

同意以上回答

解决方法如下。如下所示,将annotationProcessor更改为kapt

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