Kotlin multiplatform

From G1Joshi/Agent-Skills · View interactive version

Kotlin multiplatform is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Kotlin Multiplatform (KMP) assistance covering expect/actual declarations, shared business logic across iOS/Android, Compose Multiplatform, and Ktor client integration. Use when sharing domain models, networking, or persistence across platforms while retaining native UI.

How to use this skill

Skill file

Kotlin Multiplatform (KMP)

Kotlin Multiplatform (KMP) allows you to share code between Android, iOS, Web, and Desktop. It emphasizes sharing logic (Business, Data, Networking) while allowing for native or shared (Compose Multiplatform) UIs.

When to Use

Quick Start

// commonMain/kotlin/Platform.kt
interface Platform {
    val name: String
}
expect fun getPlatform(): Platform

// androidMain/kotlin/Platform.kt
actual fun getPlatform(): Platform = object : Platform {
    override val name = "Android ${android.os.Build.VERSION.SDK_INT}"
}

// iosMain/kotlin/Platform.kt
import platform.UIKit.UIDevice
actual fun getPlatform(): Platform = object : Platform {
    override val name = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
}

// commonMain/kotlin/Greeting.kt
class Greeting {
    fun greet(): String = "Hello, ${getPlatform().name}!"
}

Core Concepts

Source Sets Hierarchy (commonMain vs Platform Sets)

Shared code lives in commonMain; platform-specific source sets bridge native OS features:

shared/src/
  ├── commonMain/kotlin/     # Shared business logic, Ktor, SQLDelight
  ├── androidMain/kotlin/    # Android-specific APIs & Context
  └── iosMain/kotlin/        # iOS Objective-C / Swift interop

`expect` / `actual` Platform Declarations

Defines a common contract that must be implemented by each target platform:

// commonMain: Contract declaration
expect class PlatformSecureStorage() {
    fun store(key: String, value: String)
    fun retrieve(key: String): String?
}

// iosMain: Actual implementation using iOS Keychain
actual class PlatformSecureStorage actual constructor() {
    actual fun store(key: String, value: String) { /* iOS SecItemAdd */ }
    actual fun retrieve(key: String): String? { /* iOS SecItemCopyMatching */ return null }
}

Shared Persistence with SQLDelight / Room KMP

Compiles SQL queries into type-safe Kotlin data classes shared across platforms:

// commonMain: Shared Database driver setup
class DatabaseDriverFactory(private val driver: SqlDriver) {
    fun createDatabase(): AppDatabase = AppDatabase(driver)
}
// Android uses AndroidSqliteDriver; iOS uses NativeSqliteDriver

Common Patterns

Shared Ktor Client Across iOS and Android

Problem: Duplicating HTTP request logic and deserialization schemas across platforms.

Solution: Define shared Ktor HttpClient in commonMain.

// commonMain/src/ApiClient.kt
class ApiClient(engine: HttpClientEngine) {
    private val client = HttpClient(engine) {
        install(ContentNegotiation) { json(Json { ignoreUnknownKeys = true }) }
    }

    suspend fun getItems(): List<Item> = client.get("https://api.example.com/items").body()
}

// androidMain: ApiClient(OkHttp.create())
// iosMain: ApiClient(Darwin.create())

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :----------------------------------- | :------------------------------------------------------------------- | :----------------------------------------------------------- |

| Unresolved reference in commonMain | Using platform-specific API (java.util, android.\*). | Use KMP-compatible libraries (kotlinx-datetime). |

| C-interop build errors | iOS build configuration or missing headers. | Check cocoapods or framework config in build.gradle.kts. |

| Memory Leaks on iOS | Circular references or freezing (mostly solved in new memory model). | Ensure newer Kotlin version (1.9+) is used. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.