Search code examples
androidkotlinandroid-room

Type mismatch: inferred type is AutoMigration but KClass<*> was expected


I was trying to implement a simple auto migration that just adds a nullable string field to my only table, but for some reason I am getting the following error in the autoMigrations = [AutoMigration(from = 1, to = 2)] line:

Type mismatch: inferred type is AutoMigration but KClass<*> was expected

I say that this is a weird error, because even the documentation has it this way.

The full code is below:

@Database(
    version = 2,
    entities = [Note::class],
    autoMigrations = [AutoMigration(from = 1, to = 2)]
)
@TypeConverters(Converters::class)
abstract class NotesDB : RoomDatabase() {
    abstract fun noteDao(): NoteDao

    companion object {
        @Volatile
        private var INSTANCE: NotesDB? = null

        fun getDB(context: Context): NotesDB {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    NotesDB::class.java,
                    "notesDB"
                ).build()
                INSTANCE = instance
                instance
            }
        }
    }
}

Solution

  • Change room version in your build.gradle to the newest one (2.4.0-alpha04 at the moment). Then if you will get an error about room.schemaLocation, look at this answer https://stackoverflow.com/a/44424908/6568162