Home > other >  Duplicate class error when using Room for initalizing a Database
Duplicate class error when using Room for initalizing a Database

Time:01-17

I started to mess around with Room and right now all I did was making a dummy class with a simple Database impl for Room in Kotlin.

So far I've got:

@Database(entities = [Junk::class], version = 1)
abstract class MyDatabase : RoomDatabase() {

      companion object {

        private var appDatabaseInstance: MyDatabase? = null

        fun getDatabase(applicationContext: Context): MyDatabase {
            if (appDatabaseInstance == null) {
                appDatabaseInstance = Room.databaseBuilder(
                    applicationContext,
                    MyDatabase::class.java,
                    "my_db"
                ).build()
            }
            return appDatabaseInstance!!
        }

    }
}

And my Junk entity is just a very simple dummy class for now:

@Entity
class Junk {

    @PrimaryKey
    var id = 0
    var name: String?=null

}

Here comes the error message:

C:\android_projects\room_test_app\app\build\generated\source\kapt\debug\hu\roomtest\stuff\db\MyDatabase_Impl.java:34: error: duplicate class: hu.roomtest.stuff.db.MyDatabase_Impl
public final class MyDatabase_Impl extends MyDatabase {
             ^

What may cause this?

Thanks in advance.

E D I T:

I've added a DAO just to mess around and it says the very same for my DAO:

C:\android_projects\room_test_app\app\build\generated\source\kapt\debug\hu\roomtest\stuff\db\JunkDAO_Impl.java:34: error: duplicate class: hu.roomtest.stuff.db.JunkDAO_Impl
    public final class MyDatabase_Impl extends MyDatabase {
                 ^

E D I T 2:

Room's gradle config:

    def room_version = "2.5.0"

    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"

    // To use Kotlin annotation processing tool (kapt)
    kapt "androidx.room:room-compiler:$room_version"
    // To use Kotlin Symbol Processing (KSP)
    ksp "androidx.room:room-compiler:$room_version"
    // optional - RxJava3 support for Room
    implementation "androidx.room:room-rxjava3:$room_version"
    // optional - Test helpers
    testImplementation "androidx.room:room-testing:$room_version"

Also inside the android/defaultConfig I have this:

 javaCompileOptions {
            annotationProcessorOptions {
                arguments  = ["room.schemaLocation": "$projectDir/schemas".toString()]
            }
        }

E D I T 3:

When I try to "Make Project" (CTRL F9) it says the very same error message, also with "Clean Project" and "Rebuild Project".

CodePudding user response:

Well, after trying a couple of solutions in comments, it turns out that the KSP annotation conflicts with KAPT one; each generates its own version of the Room Database class:

// To use Kotlin annotation processing tool (kapt)
kapt "androidx.room:room-compiler:$room_version"
// To use Kotlin Symbol Processing (KSP)
ksp "androidx.room:room-compiler:$room_version"

Both, kapt and ksp are alternative to one another; the latter is 2x faster than kapt.

As per documentation:

Kotlin Symbol Processing (KSP) is an API that you can use to develop lightweight compiler plugins. KSP provides a simplified compiler plugin API that leverages the power of Kotlin while keeping the learning curve at a minimum. Compared to KAPT, annotation processors that use KSP can run up to 2x faster.

So, you need to keep either one to solve this conflict. But notice that the ksp has some build procedure that you need to follow.

  • Related