Senior
Have you worked with Kotlin Multiplatform?
sobes.tech AI
Answer from AI
Yes, I have worked.
I used Kotlin Multiplatform Mobile (KMM) to create shared business logic in iOS and Android applications.
Main advantages of KMM:
- Shared codebase for business logic.
- Ability to use native UI frameworks.
- Good interoperability with Java/Kotlin on Android and Swift/Objective-C on iOS.
- Gradual integration into existing projects.
Example of using KMM:
- Network layer (Ktor).
- Database (SQLDelight).
- Working with preferences (Multiplatform Settings).
- Business logic (ViewModels, Use Cases).
Structure of a typical KMM project:
shared: Shared module.androidApp: Android application depending onshared.iosApp: iOS application depending onshared.
Dependencies in KMM are described in the build.gradle.kts of the shared module using platform-specific blocks:
// build.gradle.kts shared
kotlin {
// ...
sourceSets {
val commonMain by getting {
dependencies {
// Common dependencies (Ktor, SQLDelight, etc.)
}
}
val androidMain by getting {
dependencies {
// Dependencies only for Android
}
}
val iosMain by getting // Empty for native iOS dependencies
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
}
}
To interact with native code, expect/actual mechanisms are used:
// commonMain:
expect class Platform() {
fun getPlatformName(): String
}
// androidMain:
actual class Platform actual constructor() {
actual fun getPlatformName(): String {
return "Android ${android.os.Build.VERSION.SDK_INT}"
}
}
// iosMain:
import platform.UIKit.UIDevice
actual class Platform actual constructor() {
actual fun getPlatformName(): String {
return UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion()
}
}
Building the iOS framework is done with the Gradle task syncFramework:
./gradlew syncFramework
The framework is then integrated into the Xcode project.
I like the KMM approach because it allows code reuse without compromising native UX.