Number cannot find implementation

I have a problem with testing the Room database: when I run the test, I get this exception:

java.lang.RuntimeException: cannot find implementation for database.TicketDatabase. TicketDatabase_Impl does not exist at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92) at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:454) at com.sw.ing.gestionescontrini.DatabaseTest.setDatabase(DatabaseTest.java:36) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886) 

DatabaseTest Class:

 public class DatabaseTest { TicketDatabase database; DAO ticketDAO; @Before public void setDatabase() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Context context = InstrumentationRegistry.getTargetContext(); database = Room.inMemoryDatabaseBuilder(context, TicketDatabase.class).build(); Method method = TicketDatabase.class.getDeclaredMethod("ticketDao()"); method.setAccessible(true); ticketDAO = (DAO) method.invoke(database, null); } } 

gradle file:

 apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion '26.0.2' defaultConfig { applicationId "com.sw.ing.gestionescontrini" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } defaultConfig { multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' annotationProcessor 'android.arch.lifecycle:compiler:1.0.0' compile 'android.arch.persistence.room:rxjava2:1.0.0-rc1' testCompile'android.arch.persistence.room:testing:1.0.0-rc1' } 

I do not know what this implementation should be, I was looking for a solution, but everything I found does not work for me. For example, many have found a solution adding kapt "android.arch.persistence.room ...", but gradle does not recognize "kapt".

+36
android implementation android-room
source share
9 answers

kapt for Kotlin.

First add:

 annotationProcessor "android.arch.persistence.room:compiler:1.0.0" 

to close dependencies .

Then update android.arch.persistence.room:rxjava2 and android.arch.persistence.room:testing to 1.0.0 instead of 1.0.0-rc1 .

link: https://developer.android.com/jetpack/androidx/releases/room

+57
source share

for Kotlin, change the annotationProcessor keyword to kapt in the Gradle file.

 kapt "android.arch.persistence.room:compiler:1.1.1" 

and add over dependencies file

apply plugin: 'kotlin-kapt'

link: https://developer.android.com/jetpack/androidx/releases/room

+18
source share

If you use Kotlin, this code is necessarily present in your ratings 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

+7
source share

I see that some people here (Vishu Bhardwaj and others on similar StackOverflow issues) complain that the accepted answer does not suit them. I think it is worth mentioning that I had a similar problem in the MULTI modules project, which needed a trick around the accepted answer.

In my projects with several modules, the dependencies of the room database depend on which basically one basic module is used and exported using the correct api entry, but the room code was divided into two different modules. The project compiled without warning, but java.lang.RuntimeException: could not find an implementation for the database ... was raised at run time.

This has been fixed by adding

 annotationProcessor "android.arch.persistence.room:compiler:$room_version" 

in ALL modules that include the code associated with the room .

+3
source share

This hail works great. Check out the kotlin-kapt plugin at the beginning. It is important.

 apply plugin: 'kotlin-kapt' apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { applicationId "????" minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'com.android.support:appcompat-v7:28.0.0' implementation 'com.android.support.constraint:constraint-layout:1.1.3' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' implementation 'android.arch.persistence.room:runtime:1.1.1' kapt 'android.arch.persistence.room:compiler:1.1.1' } 
+1
source share

I did, as everyone said in the answer above, still no luck.
The problem for my part was that I was inserting into the 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 to execute requests in the main thread. But keep in mind to remove it and implement RxJava, Live Data or any other background process engine for database queries. Hope this helps.

+1
source share

Add below plugin and dependency

apply plugin: 'kotlin-kapt'

kapt "android.arch.persistence.room:compiler:1.1.1"

0
source share
 You can see your database tables on DB browser for SQLITE. In android studio 1. Click on View -> Tool Windows -> Device File Explorer -> data 2. Select your project package name -> database -> select all file and save on desktop Install **DB browser for SQLITE** 1. sudo apt-get install sqlitebrowser //write on your terminal for install DB browser 2. install DB browser for sqlite 3. click on Open database and select file where you had saved your data 4. click on Brower Data and see your tables This is all where i have see my android room database 
0
source share

I will add a complete answer that solved my problem

First add all the room dependencies on Android X second add apply the plugin: 'kotlin-kapt' and replace the annotation of the room processor compiler with saved

0
source share

All Articles