代码之家  ›  专栏  ›  技术社区  ›  chrming

为什么是androidx。房间RoomDatabase未被识别?

  •  0
  • chrming  · 技术社区  · 2 年前

    我正在学习模块化,在我的一个模块中 androidx.room.* 不被认可。降级房间版本只对 @Database 注释。而且,模块似乎看不到 Room.roomKtx 实现,因为快速帮助建议将此实现添加到 build.gradle 我的 科特林版本 1.6.21

    档案数据库。kt

    import androidx.room.Database
    import androidx.room.RoomDatabase
    import com.example.modules.Gift
    import com.example.modules.Profile
    
    @Database(
        entities = [Profile::class, Gift::class],
        version = 1
    )
    abstract class ProfileDatabase: RoomDatabase() {
        abstract val profileDao: ProfileDao
    
        companion object {
            const val DATABASE_NAME = "profile_db"
        }
    }
    

    建筑gradle(:profileDatabase)

    apply {
        from("$rootDir/library-build.gradle")
        plugin 'kotlin-kapt'
    }
    
    dependencies {
        implementation(project(':modules'))
        implementation Kotlinx.coroutinesCore
    
        implementation Room.runtime
        implementation Room.roomKtx
        kapt Room.compiler
    
    }
    

    房间kt

    object Room {
        private const val roomVersion = "2.4.2"
        const val runtime = "androidx.room:room-runtime:$roomVersion"
        const val compiler = "androidx.room:room-compiler:$roomVersion"
        const val roomKtx = "androidx.room:room-ktx:$roomVersion"
    }
    
    0 回复  |  直到 2 年前
        1
  •  0
  •   Simpa    2 年前

    在我看来这是正确的。但是你是否在正确的构建中添加了房间依赖性。格雷德尔档案?您在导入文件室的同一个gradle文件中导入“modules”模块,我怀疑您已将DB实现放在您要导入的模块中。

        2
  •  0
  •   chrming    2 年前

    问题是“模块类型”错误。我曾经 apply plugin:'java-library' ,
    但我们应该使用 apply plugin:'com.android.library' .