I wanted to use Room for storing data. For this I have my AppDatabase.kt
:
@Database(entities = [SensorEntry::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
abstract fun sensorDao(): SensorDao
companion object {
// For Singleton instantiation
@Volatile private var instance: AppDatabase? = null
fun getInstance(context: Context): AppDatabase {
return instance ?: synchronized(this) {
instance ?: buildDatabase(context).also { instance = it }
}
}
private fun buildDatabase(context: Context): AppDatabase {
return Room.databaseBuilder(
context,
AppDatabase::class.java, "SensorEntry.db"
)
.addTypeConverter(Converters::class)
.addCallback(
object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
ioThread { getInstance(context).sensorDao() }
}
}
)
.build()
}
}
}
using the following dependecies (from [developer.android.com])(https://developer.android.com/training/data-storage/room):
build.gradle
dependencies {
def room_version = "2.4.2"
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// optional - RxJava2 support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - RxJava3 support for Room
implementation "androidx.room:room-rxjava3:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// optional - Test helpers
testImplementation "androidx.room:room-testing:$room_version"
// optional - Paging 3 Integration
implementation "androidx.room:room-paging:2.5.0-alpha01"
}
The database is instanciated in my MainActivity
val db = AppDatabase.getInstance(applicationContext)
I can compile the application but I get the error during runtime which is discussed here: Android room persistent: AppDatabase_Impl does not exist
Caused by: java.lang.RuntimeException: cannot find implementation for AppDatabase. AppDatabase_Impl does not exist
No this thread suggest to replace the annotationProcessor
in my gradle.build
with kapt
.
This seemed to do the trick, but then I get another error during comiling for which I've already openend a question Using kotlin.UByte for Room Entity not working
C:<...>\UInt8.java:15: error: Cannot find getter for field.
private byte value;
Cannot find getter for field.
Update: I have purposely not included any code of object that should be stored. Please see my other question if you think, this might be the root of the problem.