我的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
}
}
使用下面的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
}
对于那些使用Kotlin的人,尝试在应用程序build.gradle中将annotationProcessor更改为kapt
例如:
// 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"
还记得添加这个插件
apply plugin: 'kotlin-kapt'
到应用程序级别构建的顶部。Gradle文件,并做一个清洁和重建(根据https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#6)
在Android Studio中,如果在粘贴代码或构建过程中出现错误,请选择“构建>清洁项目”。然后选择Build > Rebuild Project,然后再次构建。
更新
如果您已经迁移到androidx
def room_version = "2.3.0" // check latest version from docs
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
更新2(自2021年7月起)
def room_version = "2.3.0" // check latest version from docs
implementation "androidx.room:room-ktx:$room_version"
kapt "androidx.room:room-compiler:$room_version"
我也有同样的问题。在一个由Android Studio创建的新示例项目中实现了几个类和接口:
https://developer.android.com/training/data-storage/room/
上面提到的所有解决方案都没有帮助,根据我的数据库类所需的_Impl文件不是由Room生成的。最后,在终端中执行gradle clean build给了我提示,导致解决方案:
警告:没有向注释处理器提供模式导出目录,因此我们不能导出模式。你可以提供空间。schemaLocation注释处理器参数或将exportSchema设置为false。
我在数据库类中添加了参数exportSchema = false
@Database(entities = arrayOf(User::class), version = 1, exportSchema = false)
abstract class AppDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
}
然后它工作了,在generatedJava下的app模块中找到了这两个生成的文件:
AppDatabase_Impl
UserDao_Impl
我不理解这种行为,因为参数是可选的,看
https://stackoverflow.com/a/44645943/3258117
对我来说,只要你包含任何与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,将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。如果你的实现有问题,这些插件会在编译时显示出来。
在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"