I have a problem with testing a Room database: when I run the test, I get this exception:
java.lang.RuntimeException: cannot find implementation for databa
In my case, adding the following annotation above the database class solves the problem.
@Database(entities = {Entities.class}, version = 1, exportSchema = false)
public abstract class TheDatabase extends RoomDatabase {...}
Thanks to @nicklocicero.
for Kotlin change the 'annotationProcessor' keyword to 'kapt' in gradle file.
kapt "android.arch.persistence.room:compiler:1.1.1"
and also add on top of the dependency file
apply plugin: 'kotlin-kapt'
ref: https://developer.android.com/jetpack/androidx/releases/room
If You Use Kotlin sure exists this code in your in grade file.
apply plugin: "kotlin-kapt"
// Room
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
Documentation: https://developer.android.com/jetpack/androidx/releases/room
I did as everybody said in above answer still no luck.
The issue on my end was that, I was doing insertion on main thread.
roomDatabase = Room.databaseBuilder(DexApplication.getInstance(),MyRoomDatabase.class,"db_name")
.fallbackToDestructiveMigration() // this is only for testing,
//migration will be added for release
.allowMainThreadQueries() // this solved the issue but remember that its For testing purpose only
.build();
Explanation:
.allowMainThreadQueries()
allows you run queries on the main thread. But keep in mind to remove it and implement, RxJava, Live Data or any other background process mechanism for querying database. Hope that would help.
Add below plugin and dependency
apply plugin: 'kotlin-kapt'
kapt "android.arch.persistence.room:compiler:1.1.1"