【问题标题】:AndroidX Room unresolved supertypes RoomDatabaseAndroidX Room 未解析的超类型 RoomDatabase
【发布时间】: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


【解决方案1】:

问题更可能在于您如何定义依赖项,RoomDatabase 是公共 API 的一部分,因为您的 AppDatabase 扩展了它,并且您可能在下游依赖项中使用了该类。但是 RoomDatabase 被声明为仅实现依赖项。这意味着该类在编译期间通常不适用于下游依赖项。

尝试将 "androidx.room:room-runtime:$androidXRoom" 更改为 api 配置,使其成为公共 API 的一部分。这应该可以解决您遇到的错误。

【讨论】:

    【解决方案2】:

    更改 gradle 依赖项,如

       REMOVE -> implementation "androidx.room:room-runtime:$androidXRoom"
       REPLACE WITH -> api "androidx.room:room-runtime:$androidXRoom"
    

    【讨论】:

      【解决方案3】:

      其他答案可以编译,但违背了为您的数据库/持久性功能提供单独模块的意义。

      您不应该在persistence 模块之外公开您的房间实体或房间数据库。相反,您应该在 persistence 模块中编写一个函数,将您的 PostEntity @Entity 映射到一个简单的 Post 数据类。

      我在这里写了更详细的内容:https://jacquessmuts.github.io/post/modularization_room/

      【讨论】:

        【解决方案4】:

        除了答案。当我在创建新模块时选择Module-> Android Library时,“api”过程成功完成,我提供了解决方案。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多