kotlin-multiplatform-samples
kotlin-multiplatform-samples copied to clipboard
AppDatabase::class.instantiateImpl() not found - unresolved reference
Compiling with Android Studio Koala 2024.1.1 give this error
file:///Users/admin/Sviluppo/AppNativa/kotlin-multiplatform-samples/Fruitties/shared/src/nativeMain/kotlin/com/example/fruitties/di/Factory.native.kt:38:24 Type mismatch: inferred type is Unit but AppDatabase was expected
This issue is similar to this question on stack overflow https://stackoverflow.com/questions/78627516/room-with-kmm-unresolved-reference-instantiateimpl
In your gradle.properties, add theses lines :
kotlin.apple.deprecated.allowUsingEmbedAndSignWithCocoaPodsDependencies=true
kotlin.mpp.androidGradlePluginCompatibility.nowarn=true
kotlin.native.disableCompilerDaemon = true
In shared/build.gradle.kts :
iosMain {
// Fixes RoomDB Unresolved reference 'instantiateImpl' in iosMain
kotlin.srcDir("build/generated/ksp/metadata")
}
//And :
dependencies {
add("kspCommonMainMetadata", libs.androidx.room.compiler)
}
tasks.withType<org.jetbrains.kotlin.gradle.dsl.KotlinCompile<*>>().configureEach {
if (name != "kspCommonMainKotlinMetadata" ) {
dependsOn("kspCommonMainKotlinMetadata")
}
}
It fix the problem for me :
I fix the problem in this commit : https://github.com/ThomasBernard03/Tarot/commit/9e125e538c148c2055587ebeee6bd2d4916a1b19
This should be fixed in https://github.com/android/kotlin-multiplatform-samples/pull/28.
- Updated to Kotlin 2.0.10
- Room 2.7.0-alpha06
- https://developer.android.com/jetpack/androidx/releases/room#2.7.0-alpha06
- https://developer.android.com/kotlin/multiplatform/room#defining-database
"Due to Kotlin 2.0 compilation model, the strategy of referencing a to-be-generated function, named instantiateImpl() is longer viable. Two new APIs, @ConstructedBy and RoomDatabaseConstructor are introduced that replace the instantiateImpl() strategy."