Room DB:Dao 和 RoomDatabase 类的重复类问题

krt*_*ush 8 android kotlin-android-extensions android-room

我的应用程序中有一个简单的 Room 实现,但在构建应用程序时遇到了 Dao 和 RoomDatabase 类的问题(构建失败)。

\n
    \n
  1. Dao 类是“NoteDao”

    \n
    @Dao\ninterface NoteDao {\n\n @Query("SELECT * FROM notes")\n suspend fun getAllNotes(): List<Note>\n\n @Query("SELECT * FROM notes WHERE uid = :uid")\n suspend fun getNoteByUid(uid: Int): Note\n\n @Insert(onConflict = OnConflictStrategy.REPLACE)\n suspend fun insertNote(note: Note): Long\n\n @Delete\n suspend fun deleteNote(note: Note): Int\n}\n
    Run Code Online (Sandbox Code Playgroud)\n
  2. \n
  3. 房间数据库类

    \n
     @Database(\n     entities = [Note::class],\n     version = 1,\n     exportSchema = false\n )\n abstract class AppDatabase : RoomDatabase() {\n\n abstract fun noteDao() : NoteDao\n\n companion object {\n\n     @Volatile private var instance: AppDatabase? = null\n\n     fun getDatabase(context: Context): AppDatabase =\n         instance ?: synchronized(this) { instance ?: buildDatabase(context).also { instance = it } }\n\n     private fun buildDatabase(appContext: Context) =\n         Room.databaseBuilder(appContext, AppDatabase::class.java, "NADB")\n             .fallbackToDestructiveMigration()\n             .build()\n }\n}\n
    Run Code Online (Sandbox Code Playgroud)\n
  4. \n
  5. 注释实体

    \n
     @Entity(tableName = "notes")\n data class Note (\n         @PrimaryKey(autoGenerate = true)\n         val uid: Long,\n         val title: String,\n         val content: String,\n         @ColumnInfo(name = "image_url") val imageUrl: String,\n         @ColumnInfo(name = "created_on") val createdOn: Long,\n         @ColumnInfo(name = "edited_on") val editedOn: Long\n     )\n
    Run Code Online (Sandbox Code Playgroud)\n
  6. \n
\n

构建输出错误 -

\n
    \n
  1. NoteDao_Impl.java
  2. \n
\n
\n

/Users/KK/StudioProjects/KK/app/build/ generated/source/kapt/debug/com/task/noteapp/data/daos/NoteDao_Impl.java:29:\n错误:重复的类:com.task.noteapp.data .daos.NoteDao_Impl public\n最终类 NoteDao_Impl 实现 NoteDao {

\n
\n

\n
    \n
  1. AppDatabase_Impl.java
  2. \n
\n
\n

/Users/KK/StudioProjects/KK/app/build/ generated/source/kapt/debug/com/task/noteapp/data/local/AppDatabase_Impl.java:34:\n错误:重复的类:com.task.noteapp.data .local.AppDatabase_Impl\n公共最终类AppDatabase_Impl扩展AppDatabase {

\n
\n

其次是 -

\n
\n

任务 \':app:kaptDebugKotlin\' 执行失败。\n执行时发生故障\norg.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction\njava.lang.reflect.InitationTargetException(无错误消息)

\n

例外是:org.gradle.api.tasks.TaskExecutionException:任务 \':app:kaptDebugKotlin\' 执行失败。\n原因:org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException:执行 org.jetbrains 时发生失败.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction\n由以下原因引起:java.lang.reflect.InitationTargetException

\n

引起原因:org.jetbrains.kotlin.kapt3.base.util.KaptBaseError:注释处理时出错

\n
\n

经过进一步调查,我发现相应生成的文件存在以下问题 -

\n
    \n
  1. NoteDao_Impl - 类级别错误
  2. \n
\n
\n

类“NoteDao_Impl”必须声明为抽象或在“NoteDao”中实现\nabstract方法“getAllNotes(Continuation<? super List<?extends\nNote>>)”

\n
\n

在此类中,“getAllNotes()”方法出现此错误 -

\n
\n

getAllNotes(Continuation<? super List>)\' in\n\'com.task.noteapp.data.daos.NoteDao_Impl\' 与\n\'getAllNotes(Continuation<? super List<? extends Note>>)\ 冲突' in\n\'com.task.noteapp.data.daos.NoteDao\'; 两种方法都有相同的擦除,但都不会覆盖另一个

\n
\n
    \n
  1. AppDatabase_Impl - 类级别错误
  2. \n
\n
\n

在文件中找到重复的类\n\'/Users/KK/StudioProjects/KK/app/build/ generated/ksp/debug/java/com/task/noteapp/data/local/AppDatabase_Impl.java\'

\n
\n

据我所知,我的 Room 实现并没有错误。但是,我无法\xe2\x80\x99解决问题。

\n

依赖关系 -

\n
\n
def room_version = "2.4.3"\nimplementation "androidx.room:room-runtime:$room_version"\nimplementation "androidx.room:room-ktx:$room_version"\nannotationProcessor "androidx.room:room-compiler:$room_version"\nkapt "androidx.room:room-compiler:$room_version"\nksp "androidx.room:room-compiler:$room_version"\n
Run Code Online (Sandbox Code Playgroud)\n
\n

krt*_*ush 13

kapt和ksp似乎有冲突。删除 kapt 似乎已经解决了这个问题。

kapt "androidx.room:room-compiler:$room_version"

ksp“androidx.room:房间编译器:$ room_version”