Android Room Persistences library and Kotlin

I am trying to write a simple app using Kotlin and Room Persistance Library. I followed the tutorial in the Android Persistance codelab.

Here is my AppDatabase class in Kotlin:

@Database(entities = arrayOf(User::class), version = 1)
abstract class AppDatabase : RoomDatabase() {
    abstract fun userModel(): UserDao

    companion object {
        private var INSTANCE: AppDatabase? = null
        @JvmStatic fun getInMemoryDatabase(context: Context): AppDatabase {
            if (INSTANCE == null) {
                INSTANCE = Room.inMemoryDatabaseBuilder(context.applicationContext,
            return INSTANCE!!

        @JvmStatic fun destroyInstance() {
            INSTANCE = null

But when I tried to run the app, it crashes immediately. Here is the crash log:

Caused by: java.lang.RuntimeException: cannot find implementation for AppDatabase_Impl does not exist
    at com.ttp.kotlin.kotlinsample.MainKotlinActivity.onCreate(MainKotlinActivity.kt:28)

It looks like the class AppDatabase_Impl wasn't autogenerated. I checked the original java app downloaded from codelab and found that AppDatabase_Impl was autogenerated.

Kotlin version: 1.1.2-3 Room version: 1.0.0-alpha1

Is there anyone experienced with this?

Edit: Using kapt solves my problem. In my case, I have to replace annotationProcessor with kapt.


Usually in project build.gradle I define the dependencies versions:

ext {
    buildToolsVersion = '25.0.2'
    supportLibVersion = '25.3.1'
    espressoVersion = '2.2.2'
    archRoomVersion = '1.0.0-alpha1'

so in app build.gradle the dependencies look like:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    compile "${rootProject.supportLibVersion}"

    compile "${rootProject.archRoomVersion}"
    annotationProcessor "${rootProject.archRoomVersion}"
    kapt "${rootProject.archRoomVersion}"

    androidTestCompile("${rootProject.espressoVersion}", {
        exclude group: '', module: 'support-annotations'
    testCompile 'junit:junit:4.12'

Now you can define Entities Daos and Database in Kotlin.


@Database(entities = arrayOf(User::class), version = 1)
abstract class Database : RoomDatabase() {
    abstract fun userDao(): UserDao


@Entity(tableName = "user")
class User {
    @PrimaryKey(autoGenerate = true)
    var id: Int = 0
    var name: String = ""


interface UserDao {
    @Query("SELECT * FROM user")
    fun getAll(): List<User>

    fun insertAll(vararg users: User)

    fun delete(user: User)

NB: Query with parameters. Kotlin renames params, so the SQL query to retrieve all the emails that belong at an user via the userId is:

@Query("SELECT * FROM email "
            + "INNER JOIN user ON = email.userId "
            + "WHERE = :arg0")
    fun getEmailsForUser(userId: Int): List<Email>

In my case, in build.gradle, when you have "annotationProcessor" you need to duplicate with "kapt" and it works.

compile "$room_version"
annotationProcessor "$room_version"
kapt "$room_version"

Try out these steps

Step 1. Set the room_version in the project.gradle file

buildscript {
    ext.kotlin_version = '1.1.51'
    ext.room_version = '1.0.0-alpha9-1'

Step 2. Apply the kotlin-kapt plugin in the app.gradle file, and this solved my issue.

apply plugin: ''
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"

Step 3. Add the kapt dependency in the app.gradle file

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "$room_version"
    annotationProcessor "$room_version"
    kapt "$room_version"

Anyone interested in using Kotlin with Room and Data Binding can see this sample project

i almost gave up. but after doing just what dharmin007 said i also had to clean the project. that made it work. I've noticed that whenever you add kapt to gradle you MUST clean the project after synching gradle.

I don't know if there is a necessity to my answer I know that some of the above answers already included this to their answers but they added other things

ONLY ADD apply plugin: 'kotlin-kapt'

Need Your Help

ui-router nested route controller isn't being called

angularjs angular-ui-router

I am trying to call a controller which should be linked with the home.category route but it isn't being called. What's wrong in it?

npm install bower using -g vs --save-dev

node.js install npm global bower

I'm new to node and using npm to both do some node, angular and Express tutorials. I have used bower before in a tutorial. I'm pretty sure I have installed it using -g already as when i run the bo...