Android room persistent:AppDatabase_Impl不存在

我的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 lib:
编译“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 } 

}

Solutions Collecting From Web of "Android room persistent:AppDatabase_Impl不存在"

对于那些使用Kotlin的人 ,尝试在应用程序build.gradle annotationProcessor更改为build.gradle

例如:

 // Extensions = ViewModel + LiveData implementation "android.arch.lifecycle:extensions:1.1.0" kapt "android.arch.lifecycle:compiler:1.1.0" // Room implementation "android.arch.persistence.room:runtime:1.0.0" kapt "android.arch.persistence.room:compiler:1.0.0" 

我错过了这个错误

 @Database(entity="{}) 

确保上面注释中指定的实体模型引用特定模型类,并确保必要的注释:

 @Entity(tableName = "" ...) 

正确定义,你会很好

只是用

 apply plugin: 'kotlin-kapt' 

在app build.gradle中

并保持两者的依赖关系

 annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion" kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion" 

它不仅仅是更新您的依赖项。 确保所有Room依赖项具有相同的版本。

 implementation 'android.arch.persistence.room:rxjava2:1.1.0-alpha2' implementation 'android.arch.persistence.room:runtime:1.1.0-alpha2' annotationProcessor "android.arch.persistence.room:compiler:1.1.0-alpha2" 

在上面的示例代码段中,我的所有Room依赖项都具有相同的版本1.1.0-alpha2

使用以下gradle链接:

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

你需要创建diffretn singloton类并从这里得到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.lifecycle:extensions:$archLifecycleVersion" implementation "android.arch.persistence.room:runtime:$archRoomVersion" annotationProcessor "android.arch.lifecycle:compiler:$archLifecycleVersion" annotationProcessor "android.arch.persistence.room:compiler:$archRoomVersion" 

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