【问题标题】:class file for androidx.room.RoomDatabase not found when trying to use Hilt with Roomdb尝试将 Hilt 与 Roomdb 一起使用时,找不到 androidx.room.RoomDatabase 的类文件
【发布时间】:2021-06-10 18:06:03
【问题描述】:

这是我收到的错误消息。我检查了网络,我有所有需要的依赖项,我正确设置了一切。问题是我已经在另一个工作正常的项目上进行了这个设置,但由于某种原因它在这里不起作用。我尝试使缓存无效,重新启动等,但没有任何结果。我对造成这种情况的原因感到非常困惑。

error: cannot access RoomDatabase
    return DatabaseModule_ProvideAppRepositoryFactory.provideAppRepository(databaseModule, appDatabase(), ApplicationContextModule_ProvideContextFactory.provideContext(applicationContextModule));
                                                                          ^
  class file for androidx.room.RoomDatabase not found
1 error

这是数据库模块

@Module
@InstallIn(SingletonComponent::class)
class DatabaseModule {
    @Provides
    @Singleton
    fun provideAppDatabase(@ApplicationContext context: Context): AppDatabase {
        return Room.databaseBuilder(
            context,
            AppDatabase::class.java, "app-db"
        ).build()
    }

    @Provides
    fun provideAppRepository(
        db: AppDatabase,
        @ApplicationContext ctx : Context
    ): AppRepository {
        return AppRepository(db, ctx)
    }

这些是我与 Kotlin Kapt 一起的依赖项

Plugins{
    id(GradlePluginId.ANDROID_LIBRARY)
    id(GradlePluginId.KOTLIN_ANDROID)
    id(GradlePluginId.KOTLIN_KAPT)
    id(GradlePluginId.HILT_ANDROID)
}
...

  implementation(LibraryDependency.ROOM)
    implementation(LibraryDependency.ROOM_KTX)
    kapt(LibraryDependency.ROOM_COMPILER)

Which is this 2.2.6 version, but i've been using the "2.3.0-beta02" as well
    const val ROOM = "androidx.room:room-runtime:${LibraryVersion.ROOM}"
    const val ROOM_COMPILER = "androidx.room:room-compiler:${LibraryVersion.ROOM}"
    const val ROOM_KTX = "androidx.room:room-ktx:${LibraryVersion.ROOM}"
@Database(entities = [CBImageDataModel::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun imageDao(): ImageDao
}

@Entity(tableName = "images")
@TypeConverters(ImageTypeConverters::class)
data class CBImageDataModel(
    @PrimaryKey(autoGenerate = true) val id: Long? = null,
    @ColumnInfo(name = "server_id") val serverID: Int
)

class AppRepository @Inject constructor(private val db: AppDatabase, private val context: Context) { ... 

【问题讨论】:

    标签: android android-gradle-plugin android-room dagger-2 dagger-hilt


    【解决方案1】:

    您是否尝试过 Build > Clean ProjectBuild > Rebuild Project 并且还使用@PrimaryKey(autoGenerate = true) val id: Long, 而不是@PrimaryKey(autoGenerate = true) val id: Long? = null, 作为主键将自动生成。见Room Database

    【讨论】:

    • 我已经尝试了大约 10 次清理和重建,只是做了一些小的改动。但是,如果我将 id 设置为 val Long,我该如何构造对象呢?当我创建一个实例时,我还必须提供 id 字段吗?
    【解决方案2】:

    将上面列出的依赖项添加到我的 App 的 Build gradle 中,而不仅仅是使用依赖项的模块...

    build.gradle.kts(应用程序)

    Plugins{
        id(GradlePluginId.ANDROID_LIBRARY)
        id(GradlePluginId.KOTLIN_ANDROID)
        id(GradlePluginId.KOTLIN_KAPT)
        id(GradlePluginId.HILT_ANDROID)
    }
    ...
    
      implementation(LibraryDependency.ROOM)
        implementation(LibraryDependency.ROOM_KTX)
        kapt(LibraryDependency.ROOM_COMPILER)
    
    Which is this 2.2.6 version
        const val ROOM = "androidx.room:room-runtime:${LibraryVersion.ROOM}"
        const val ROOM_COMPILER = "androidx.room:room-compiler:${LibraryVersion.ROOM}"
        const val ROOM_KTX = "androidx.room:room-ktx:${LibraryVersion.ROOM}"
        ```
    

    【讨论】:

    • 问题在于您将房间运行时依赖项声明为实现细节。如果您在非应用模块中将implementation 更改为api,那么您不必再在应用模块中指定它。刚遇到这个问题。
    猜你喜欢
    • 2020-08-06
    • 1970-01-01
    • 2012-03-16
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多