Room cannot find implementation

前端 未结 11 1792
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 07:17

I have a problem with testing a Room database: when I run the test, I get this exception:

java.lang.RuntimeException: cannot find implementation for databa         


        
相关标签:
11条回答
  • 2020-12-09 07:46

    I'm coming a little bit late but I had the same issue but none of the answers helped me. So I hope my experience will help others. Room couldn't find the implementation while executing my instrumentation tests so adding below line to my gradle file fixed the problem

    kaptAndroidTest "androidx.room:room-compiler:$room_version"
    

    If you use annotation processors for your androidTest or test sources, the respective kapt configurations are named kaptAndroidTest and kaptTest

    0 讨论(0)
  • 2020-12-09 07:51

    java.lang.RuntimeException: cannot find implementation for com.example.kermovie.data.repository.local.MoviesDatabase. MoviesDatabase_Impl does not exist

    You have to add teh following code on your build.gradel:

    apply plugin: 'kotlin-kapt' 
    
    dependencies {
        implementation 'androidx.room:room-runtime:2.2.5'
        implementation "androidx.room:room-ktx:2.2.5"
        kapt "androidx.room:room-compiler:2.2.5"
    }
    
    0 讨论(0)
  • 2020-12-09 07:55

    I will add complete answer that solved my problem

    First add all the room dependency of android x second add apply plugin: 'kotlin-kapt' and replace annotationProcessor of room compiler with kept

    0 讨论(0)
  • 2020-12-09 07:56

    kapt is for Kotlin.

    First, add:

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

    to your dependencies closure.

    Then, upgrade android.arch.persistence.room:rxjava2 and android.arch.persistence.room:testing to 1.0.0 instead of 1.0.0-rc1.

    ref: https://developer.android.com/jetpack/androidx/releases/room

    0 讨论(0)
  • 2020-12-09 07:57

    This gradle works nicely. Notice the plugin kotlin-kapt at the beggining. It's crucial.

    apply plugin: 'kotlin-kapt'
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "????"
            minSdkVersion 21
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    
        implementation 'android.arch.persistence.room:runtime:1.1.1'
        kapt 'android.arch.persistence.room:compiler:1.1.1'
    }
    
    0 讨论(0)
  • 2020-12-09 07:59

    I see some people around here (Vishu Bhardwaj and others on similar StackOverflow questions) complains the accepted answer does not work for them. I think it deserve mentioning I had a similar problem in a MULTI modules project that needed a trick around the accepted answer.

    In my multi modules project the room database dependencies where included mostly in one basic module and exported using proper api notation but room code was split along 2 different modules. Project compiled with no warning but java.lang.RuntimeException: cannot find implementation for database... was raised on run-time.

    This was fixed by adding

    annotationProcessor "android.arch.persistence.room:compiler:$room_version"
    

    in ALL modules that included Room related code.

    0 讨论(0)
提交回复
热议问题