【发布时间】:2018-11-05 10:43:26
【问题描述】:
当我尝试构建我的应用时,我收到了这个编译错误:
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
com.example.persistence.AppDatabase, unresolved supertypes: androidx.room.RoomDatabase
持久性设置在单独的 Android 模块(持久性)中。
build.gradle
// Kotlin StdLib
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
// Room
implementation "androidx.room:room-runtime:$androidXRoom"
kapt "androidx.room:room-compiler:$androidXRoom"
implementation "androidx.room:room-rxjava2:$androidXRoom"
ext.androidXRoom = "2.1.0-alpha02"
我尝试将 kotlin 版本、房间版本更改回 Android Arch Room,但它不起作用。我还尝试清理项目并使 Android Studio 的缓存无效。但它不起作用。
编辑:AppDatabase 源
package com.example.persistence.db
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.example.persistence.post.PostDbDao
import com.example.persistence.post.PostDbEntity
@Database(entities = [PostDbEntity::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
abstract fun favoritePostsDao(): PostDbDao
companion object {
var INSTANCE: AppDatabase? = null
fun getDatabase(context: Context): AppDatabase? {
if(INSTANCE == null) {
synchronized(AppDatabase::class) {
INSTANCE = Room.databaseBuilder(context.applicationContext, AppDatabase::class.java, "post_db").build()
}
}
return INSTANCE
}
fun destroy() {
INSTANCE = null
}
}
}
【问题讨论】:
-
能否在问题中添加AppDatabase类
-
添加了 AppDatabase 类
-
请添加这个:
annotationProcessor "androidx.room:room-compiler:$room_version" // use kapt for Kotlin并尝试。 -
但我已将其添加到 build.gradle 中。我正在使用 kapt,因为我正在用 Kotlin 编写应用程序
-
你确定你有正确的进口吗?
标签: android kotlin compiler-errors