What is Kotlin Multiplatform?
Kotlin Multiplatform (KMP) is JetBrains’ answer to code reusability across platforms. With KMP, you can write your business logic once and share it across Android, iOS, desktop, and web applications.
No more rewriting the same data layer in Swift and Kotlin. Just write it once and focus on building great features!
Why Use Kotlin Multiplatform?
- Code sharing: Share logic across platforms (networking, caching, data models).
- Native UIs: Build platform-specific UI while reusing backend code.
- Faster development: Less duplicated logic = fewer bugs = faster releases.
Setting Up a KMP Project in Android Studio
Let’s create a simple Kotlin Multiplatform project that targets Android and iOS.
✅ Prerequisites
- Android Studio Hedgehog or newer
- Kotlin Plugin 1.9.0+
- macOS with Xcode installed (for iOS support)
- Gradle 8.0+
Step-by-Step Project Setup
Step 1: Create a New Project
- Open Android Studio → New Project → KMM Application.
- Select targets: Android and iOS (CocoaPods).
- Name your project and finish.
Step 2: Project Structure
You’ll see something like:
graphqlCopyEdit|-- shared/ # Shared Kotlin code
|-- androidApp/ # Native Android app
|-- iosApp/ # Native iOS app (Xcode)
Step 3: shared/build.gradle.kts
Your shared module is where the magic happens. You’ll see dependencies like this:
kotlinCopyEditkotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64()
sourceSets {
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:2.0.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.5.0")
}
}
val androidMain by getting
val iosMain by creating {
dependsOn(commonMain)
}
}
}
Step 4: Add Your Shared Code
In shared/src/commonMain/kotlin
, create something simple:
kotlinCopyEditclass Greeting {
fun greet(): String {
return "Hello from KMP! 👋"
}
}
Call this from both Android and iOS apps.
🤖 Android Integration
Use the shared module like any Kotlin library:
kotlinCopyEditval greeting = Greeting().greet()
textView.text = greeting
🍏 iOS Integration (via Xcode)
- Open the
iosApp
folder in Xcode. - Use the shared framework (generated via CocoaPods).
- Call:
swiftCopyEditlet greeting = Greeting().greet()
label.text = greeting
🚀 Build & Run
- On Android: Just run as usual in Android Studio.
- On iOS: Run from Xcode with a real/simulated device.
💡 What’s Next?
- Add networking with Ktor
- Use SQLDelight for cross-platform database
- Share more modules: authentication, API services, etc.
- Explore Jetpack Compose Multiplatform for unified UI (coming soon!)
🧠 Final Thoughts
Kotlin Multiplatform isn’t just hype—it’s the future of cross-platform development with native performance and code reuse. If you’re tired of duplicating your logic for Android and iOS, KMP is definitely worth exploring.
Like this post?
⭐ Bookmark, 🔁 Share, and 🧑💻 Subscribe to Programming Keeda for more Kotlin tutorials, dev hacks, and project ideas.