我的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,将kotlin注释处理器插件添加到应用级别build.gradle

plugins {
    id "org.jetbrains.kotlin.kapt"
}

还要删除annotationProcessor并将其替换为kapt

而不是

dependencies {
    def room_version = "2.3.0"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
}

使用

dependencies {
    def room_version = "2.3.0"
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
}

注释处理器只能在java环境中工作。kapt同时负责java和kotlin。如果你的实现有问题,这些插件会在编译时显示出来。

其他回答

使用下面的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
}

在OP的情况下不是这样,但是当你错误地使用implementation而不是annotationProcessor时也会发生这种情况,就像这样:

implementation "android.arch.persistence.room:compiler:x.x.x"

而不是这样:

annotationProcessor "android.arch.persistence.room:compiler:x.x.x"

为了防止其他人犯和我一样的错误,不要将数据库类称为“database”,否则您将得到相同的错误。

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

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并重新构建之后,问题就解决了。

如果您正在使用kotlin类来实现数据库,那么 使用

apply plugin: 'kotlin-kapt'

and

kapt "android.arch.persistence.room:compiler:1.1.1"

在您的gradle文件中,它将工作。