# Kotlin Multiplatform — Kotlin

Source: https://www.skillbyai.com/en/kotlin/e-multiplatform

> Share logic across Android, iOS, desktop, web and server.

## Shared code with platform-specific edges

**Kotlin Multiplatform (KMP)** lets you write common code once, typically business logic, networking, data and validation, and compile it for the JVM, Android, iOS (via Kotlin/Native), desktop and web. Code lives in source sets such as `commonMain`, `androidMain` and `iosMain`. When common code needs a platform API it declares an `expect` function or class and each platform provides an `actual` implementation. Multiplatform libraries such as kotlinx.coroutines, kotlinx.serialization, Ktor client and SQLDelight work in common code. **Compose Multiplatform** from JetBrains extends Compose UI to other platforms. Support levels differ per target, so check the docs for your version.

## expect and actual

Common declaration, platform implementations.

```kotlin
// commonMain
expect fun platformName(): String

class Greeter {
    fun greet(): String = "Hello from ${platformName()}"
}

// androidMain
actual fun platformName(): String = "Android ${android.os.Build.VERSION.SDK_INT}"

// iosMain
actual fun platformName(): String =
    platform.UIKit.UIDevice.currentDevice.systemName()
```

## Share logic first, UI later

A common adoption path is sharing domain and data code while keeping native UIs, then deciding later whether shared UI with Compose Multiplatform is worth it.

**Quiz:** How does common KMP code access a platform-specific API?

- [x] It declares an `expect` declaration with `actual` implementations per platform
- [ ] It uses reflection at runtime
- [ ] It cannot; common code is pure math only
- [ ] It calls Java directly on every platform

*Answer:* It declares an `expect` declaration with `actual` implementations per platform. expect/actual connects common code to platform code.
