Error implementing Room Persistence Library + Kotlin

Asked

Viewed 311 times

1

When trying to run an application developed in Kotlin with ROOM Persistence Library I have the following return:

java.lang.Runtimeexception: cannot find implementation for br.com.androidxexample.Database.Appdatabase. Appdatabase_impl does not exist

The error occurs when trying to run databaseBuilder

val db = Room.databaseBuilder(
   applicationContext,
   AppDatabase::class.java, "localDB"
).build()

build.Gradle (Module:App)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "br.com.androidxexample"
        minSdkVersion 22
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.core:core-ktx:1.1.0-alpha05'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    def room_version = "2.1.0-alpha06"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
}

GITHUB of the Project: https://github.com/tpoderoso/AndroidXExample

  • Add your project dependencies to the question.

1 answer

1


The Room generates at compile time some classes for its operation:

  • A class that will extend the abstract class you wrote down with @Database
  • Classes that implement / extend the interfaces / classes you annotate with @Dao

The library responsible for this is the androidx.room:room-compiler. Its operation depends on a annotation processor.

Normally you would add this dependency with the function annotationProcessor. But since you’re using Kotlin, it has a compiler plugin for this called kapt.

Your build.Radle should import the plugin kotlin-kapt and import dependency with the kapt:

apply plugin: 'kotlin-kapt'

...
...

dependencies {
    ...
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.