KMP
- Kotlin course
- Expert
- Lesson 25/47
- 5 min
Learn the Kotlin Multiplatform (KMP) concept.
What is KMP?
Kotlin Multiplatform (KMP) lets you share the same Kotlin code across several platforms. It supports JVM, Android, iOS, JS and Native targets.
A KMP project starts with the kotlin("multiplatform") plugin in Gradle. Shared code goes into the commonMain source set; platform-specific code goes into each target's own source set. In this lesson we model the concept with a simple kmpInfo function.
KMP targets
KMP hosts many targets in the same project: JVM, Android, iOS (arm64, x64), JS and Native (Linux, Windows, macOS).
Each target is added with a separate Gradle block; for example jvm(), iosArm64(), js(). Targets have their own source sets: jvmMain, iosMain, jsMain. Shared code lives in commonMain and every target can use it. Platform-specific parts go into the matching target source set.
Practice Write kmpInfo
Define fun kmpInfo(): String. It should return "KMP". In main, write println(kmpInfo()).
Starter code
// add the kmpInfo function
fun main() {
// print the kmp info
}Sample solution
fun kmpInfo(): String = "KMP"
fun main() {
println(kmpInfo())
}Expected output
KMP
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which Gradle plugin starts a KMP project?
- application
- kotlin("jvm")
- kotlin("multiplatform") Correct answer
- java-library
Explanation: The kotlin("multiplatform") plugin starts a KMP project.
2 Complete the plugin line
plugins { kotlin("____") }
plugins { kotlin("multiplatform") }
Explanation: The kotlin("multiplatform") plugin starts KMP.
3 Match the KMP parts
| Term | Meaning |
|---|---|
kotlin("multiplatform") |
Gradle plugin |
commonMain |
Shared code |
iosArm64() |
iPhone target |
js() |
Browser target |