Android room persistent: AppDatabase_Impl does not exist

前端 未结 21 1394
日久生厌
日久生厌 2020-11-29 01:09

My app database class

@Database(entities = {Detail.class}, version = Constant.DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {

            


        
相关标签:
21条回答
  • 2020-11-29 01:23

    For me, the Android Studio automatically updated dependencies as soon as you include any of the Room database related imports. But as per https://developer.android.com/jetpack/androidx/releases/room#declaring_dependencies you need to update few. Here is how my code-base looks like:

    AppDatabase.kt

    import android.content.Context
    import androidx.room.Database
    import androidx.room.Room
    import androidx.room.RoomDatabase
    
    @Database(entities = arrayOf(MyEntity::class), version = 1)
    abstract class AppDatabase : RoomDatabase() {
        abstract fun myDAO(): MyDAO
    
        companion object {
            @Volatile private var instance: AppDatabase? = null
            private val LOCK = Any()
    
            operator fun invoke(context: Context)= instance ?: synchronized(LOCK){
                instance ?: buildDatabase(context).also { instance = it}
            }
    
            private fun buildDatabase(context: Context) = Room.databaseBuilder(context,
                AppDatabase::class.java, "db-name.db")
                .build()
        }
    }
    

    Update the build.gradle as specified in one of the answers:

    apply plugin: 'kotlin-kapt' // this goes with other declared plugin at top
    dependencies { // add/update the following in dependencies section
        implementation 'androidx.room:room-runtime:2.2.3'
    //    annotationProcessor 'androidx.room:room-compiler:2.2.3' // remove this and use the following
        kapt "androidx.room:room-compiler:2.2.3"
    
    }
    

    Sync the gradle and you should be good to go.

    0 讨论(0)
  • 2020-11-29 01:26

    I meet with the problem, because I forget @Dao annotation

    @Dao
    public interface SearchHistoryDao {
        @Query("SELECT * FROM search_history")
        List<SearchHistory> getAll();
    
        @Insert
        void insertAll(SearchHistory... histories);
    
        @Delete()
        void delete(SearchHistory history);
    }
    

    Room Official tutorial

    0 讨论(0)
  • 2020-11-29 01:26

    Reading the example here: Room Example

    I fixed this error just using the correct (I guess it is) annotationProcessorFile, as follows:

    annotationProcessor "android.arch.persistence.room:compiler:<latest_version>"
    

    Also, I upgraded to 2.2.0 either in Room Version as in Lifecycle version.

    Once synchronized the graddle, I could start working with Room.

    So, Good luck! And let the code be with you!

    0 讨论(0)
  • 2020-11-29 01:28

    In my kotlin app, I just added the following line at the top of my build.gradle file :

    apply plugin: 'kotlin-kapt'
    

    And the following line in the dependencies section:

    kapt "androidx.room:room-compiler:2.2.5"
    

    I hope it fixes your issue.

    0 讨论(0)
  • 2020-11-29 01:29

    For those working with Kotlin, try changing annotationProcessor to kapt in the apps build.gradle

    for example:

    // Extensions = ViewModel + LiveData
    implementation "android.arch.lifecycle:extensions:1.1.0"
    kapt "android.arch.lifecycle:compiler:1.1.0"
    // Room
    implementation "android.arch.persistence.room:runtime:1.0.0"
    kapt "android.arch.persistence.room:compiler:1.0.0"
    

    also remember to add this plugin

    apply plugin: 'kotlin-kapt'
    

    to the top of the app level build.gradle file and do a clean and rebuild (according to https://codelabs.developers.google.com/codelabs/android-room-with-a-view/#6)

    In Android Studio, if you get errors when you paste code or during the build process, select Build >Clean Project. Then select Build > Rebuild Project, and then build again.


    UPDATE

    If you have migrated to androidx

    def room_version = "2.2.3" // check latest version from docs
    
    implementation "androidx.room:room-runtime:$room_version"
    kapt "androidx.room:room-compiler:$room_version"
    
    0 讨论(0)
  • 2020-11-29 01:31

    I had this error when I missed

    @Database(entity="{<model.class>})
    

    Ensure that the entity model specified in the annotation above refers to the particular model class and also ensure that the necessary annotation:

    @Entity(tableName = "<table_name>" ...)
    

    is properly defined and you'd be good

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