Step 1 of 7
Create a KMP project, understand source set structure, build configuration
เริ่มต้น KMP — สร้าง project, ทำความเข้าใจ commonMain vs platform source sets
Kotlin Multiplatform (KMP) lets you write business logic once in Kotlin and run it on Android, iOS, desktop, and more. You share the logic (networking, database, validation) and keep each platform's UI native.
The current stable version is Kotlin 2.4.x with the K2 compiler.
Do not use KMP when you have only one target. A plain Kotlin/JVM project is simpler.
MyApp.Run the app on each target from the IDE run configurations.
A KMP project has a shared module with source sets for each platform.
Diagram: A KMP shared module with common code and platform-specific code.
commonMainholds shared code;androidMainandiosMainhold platform code.
Loading diagram...
commonMain — code shared by all targets. No platform imports here.androidMain — Android-only code (uses android.* APIs).iosMain — iOS-only code (uses Apple frameworks via Kotlin/Native).The shared module's build.gradle.kts declares the targets and dependencies:
// shared/build.gradle.kts
kotlin {
androidTarget()
iosArm64()
iosX64()
iosSimulatorArm64()
sourceSets {
commonMain.dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.10.2")
}
}
}
Add shared dependencies inside commonMain.dependencies. They work on every target.
The wizard generates a sample Greeting. Run the Android and iOS configurations. Both print the same shared message.
Greeting: Hello, KMP!
shared/build.gradle.kts and find the kotlin { } block.commonMain that returns "KMP is ready" and call it from both apps.