androidkotlinandroid-room

Problem with database declaration in android Room


If I leave @Database as it is in the code bellow, I get KSP failed with exit code: PROCESSING_ERROR. If I remove it, well then the database is not declared and I get another error.

I am sure the error happens when entities = [User::class, Task::class, Stat::class] is called, because when I remove this part the app builds fine, but then tells me I am missing STAT,TASK and USER tables (which is logical since they are not declared in entities).

Also, take into account that I am new to android studio and do not fully understand how all this works and do not know how my code "should" look.

Here is my database:

@Database(entities = [User::class, Task::class, Stat::class], version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun getUserDao(): UserDao
    abstract fun getStatDao(): StatDao
    abstract fun getTaskDao(): TaskDao

    companion object {
        const val NAME = "App_DB"
    }

}

Here are the 3 classes that are used:

User:

@Entity
data class User(
    @PrimaryKey val id: Int = 0,
    @ColumnInfo var db_username: String = "",
    @ColumnInfo var db_level: Int = 1,
    @ColumnInfo var db_coins: Int = 0,
    @ColumnInfo var db_xp: Int = 0,
    @ColumnInfo var db_stats: MutableList<Stat> = mutableListOf()
)

Stat:

@Entity
class Stat (
    @PrimaryKey(autoGenerate = true) val id: Int = 0,
    @ColumnInfo var name: String,
    @ColumnInfo var description: String = "",
    @ColumnInfo var db_tasks: MutableList<Task> = mutableListOf(),
    @ColumnInfo val owner: User
) {
    @ColumnInfo var db_level = 1
    @ColumnInfo var db_totalXP = 0
    @ColumnInfo var db_currentXP = 0
    @ColumnInfo var db_neededXP = 1
}

Task:

@Entity
data class Task(
    @PrimaryKey(autoGenerate = true) val id:Int = 0,
    @ColumnInfo var name: String,
    @ColumnInfo var xpPerUnit: Int = 1,
    @ColumnInfo var xpToday: Int = 0,
    @ColumnInfo var notes: String = "",
    @ColumnInfo val stat: Stat
) {
    @ColumnInfo var db_level = 1
    @ColumnInfo var db_totalXP = 0
    @ColumnInfo var db_currentXP = 0
    @ColumnInfo var db_neededXP = 1
}

Solution

  • The error was in my total lack of type converters.

    The type converters in MikeT's anwser seem to work.