Home > Net >  Room DB: Duplicate class issues with Dao and RoomDatabase classes
Room DB: Duplicate class issues with Dao and RoomDatabase classes

Time:01-08

I have a simple Room implementation in my app and am having issues with the Dao and RoomDatabase classes upon building the app (Build is failing).

  1. Dao class is 'NoteDao'

    @Dao
    interface NoteDao {
    
     @Query("SELECT * FROM notes")
     suspend fun getAllNotes(): List<Note>
    
     @Query("SELECT * FROM notes WHERE uid = :uid")
     suspend fun getNoteByUid(uid: Int): Note
    
     @Insert(onConflict = OnConflictStrategy.REPLACE)
     suspend fun insertNote(note: Note): Long
    
     @Delete
     suspend fun deleteNote(note: Note): Int
    }
    
  2. RoomDatabase class

     @Database(
         entities = [Note::class],
         version = 1,
         exportSchema = false
     )
     abstract class AppDatabase : RoomDatabase() {
    
     abstract fun noteDao() : NoteDao
    
     companion object {
    
         @Volatile private var instance: AppDatabase? = null
    
         fun getDatabase(context: Context): AppDatabase =
             instance ?: synchronized(this) { instance ?: buildDatabase(context).also { instance = it } }
    
         private fun buildDatabase(appContext: Context) =
             Room.databaseBuilder(appContext, AppDatabase::class.java, "NADB")
                 .fallbackToDestructiveMigration()
                 .build()
     }
    }
    
  3. Note Entity

     @Entity(tableName = "notes")
     data class Note (
             @PrimaryKey(autoGenerate = true)
             val uid: Long,
             val title: String,
             val content: String,
             @ColumnInfo(name = "image_url") val imageUrl: String,
             @ColumnInfo(name = "created_on") val createdOn: Long,
             @ColumnInfo(name = "edited_on") val editedOn: Long
         )
    

Build Output errors -

  1. NoteDao_Impl.java

/Users/KK/StudioProjects/K-K/app/build/generated/source/kapt/debug/com/task/noteapp/data/daos/NoteDao_Impl.java:29: error: duplicate class: com.task.noteapp.data.daos.NoteDao_Impl public final class NoteDao_Impl implements NoteDao {

and

  1. AppDatabase_Impl.java

/Users/KK/StudioProjects/K-K/app/build/generated/source/kapt/debug/com/task/noteapp/data/local/AppDatabase_Impl.java:34: error: duplicate class: com.task.noteapp.data.local.AppDatabase_Impl public final class AppDatabase_Impl extends AppDatabase {

followed by -

Execution failed for task ':app:kaptDebugKotlin'. A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction java.lang.reflect.InvocationTargetException (no error message)

Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptDebugKotlin'. Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction Caused by: java.lang.reflect.InvocationTargetException

Caused by: org.jetbrains.kotlin.kapt3.base.util.KaptBaseError: Error while annotation processing

On further investigation, I found the following issues with the respective generated files -

  1. NoteDao_Impl - class level error

Class 'NoteDao_Impl' must either be declared abstract or implement abstract method 'getAllNotes(Continuation<? super List<? extends Note>>)' in 'NoteDao'

and within this class, the 'getAllNotes()' method had this error -

getAllNotes(Continuation<? super List>)' in 'com.task.noteapp.data.daos.NoteDao_Impl' clashes with 'getAllNotes(Continuation<? super List<? extends Note>>)' in 'com.task.noteapp.data.daos.NoteDao'; both methods have same erasure, yet neither overrides the other

  1. AppDatabase_Impl - class level error

Duplicate class found in the file '/Users/KK/StudioProjects/K-K/app/build/generated/ksp/debug/java/com/task/noteapp/data/local/AppDatabase_Impl.java'

As far as I can see that my implementation of Room is not wrong. However, I can’t figure out the issues.

Dependencies -

def room_version = "2.4.3"
implementation "androidx.room:room-runtime:$room_version"
implementation "androidx.room:room-ktx:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
kapt "androidx.room:room-compiler:$room_version"
ksp "androidx.room:room-compiler:$room_version"

CodePudding user response:

Try this version,

def room_version = "2.5.0-beta01"

I found the same issue,, tried everything, it didn't work. Then I just update my dependencies of room along with navigational components. That fixed my problem.

CodePudding user response:

kapt and ksp seems to be in conflict. Removing kapt seems to have solved the issue.

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

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

  • Related