Access Android Room Database Version At Runtime

April 13, 2018

I believe there is no Room API to access the database version programatically.

But, you can use this trick.

Create a singleton object class (Kotlin) to store the database version.

object App {
    const val DATABASE_VERSION = 1
}

Specify the database version using the App singleton object.

@Database(entities = arrayOf(...), version = App.DATABASE_VERSION)
@TypeConverters(...)
abstract class AppDatabase : RoomDatabase() {
    ...
}

Now you can access the database version anywhere.

App.DATABASE_VERSION
This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.