6. Implementing KMP in Android App
--
Implementing Kotlin Multiplatform (KMP) can be a complex process, but it significantly streamlines the development of shared code across different platforms. Follow the steps below to integrate KMP into your Android project.
Create a new module named shared:
Click “Finish” and sync the project.
On the first sync, you may encounter several issues. Let’s address them one by one.
Issue 1: Unresolved Reference Errors
You might see errors like:
* What went wrong:
Script compilation errors:
Line 2: alias(libs.plugins.kotlinMultiplatform)
^ Unresolved reference: kotlinMultiplatform
Line 3: alias(libs.plugins.androidLibrary)
^ Unresolved reference: androidLibrary
TO RESOLVE:
To resolve this, add the necessary dependencies:
…/gradle/libs.versions.toml
[versions]
agp = "8.4.1" // this stays unchanged
kotlin = "1.9.0" // this stays unchanged
[plugins]
...
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
And update your …/build.gradle.kts:
plugins {
...
alias(libs.plugins.androidLibrary) apply false
alias(libs.plugins.kotlinMultiplatform) apply false
}
After syncing, you may face the next issue.
Issue 2: Receiver Type Mismatch Errors
* What went wrong:
Script compilation errors:
Line 27: commonMain.dependencies {
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun Project.dependencies(configuration: DependencyHandlerScope.() -> Unit): Unit defined in org.gradle.kotlin.dsl
Line 30: commonTest.dependencies {
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public fun Project.dependencies(configuration: DependencyHandlerScope.() -> Unit): Unit defined in org.gradle.kotlin.dsl
Line 31: implementation(libs.kotlin.test)
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val Project.kotlin: KotlinMultiplatformExtension defined in org.gradle.kotlin.dsl
3 errors
TO RESOLVE:
To resolve this, increase the Kotlin version to 2.0.0 in …/gradle/libs.versions.toml:
[versions]
...
kotlin = "2.0.0"
Sync the project again.
Issue 3: Missing kotlin-test
Dependency
* What went wrong:
Script compilation error:
Line 31: implementation(libs.kotlin.test)
^ Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:
public val Project.kotlin: KotlinMultiplatformExtension defined in org.gradle.kotlin.dsl
1 error
TO RESOLVE:
Add the kotlin-test
dependency in …/gradle/libs.versions.toml:
[versions]
...
kotlin = "2.0.0" // unchanged
[libraries]
...
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
Sync the project again. It would success, but build would fail.
Issue 4: KSP Update Required
* What went wrong:
Execution failed for task ':androidApp:kspDebugKotlin'.
> 'org.jetbrains.kotlin.incremental.ChangedFiles com.google.devtools.ksp.gradle.KspTaskJvm.getChangedFiles(org.gradle.work.InputChanges, java.util.List)'
TO RESOLVE:
Update the KSP version in …/gradle/libs.versions.toml:
[versions]
...
ksp = "2.0.0-1.0.21"
Sync the project again.
Issue 5: Compose Compiler Compatibility
You might encounter a compatibility error with the Compose Compiler:
This version (1.5.1) of the Compose Compiler requires Kotlin version 1.9.0
but you appear to be using Kotlin version 2.0.0 which is not known to be
compatible. Please consult the Compose-Kotlin compatibility map located
at https://developer.android.com/jetpack/androidx/releases/compose-kotlin
to choose a compatible version pair
(or `suppressKotlinVersionCompatibilityCheck` but don't say I didn't warn you!).
REASON:
This is because in …/androidApp/build.gradle.kts , you have:
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
According to the compatibility map, Kotlin 2.0.0 and above require the Compose Compiler Gradle plugin. Detailed information can be found here.
Additional info:
TO RESOLVE:
To resolve this, add the Compose Compiler plugin in …/gradle/libs.versions.toml
[plugins]
...
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }
And update your …/build.gradle.kts
plugins {
...
alias(libs.plugins.compose.compiler) apply false
}
Also, update your …/androidApp/build.gradle.kts
plugins {
...
alias(libs.plugins.compose.compiler)
}
You can remove the composeOptions
block:
…/androidApp/build.gradle.kts
// Remove this block
composeOptions {
kotlinCompilerExtensionVersion = "1.5.1"
}
Sync and run your app. It should build successfully.
Add to …/.gitignore generated files:
.kotlin/
shared/build/
Congratulations! You have successfully implemented Kotlin Multiplatform in your Android app, but it’s still not connected with androidApp module.