Reputation: 61
I'm making an app with Kotlin Multiplatform. I encountered an error when implementing the Room database, using this tutorial.
The error goes like this (Note that this error appears while debugging on both iOS and Android):
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myApp/com.example.myApp.MainActivity}: java.lang.RuntimeException: Cannot find implementation for model.database.AppDatabase. AppDatabase_Impl does not exist. Is Room annotation processor correctly configured?
I tried solving it with this Stackoverflow answer by following this recommendation:
This is a workaround, but you can build it with the following code.
https://github.com/cvivek07/KMM-PicSplash/blob/main/composeApp/build.gradle.kts https://github.com/cvivek07/KMM-PicSplash/blob/main/composeApp/src/commonMain/kotlin/database/AppDatabase.kt
I implemented these lines:
In Gradle build (I think this is the only important workaround, am I right?)
dependencies {
//Replace it with -> ksp(libs.room.compiler) when it is stable
add("kspCommonMainMetadata", libs.room.compiler)
}
tasks.withType<KotlinJvmCompile>().configureEach {
if (name != "kspCommonMainKotlinMetadata" ) {
dependsOn("kspCommonMainKotlinMetadata")
}
}
And in the Database class:
abstract class AppDatabase: RoomDatabase() ,DB {
abstract fun ExampleDao(): ExampleDao
override fun clearAllTables() {
super.clearAllTables()
}
}
// FIXME: Added a hack to resolve below issue:
// Class 'AppDatabase_Impl' is not abstract and does not implement abstract base class member 'clearAllTables'.
interface DB {
fun clearAllTables() {}
}
But the error persists.
Upvotes: 6
Views: 582
Reputation: 69
I was getting a similar error when running an example for JVM. The error is gone after adding "kspJvm," as shown below.
dependencies {
listOf(
"kspAndroid",
"kspJvm",
"kspIosSimulatorArm64",
"kspIosX64",
"kspIosArm64",
"kspCommonMainMetadata",
).forEach {
add(it, libs.androidx.room.compiler)
}
}
Upvotes: 2