Room “cannot find implementation for” even with us

2019-07-25 18:19发布

问题:

I am trying to use Room in my project. Gradle syncing files well, but I get RunitomeException when trying to get database instance.

"Caused by: java.lang.RuntimeException: cannot find implementation for com.fillooow.android.testtochka.BusinessLogic.database.GithubUserSearchDataBase. GithubUserSearchDataBase_Impl does not exist"

I searched this issue and find that solution is to add this lines into build.gradle file:

implementation "android.arch.persistence.room:runtime:1.1.1"
implementation "android.arch.persistence.room:rxjava2:1.1.1"
kapt "android.arch.persistence.room:compiler:1.1.1"

and also aply this plugin

apply plugin: 'kotlin-kapt'

But this is my build.gradle file, and I still have this issue:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.fillooow.android.testtochka"
        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(include: ['*.jar'], dir: 'libs')
    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'
    implementation 'com.android.support:support-v4:28.0.0'
    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 'com.squareup.retrofit2:converter-gson:2.5.0'
    implementation 'com.squareup.retrofit2:retrofit:2.5.0'
    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.jakewharton.rxbinding2:rxbinding-kotlin:2.0.0'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'com.facebook.android:facebook-android-sdk:[4,5)'
    implementation 'com.vk:androidsdk:1.6.9'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation "android.arch.persistence.room:runtime:1.1.1"
    implementation "android.arch.persistence.room:rxjava2:1.1.1"
    kapt "android.arch.persistence.room:compiler:1.1.1"
}

And this is DataBase class

import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.content.Context

abstract class GithubUserSearchDataBase : RoomDatabase(){

    abstract fun githubUserSearchDataDao(): GithubUserSearchDataDao

    companion object {
        private var INSTANCE: GithubUserSearchDataBase? = null

        fun getInstance(context: Context): GithubUserSearchDataBase?{
            if (INSTANCE == null){
                synchronized(GithubUserSearchDataBase::class){
                    INSTANCE = Room.databaseBuilder(context.applicationContext,
                        GithubUserSearchDataBase::class.java,
                        "github.db")
                    .build()
                }
            }
            return INSTANCE
        }

        fun destroyInstance(){
            INSTANCE = null
        }
    }
}

Project were cleared and rebuild a lot of times. So, maybe I missed something?

回答1:

Your gradle file looks fine. Just be sure to Sync it after you have added the proper imports.

What you are missing is the @Database annotation on top of your Database class.

@Database(entities = [Entity1::class, Entity2::class, Entity3::class, Entity4::class], version = 1)
abstract class GithubUserSearchDataBase : RoomDatabase(){

    abstract fun githubUserSearchDataDao(): GithubUserSearchDataDao

    companion object {
        private var INSTANCE: GithubUserSearchDataBase? = null

        fun getInstance(context: Context): GithubUserSearchDataBase?{
            if (INSTANCE == null){
                synchronized(GithubUserSearchDataBase::class){
                    INSTANCE = Room.databaseBuilder(context.applicationContext,
                        GithubUserSearchDataBase::class.java,
                        "github.db")
                    .build()
                }
            }
            return INSTANCE
        }

        fun destroyInstance(){
            INSTANCE = null
        }
    }
}

In the entities attribute of the @Database annotation you must put an array with all the classes of your model annotated with the @Entity annotation. I put there fake names, you should put the proper ones.