java 调用 Rooms inMemoryBuilder 方法时的 Room Persistence Library 运行时异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44417136/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:09:51  来源:igfitidea点击:

Room Persistence Library run time exception when calling Rooms inMemoryBuilder method

javaandroidsqliteormandroid-room

提问by dxpelou

When following the tutorial for setting up the Room persistence library I run in to this error when testing on an Android device.

按照设置 Room 持久性库的教程进行操作时,我在 Android 设备上进行测试时遇到了此错误。

java.lang.RuntimeException: cannot find implementation for PackageName.AppDatabase. AppDatabase_Impl does not exist

java.lang.RuntimeException:找不到PackageName.AppDatabase 的实现。AppDatabase_Impl 不存在

I know a similar question has been asked however the issues were due to kotlin gradle issues. Possible Duplicate

我知道有人问过类似的问题,但是这些问题是由于 kotlin gradle 问题造成的。可能重复

Test class:

测试类:

@RunWith(AndroidJUnit4.class)
public class LocalDatabaseTest {

    private PhotoDao mPhotoDao;
    private AppDatabase mDb;

    @Before
    public void createDb() {
        Context context = InstrumentationRegistry.getTargetContext();
        mDb = Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class).build();
        mPhotoDao = mDb.photoDao();
    }

    @After
    public void closeDb() throws IOException {
    //mDb.close();
}

    @Test
    public void testPreConditions() {
        assertNotNull(mDb);
   }

Dao:

道:

    @Dao
    public interface PhotoDao {
    @Delete()
    public void delete(Photo... photos);

    @Update
    public void update(Photo ... photos);

    @Insert
    public void insert(Photo ... photos);
    }

Database:

数据库:

@Database(entities = {Photo.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract PhotoDao photoDao();
}

Stack Trace:

堆栈跟踪:

java.lang.RuntimeException: cannot find implementation for *PackageName*.AppDatabase. AppDatabase_Impl does not exist
at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:90)
at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:340)
at pics.chooz.choozpics.LocalDatabaseTest.createDb(LocalDatabaseTest.java:40)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)

Gradle:

摇篮:

apply plugin: "com.android.application"
apply plugin: "android-apt"

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "*Package Name*"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 50
        versionName "1.0.32"
        multiDexEnabled true
        testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"
}

dexOptions {
    javaMaxHeapSize "4g"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
    }
    debug {
        debuggable true
    }
}

lintOptions {
    abortOnError false
    disable "ResourceType"
}

sourceCompatibility = 1.7
targetCompatibility = 1.7
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_7
    targetCompatibility JavaVersion.VERSION_1_7
}

packagingOptions {
    exclude 'META-INF/ASL2.0'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/NOTICE'
}
}
dependencies {

androidTestCompile "com.android.support:support-annotations:$androidVersion"
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'

    compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
    annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
}

回答by dxpelou

I changed the 'annotationProcessor' keyword to 'kapt' in my gradle file. Like so:

我将 gradle 文件中的“annotationProcessor”关键字更改为“kapt”。像这样:

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

回答by solidak

Rule of thumb when using Kotlin:

使用 Kotlin 时的经验法则:

Replace your annotationProcessordependencies with kapt. Also, include apply plugin: 'kotlin-kapt'in your app's build.gradle.

更换你annotationProcessor有依赖关系kapt。此外,包括 apply plugin: 'kotlin-kapt'在您的应用程序的build.gradle.

回答by Simon Rolin

Have a look on this thread

看看这个线程

The solution is to replace :

解决方法是更换:

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

with :

和 :

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

回答by galaxigirl

You must add the annotation processor dependency to the module where your AppDatabase is. I assumed that the app would take the depencency from my API library module, where my data model classes are, but apparently this is not the case.

您必须将注释处理器依赖项添加到 AppDatabase 所在的模块中。我假设该应用程序将从我的 API 库模块中获取依赖,我的数据模型类所在的位置,但显然情况并非如此。

Answer came from this google issue: https://issuetracker.google.com/issues/67520422And this SO answer: https://stackoverflow.com/a/43918701/1959110

答案来自这个谷歌问题:https: //issuetracker.google.com/issues/67520422而这个答案是:https: //stackoverflow.com/a/43918701/1959110

回答by Javatar

Had the same issue with

有同样的问题

  • gradle 3.2.0-alpha09
  • koltin 1.2.31
  • gradle 3.2.0-alpha09
  • 科尔廷 1.2.31

Due the removal of apply plugin: 'kotlin-kapt'and other buggy stuff.. I had to downgrade to kotlin 1.1.60 to make it work. Then use:

由于删除了apply plugin: 'kotlin-kapt'和其他有问题的东西..我不得不降级到 kotlin 1.1.60 才能让它工作。然后使用:

apply plugin: 'kotlin-kapt'

dependencies {
    ...
    implementation 'android.arch.persistence.room:runtime:1.0.0'
    kapt "android.arch.persistence.room:compiler:1.0.0"
}

an other option would be to write the DB entities/DAOs and DataBase in Java instead.

另一种选择是用 Java 编写 DB 实体/DAO 和数据库。

回答by Zeeshan Ali

In my case I have change scheme but forgot to change version number.

就我而言,我有更改方案但忘记更改版本号。

@Database(entities = {Task1.class, Task2.class}, version = 2)

@Database(entities = {Task1.class, Task2.class}, version = 2)

回答by Shaon

For kotlin

对于科特林

 //Room
implementation "android.arch.persistence.room:runtime:1.0.0"
//Room annotation processor
kapt "android.arch.persistence.room:compiler:1.0.0"

apply plugin: 'kotlin-kapt'

回答by Musa Abdul Wadud

change the annotationProcessor from annotationProcessor 'android.arch.persistence.room:runtime:1.1.0' to annotationProcessor 'android.arch.persistence.room:compiler:1.1.0'

将 annotationProcessor 从 annotationProcessor 'android.arch.persistence.room:runtime:1.1.0' 更改为 annotationProcessor 'android.arch.persistence.room:compiler:1.1.0'