会议室“无法找到实现"即使使用kapt [英] Room "cannot find implementation for" even with using kapt

查看:98
本文介绍了会议室“无法找到实现"即使使用kapt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的项目中使用Room. Gradle可以很好地同步文件,但是在尝试获取数据库实例时出现RunitomeException.

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

"原因:java.lang.RuntimeException:找不到com.fillooow.android.testtochka.BusinessLogic.database.GithubUserSearchDataBase的实现.GithubUserSearchDataBase_Impl不存在"

我搜索了此问题,发现解决方案是将以下行添加到build.gradle文件中:

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"

并且也使用了该插件

apply plugin: 'kotlin-kapt'

但这是我的build.gradle文件,仍然存在此问题:

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

这是数据库类

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?

推荐答案

您的gradle文件看起来不错.添加正确的导入后,只需确保Sync即可.

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

您缺少的是数据库类顶部的@Database注释.

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

@Database注释的entities属性中,您必须放置一个数组,其中模型的所有类都用@Entity注释注释.我在这里放假的名字,你应该放上正确的名字.

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.

这篇关于会议室“无法找到实现"即使使用kapt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆