Java Android 房间持久化:AppDatabase_Impl 不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46665621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android room persistent: AppDatabase_Impl does not exist
提问by pratik deshai
My app database class
我的应用程序数据库类
@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:
摇篮库:
compile "android.arch.persistence.room:runtime:+"
annotationProcessor "android.arch.persistence.room:compiler:+"
And when i ask for instance it will give this error, AppDatabase_Impl does not exist
in my application class
例如,当我询问时,它会给出此错误,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
}
}
采纳答案by RWIL
For those working with Kotlin, try changing annotationProcessor
to kapt
in the apps build.gradle
对于那些使用Kotlin 的人,请尝试在应用程序中更改annotationProcessor
为kapt
build.gradle
for example:
例如:
// 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"
also remember to add this plugin
还记得添加这个插件
apply plugin: 'kotlin-kapt'
to the top of the app level build.gradle file and do a clean and rebuild (according to https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#6)
到应用程序级 build.gradle 文件的顶部并进行清理和重建(根据https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#6)
In Android Studio, if you get errors when you paste code or during the build process, select Build >Clean Project. Then select Build > Rebuild Project, and then build again.
在 Android Studio 中,如果在粘贴代码或构建过程中出现错误,请选择 Build > Clean Project。然后选择 Build > Rebuild Project,然后再次构建。
UPDATE
更新
If you have migrated to androidx
如果您已迁移到 androidx
def room_version = "2.2.3" // check latest version from docs
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
回答by Burhanuddin Rashid
Use the following gradle link:
使用以下 gradle 链接:
compile 'android.arch.persistence.room:runtime:1.0.0-alpha9'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0-alpha9'
You need to create diffretn singloton class and get the Appdatabase from ther like this:
您需要创建 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
应用程序.java
public class App extends Application {
private boolean appRunning = false;
@Override
public void onCreate() {
super.onCreate();
RoomDB.getInstance(this); //This will provide AppDatabase Instance
}
回答by ahsiu
I met this problem because I have forgotten the apt dependences
我遇到了这个问题,因为我忘记了 apt 依赖
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"
after added the annotationProcessor, and rebuild it, the problem solved.
添加annotationProcessor后,重新构建,问题解决。
回答by Idee
It is not just about updating your dependencies. Make sure all your Room dependencies have the same version.
这不仅仅是更新您的依赖项。确保所有 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"
In the sample snippet above, all my Room dependencies have the same version 1.1.0-alpha2
在上面的示例片段中,我所有的 Room 依赖项都具有相同的版本 1.1.0-alpha2
回答by Thadeus Ajayi
I had this error when I missed
我错过时遇到了这个错误
@Database(entity="{<model.class>})
Ensure that the entity model specified in the annotation above refers to the particular model class and also ensure that the necessary annotation:
确保上面注解中指定的实体模型是指特定的模型类,并确保必要的注解:
@Entity(tableName = "<table_name>" ...)
is properly defined and you'd be good
被正确定义,你会很好
回答by Jahangir Kabir
Just use
只需使用
apply plugin: 'kotlin-kapt'
in app build.gradle
在应用程序 build.gradle 中
And keep both in dependencies
并将两者保持在依赖关系中
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
kapt "android.arch.persistence.room:compiler:$rootProject.roomVersion"
EDIT
编辑
In newer version don't need to add both dependencies at a time Just use, hope it will work.
在较新的版本中不需要一次添加两个依赖项就可以使用,希望它会起作用。
kapt 'android.arch.persistence.room:compiler:1.1.1'
回答by Farruh Habibullaev
In my case, I was testing the connectivity for room database and I have put the testing class inside the directory which I have created inside the AndroidTest folder. I have moved it out of the custom directory, then it worked pretty well.
就我而言,我正在测试房间数据库的连接性,并将测试类放在我在 AndroidTest 文件夹中创建的目录中。我已将它移出自定义目录,然后它运行得很好。
回答by Pedro Massango
For Kotlin Developers
对于 Kotlin 开发人员
Use this:
用这个:
implementation "android.arch.persistence.room:runtime:1.0.0"
kapt "android.arch.persistence.room:compiler:1.0.0"
And add apply plugin: 'kotlin-kapt'
to the top of the app level build.gradle
.
并添加apply plugin: 'kotlin-kapt'
到应用程序级别的顶部build.gradle
。
For Java Developers
对于 Java 开发人员
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
回答by shellhub
I meet with the problem, because I forget @Dao
annotation
我遇到了这个问题,因为我忘记了@Dao
注释
@Dao
public interface SearchHistoryDao {
@Query("SELECT * FROM search_history")
List<SearchHistory> getAll();
@Insert
void insertAll(SearchHistory... histories);
@Delete()
void delete(SearchHistory history);
}
回答by Hajo
Had the same problem. Implemented the few classes and interface as officially told in a new example project created by Android Studio: https://developer.android.com/training/data-storage/room/
有同样的问题。实现了 Android Studio 创建的新示例项目中官方介绍的少数类和接口:https: //developer.android.com/training/data-storage/room/
All mentioned solutions above did not help, the necessary _Impl files according to my database class were not generated by Room. Finally executing gradle clean build in terminal gave me the hint that lead to the solution:
上面提到的所有解决方案都没有帮助,Room 没有根据我的数据库类生成必要的 _Impl 文件。最后在终端中执行 gradle clean build 给了我导致解决方案的提示:
"warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide room.schemaLocation
annotation processor argument OR set exportSchema to false."
“警告:架构导出目录未提供给注释处理器,因此我们无法导出架构。您可以提供room.schemaLocation
注释处理器参数或将 exportSchema 设置为 false。”
I added the parameter exportSchema = false in the database class
我在数据库类中添加了参数exportSchema = false
@Database(entities = arrayOf(User::class), version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
And then it worked, found these two generated files in the app module under generatedJava:
然后就成功了,在generateJava下的app模块中找到了这两个生成的文件:
- AppDatabase_Impl
- UserDao_Impl
- AppDatabase_Impl
- UserDao_Impl
I don't understand this behaviour as the parameter is said to be optional, see https://stackoverflow.com/a/44645943/3258117
我不理解这种行为,因为该参数据说是可选的,请参阅 https://stackoverflow.com/a/44645943/3258117