Learning Android Development

Extend Android’s Room Abstraction Capability Further with KSP

An example of using KSP to overcome the current Android’s Room and Kotlin Inheritance Limitation

Photo by Harold Pagunsan on Unsplash

Room in Android is really a nice abstraction over SQLite! I learned it not too long ago. Definitely a win in implementing persistence for the App.

It has a neat feature of Inheritance as shared in an amazing article by Florina Muntenescu in point 2 of 7 Pro-tips for Room.

interface BaseDao<T> {
@Insert
fun insert(vararg obj: T)
}

@Dao
abstract class DataDao : BaseDao<Data>() {
@Query("SELECT * FROM Data")
abstract fun getData(): List<Data>
}

Room’s Limitation on Inheritance

Unfortunately, it only allows some inheritances but not all.

E.g. I create a BaseDao below

interface BaseDao<T> {

@Insert
fun insertAll(vararg obj: T)

@Delete
fun delete(obj: T)

@Update
fun update(obj: T)

fun getAll(): Flow<List<T>>

fun nukeTable()
}

I can do Insert, Delete, and Update. But if I also wanted to getAll or nukeTable entire, I’ll have to explicitly override them as below, as…

--

--