本文介绍了调用 Rooms inMemoryBuilder 方法时的 Room Persistence Library 运行时异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

测试类:

@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
    public interface PhotoDao {
    @Delete()
    public void delete(Photo... photos);

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

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

数据库:

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

堆栈跟踪:

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)

摇篮:

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"
}

推荐答案

我在我的 gradle 文件中将 'annotationProcessor' 关键字更改为 'kapt'.像这样:

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

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

这篇关于调用 Rooms inMemoryBuilder 方法时的 Room Persistence Library 运行时异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:31