Room cannot find implementation

前端 未结 11 1793
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 07:17

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         


        
相关标签:
11条回答
  • 2020-12-09 08:02

    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.

    0 讨论(0)
  • 2020-12-09 08:05

    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

    0 讨论(0)
  • 2020-12-09 08:07

    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

    0 讨论(0)
  • 2020-12-09 08:07

    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.

    0 讨论(0)
  • 2020-12-09 08:11

    Add below plugin and dependency

    apply plugin: 'kotlin-kapt'

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

    0 讨论(0)
提交回复
热议问题