KSP error when adding Room to Kotlin Multiplatform project

I'm trying to add Room to Android part of my Kotlin Multiplatform project (targeting Android and iOS). I'd like to achieve that using KSP (as opposed to the outdated KAPT).
For some time the ksp
configuration could not be resolved, but that went away when I updated Kotlin and aligned the KSP version with Kotlin version.
However, Gradle sync is failing on the line where I add Room compiler:
implementation(libs.room.runtime)
implementation(libs.room.ktx)
ksp(libs.room.compiler) // <-- FAILING HERE
It fails with the following error:
Type mismatch: inferred type is Provider<MinimalExternalModuleDependency!>! but Action<KspExtension> was expected
I've added the following version definitions:
[versions]
androidx-room = "2.7.1"
kotlin = "2.1.21"
ksp = "2.1.21-2.0.1"
[libraries]
room-runtime = { group = "androidx.room", name = "room-runtime", version.ref = "androidx-room" }
room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "androidx-room" }
room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "androidx-room" }
[plugins]
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
I added KSP plugin to both root and application build files.
Answer
There are a few things to be aware of:
KSP and Kotlin versions must match
In this case:kotlin = "2.1.21" ksp = "2.1.21-2.0.1"
KSP must be added in both root-level and module-level build file
In root-levelbuild.gradle.kts
:plugins { alias(libs.plugins.ksp) apply false }
In module-level
build.gradle.kts
:plugins { alias(libs.plugins.ksp) }
Room library must be added on in Android source source set (not in common)
In module-level build file:kotlin { sourceSets { androidMain.dependencies { implementation(libs.room.runtime) implementation(libs.room.ktx) } } }
Room compiler must be added in
android.dependencies
, not inkotlin.sourceSets.androidMain.dependencies
In module-level build file:android { dependencies { ksp(libs.room.compiler) } }
Enjoyed this question?
Check out more content on our blog or follow us on social media.
Browse more questions