What are the main differences in general code when developing with Kotlin Multiplatform?
sobes.tech AI
Answer from AI
When developing with Kotlin Multiplatform, the shared code has several differences from regular Kotlin code:
-
Modularity and platform separation: shared code is written in special modules (commonMain), while platform-dependent code is in separate modules (androidMain, iosMain, etc.).
-
Limited API: platform-specific libraries and APIs cannot be used directly in shared code. Instead, expect/actual mechanisms are used to declare interfaces and their implementations on specific platforms.
-
Use of expect/actual: allows declaring expected functions or classes in shared code (expect), and implementing them separately for each platform (actual).
-
Stricter restrictions on libraries: only multiplatform libraries or standard Kotlin libraries supporting MPP can be used in shared code.
Example of expect/actual usage:
// commonMain
expect fun getPlatformName(): String
// androidMain
actual fun getPlatformName() = "Android"
// iosMain
actual fun getPlatformName() = "iOS"
Thus, Kotlin Multiplatform shared code is aimed at maximum reusability but requires abstraction from platform-specific details.