会议室“无法找到实现"即使使用kapt [英] Room "cannot find implementation for" even with using 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屋!