My app database class
@Database(entities = {Detail.class}, version = Constant.DATABASE_VERSION)
public abstract class AppDatabase extends RoomDatabase {
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.
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
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!
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.
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.
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"
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