Summary - KMP
- Kotlin course
- Expert
- Lesson 30/47
- 5 min
Combine commonMain, expect/actual and platform targets to write a KMP library.
Summary - KMP
In this group you learned KMP, commonMain, expect/actual, the iOS and JS targets. Now you will write a model that combines them: a shared info function.
Your kmpSummary function represents the commonMain logic; it returns the string "KMP: common + expect". In main you will call this function and print the result. This is the typical KMP combination: shared code, the expect/actual bridge and platform targets in one flow.
Combining: commonMain + expect/actual + targets
Now we combine all parts. commonMain holds the shared logic; expect/actual builds the platform bridge; the iOS and JS targets produce the output.
Example: fun kmpSummary(): String = "KMP: common + expect". This function lives in commonMain; every target can call it. In main you write println(kmpSummary()). This simple model summarises the shared logic + platform bridge flow of KMP.
Practice Write kmpSummary
Define fun kmpSummary(): String. It should return "KMP: common + expect". In main, write println(kmpSummary()).
Starter code
// add the kmpSummary function
fun main() {
// print the KMP summary
}Sample solution
fun kmpSummary(): String = "KMP: common + expect"
fun main() {
println(kmpSummary())
}Expected output
KMP: common + expect
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the core structure of KMP?
KMP is built from shared and target sets.- One platform
- One source set
- Only iOS
- commonMain + platform sets Correct answer
Explanation: KMP consists of commonMain and platform source sets.
2 Complete the kmpSummary string
fun kmpSummary(): String = "____"
fun kmpSummary(): String = "KMP: common + expect"
Explanation: This string summarises the KMP combination.
3 Match the KMP concepts
| Term | Meaning |
|---|---|
commonMain |
Shared code |
expect/actual |
Platform bridge |
iosArm64() |
iPhone target |
js() |
JS target |