Room Persistence Library run time exception when calling Rooms inMemoryBuilder method

20,062

Solution 1

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

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

Solution 2

Rule of thumb when using Kotlin:

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

Solution 3

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"

Solution 4

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.

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

Solution 5

Had the same issue with

  • gradle 3.2.0-alpha09
  • koltin 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'

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.

Share:
20,062

Related videos on Youtube

dxpelou
Author by

dxpelou

"Java Programmer" :)

Updated on July 09, 2022

Comments

  • dxpelou
    dxpelou almost 2 years

    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: cannot find implementation for PackageName.AppDatabase. AppDatabase_Impl does not exist

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

    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"
    }
    
    • Moinkhan
      Moinkhan almost 7 years
      Is it throwing error in testing only? or in developmene also .?
    • dxpelou
      dxpelou almost 7 years
      @Moinkhan throwing the same error for both
    • yigit
      yigit almost 7 years
      what is the package for AppDatabase?
  • Bohsen
    Bohsen almost 7 years
    Same problem here. This fixed it. Going back to a deprecated annotation processor is not optimal though.
  • galaxigirl
    galaxigirl over 6 years
    This did not fix it for me - I'm already using annotationProcessor
  • Russell Ladd
    Russell Ladd over 6 years
    apply plugin: 'kotlin-kapt' is a must! Even though your build works without adding it, adding it makes the annotation processor behave differently (e.g. not renaming parameters needed for SQL placeholders)
  • kosiara - Bartosz Kosarzycki
    kosiara - Bartosz Kosarzycki over 6 years
    Thanks - as you said kapt "android.arch.persistence.room:compiler:$archRoomVersion" instead of 'annotationProcessor' does the job
  • user3144836
    user3144836 about 6 years
    Don't forget to clean your project afterward.
  • Phan Sinh
    Phan Sinh about 6 years
    I have conflict with Glide when change to kapt. How can i fix it?
  • dxpelou
    dxpelou about 6 years
    @PhanSinh whats the conflict?
  • Phan Sinh
    Phan Sinh about 6 years
    @dxpelou I have fixed. An Entity missing @Notnull annotation in primary key
  • Laurenz Albe
    Laurenz Albe almost 6 years
    To turn this into a good answer, you should add some more information. Why does this solve the problem?