PPrepLearn
Progress Β· 0/10 sections

🌐 🌐 Kotlin Multiplatform (KMP)

2 min read Β· Notion


What is Kotlin Multiplatform?

KMP allows sharing Kotlin code across platforms: Android, iOS, Web, Desktop, and Server β€” without forcing a shared UI layer.

  • Shared module: Pure Kotlin, no platform-specific APIs.
  • Android/iOS targets: Platform-specific code in androidMain / iosMain.

Different from React Native/Flutter: KMP shares logic, not UI (unless using Compose Multiplatform for UI too).

expect / actual mechanism

expect declares a contract in shared code; actual provides the platform-specific implementation:

// commonMain
expect fun getCurrentTimestamp(): Long
 
// androidMain
actual fun getCurrentTimestamp(): Long = System.currentTimeMillis()
 
// iosMain
actual fun getCurrentTimestamp(): Long =
    NSDate().timeIntervalSince1970.toLong() * 1000
KMP limitations
  • No direct access to platform APIs from shared code (must use expect/actual).
  • Compose Multiplatform for iOS is still maturing.
  • Gradle setup can be complex.
  • Some Kotlin coroutine patterns differ between Android and iOS (iOS requires @SharedImmutable for shared state in older setups).
  • Swift/ObjC interop has some rough edges (generics, coroutines).
What can be shared in KMP?
  • Business logic / UseCases
  • Data models / DTOs
  • Repository interfaces and implementations (with Ktor for networking, SQLDelight for DB)
  • ViewModels (with KMP-ViewModel library)
  • Utility functions (date parsing, validation, formatting)

Related