3

The following line of code crashes hard

Room.databaseBuilder(
        applicationContext,
        MyDatabase::class.java, "MyDB")
        .build()

Here's the Minimal Complete Verifiable (MCV) app you can easily duplicate

In the build.gradle for the project, make sure the following are present

allprojects {
    repositories {
        google()
        jcenter()
    }
}

In the build.gradle for the app, make sure the following are present

dependencies {
    def room_version = "1.1.1"

    implementation "android.arch.persistence.room:runtime:$room_version"
    kapt  "android.arch.persistence.room:compiler:$room_version" // because I'm using Kotlin
}

All of the setup instructions is per the official document https://developer.android.com/topic/libraries/architecture/adding-components

Now just use Android Studio and create a project using an "Empty Activity" template. Only 3 new files are needed in this MCV example.

Create a first file called MyThing.kt

//MyThing.kt
import android.arch.persistence.room.ColumnInfo
import android.arch.persistence.room.Entity

@Entity(tableName = "myThing")
data class MyThing(@ColumnInfo var myString: String)

Create a second file called MyDao.kt

import android.arch.persistence.room.Dao
import android.arch.persistence.room.Insert
import android.arch.persistence.room.OnConflictStrategy

@Dao
interface MyDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(myThing: MyThing)
}

Create a third file called MyDatabase.kt

import android.arch.persistence.room.Database
import android.arch.persistence.room.RoomDatabase

@Database(entities = arrayOf(MyThing::class), version = 1)
abstract class MyDatabase : RoomDatabase() {
    abstract fun myDao(): MyDao
}

Now modify the MainActivity class

class MainActivity : AppCompatActivity() {

    private var myDB: MyDatabase? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        myDB = Room.databaseBuilder(
            applicationContext,
            MyDatabase::class.java, "MyDB")
            .build()
    }
}

Running the app now will cause a hard crash on the line that I mentioned above. What am I doing wrong?

UPDATE: Logcat output

--------- beginning of crash 12-29 13:23:46.098 12801-12801/com.le.kevin.roommcv E/AndroidRuntime: FATAL EXCEPTION: main Process: com.le.kevin.roommcv, PID: 12801 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.le.kevin.roommcv/com.le.kevin.roommcv.MainActivity}: java.lang.RuntimeException: cannot find implementation for com.le.kevin.roommcv.MyDatabase. MyDatabase_Impl does not exist at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.RuntimeException: cannot find implementation for com.le.kevin.roommcv.MyDatabase. MyDatabase_Impl does not exist at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:93) at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:630) at com.le.kevin.roommcv.MainActivity.onCreate(MainActivity.kt:19) at android.app.Activity.performCreate(Activity.java:6251) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  12-29 13:23:46.099 777-1936/? W/ActivityManager: Force finishing activity com.le.kevin.roommcv/.MainActivity

Impl?

Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80
  • 2
    Use Logcat to examine the Java stack trace associated with the crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this. My guess is that the problem is that you are doing this on the main application thread. If so, the exception would reflect that. – CommonsWare Dec 29 '18 at 19:04
  • So yes, the creation of the database was done in the main thread. No query has been called yet. I thought only the query would need to be executed in a separate thread? – Kevin Le - Khnle Dec 29 '18 at 19:09
  • Possible duplicate of [Room + cannot find implementation DB + DB\_Impl does not exist](https://stackoverflow.com/questions/50453822/room-cannot-find-implementation-db-db-impl-does-not-exist) – musooff Dec 30 '18 at 14:37

1 Answers1

1

I got it to work by adding

apply plugin: 'kotlin-kapt'

in the build.graddle for app

I found the answer thanks to looking at the logcat suggested by the comment above, and then by the answer Room + cannot find implementation DB + DB_Impl does not exist

The official documentation https://developer.android.com/topic/libraries/architecture/adding-components is incomplete

Kevin Le - Khnle
  • 10,579
  • 11
  • 54
  • 80
  • 1
    The official documentation is not incomplete. It's your responsibility to add `apply plugin: 'kotlin-kapt'` when you use `kapt`. They don't have to mention it explicitly. Because with that logic, they should also add "Install Android Studio" in the beginning. – musooff Jan 13 '19 at 09:34