If you’ve ever built the same app twice — once for Android and once for iOS — you know how painful and repetitive it can be. But what if you could write your core logic once and use it on both platforms?
Welcome to Kotlin Multiplatform Mobile (KMM) — a game-changer for mobile developers who want to work smarter, not harder.
In this guide, I’ll walk you through how to build cross-platform iOS and Android apps using Kotlin Multiplatform Mobile. Whether you’re a solo dev or part of a team, this tool can drastically simplify your workflow without sacrificing native performance.
What is Kotlin Multiplatform Mobile?
Kotlin Multiplatform Mobile, or KMM, is a feature of the Kotlin language that lets you share code across Android and iOS apps. Think of it like this:
- Your business logic, such as API calls, data handling, and utilities, goes into a shared module.
- Your UI and platform-specific stuff stays native (Swift for iOS, Kotlin for Android).
So instead of writing everything twice, you write your core logic once and reuse it. The result? Less duplicated code, fewer bugs, and faster development.
What You Need to Get Started
Before jumping into code, make sure you have the right tools installed.
Prerequisites
- Android Studio (latest version)
- Kotlin Multiplatform Plugin (Install via Android Studio Plugins)
- Xcode (for iOS development — macOS only)
- KDoctor (to verify setup; install using
brew install kdoctor
)
Run this command to check if everything’s good:
kdoctor
It will let you know if anything is missing.
Setting Up a New KMM Project
Let’s get our hands dirty and create a KMM project from scratch.
1. Open Android Studio
Choose “New Project” → Select “Kotlin Multiplatform App”.
2. Configure Your Project
- Project Name:
MyKMMApp
- Package:
com.softaai.mykmmapp
- Platforms: Check both Android and iOS
- UI Sharing: Select “Do not share UI” (We’ll use native UI)
3. Finish
Click Finish. Android Studio will create the project with:
shared/
→ your shared Kotlin codeandroidApp/
→ Android-specific codeiosApp/
→ iOS-specific Swift code
Writing Shared Logic with Kotlin
Let’s write a basic example to see how shared code works.
Create a Kotlin Class
// shared/src/commonMain/kotlin/com/softaai/shared/Greeting.kt
package com.softaai.shared
class Greeting {
fun greet(): String {
return "Hello from Kotlin Multiplatform!"
}
}
This simple class is in the commonMain
source set, which means it’s shared between iOS and Android.
Using Platform-Specific Code
Sometimes, you need to run different code depending on the platform. That’s where expect
and actual
come in.
Shared Declaration
// shared/src/commonMain/kotlin/com/softaai/shared/Platform.kt
package com.softaai.shared
expect fun getPlatformName(): String
Android Implementation
// shared/src/androidMain/kotlin/com/softaai/shared/Platform.kt
package com.softaai.shared
actual fun getPlatformName(): String = "Android"
iOS Implementation
// shared/src/iosMain/kotlin/com/softaai/shared/Platform.kt
package com.softaai.shared
actual fun getPlatformName(): String = "iOS"
Now when you call getPlatformName()
, it runs the correct version based on the platform.
Using Shared Code in Android
Open MainActivity.kt
and call your shared logic.
// androidApp/src/main/java/com/softaai/androidApp/MainActivity.kt
package com.softaai.androidApp
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.softaai.shared.Greeting
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val message = Greeting().greet()
println(message) // Should print: Hello from Kotlin Multiplatform!
}
}
Using Shared Code in iOS (Swift)
In your iosApp
, open ViewController.swift
:
// iosApp/iosApp/ViewController.swift
import UIKit
import shared
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let greeting = Greeting().greet()
print(greeting) // Prints: Hello from Kotlin Multiplatform!
}
}
Make sure your iOS app links to the shared Kotlin framework properly.
Running Your App
Android
- In Android Studio, choose the
androidApp
module - Click Run
iOS
- Open
iosApp
in Xcode - Choose a simulator
- Click Run
And just like that, you’ve got both apps pulling logic from the same Kotlin codebase.
Why Kotlin Multiplatform Mobile?
Still wondering if KMM is right for you? Here’s why it’s gaining traction:
- True Native Performance: Shared code compiles to native binaries.
- Maximum Code Reuse: Write once, run on both platforms.
- Freedom of UI: Keep the native look and feel with platform-specific UIs.
- Tooling Support: First-class support in Android Studio and Xcode.
Bonus Resources
Conclusion
Learning how to build cross-platform iOS and Android apps using Kotlin Multiplatform Mobile might feel like a shift at first — but once you get the hang of it, you’ll wonder how you ever coded without it.
By sharing logic between your apps, you’re saving time, reducing bugs, and simplifying maintenance — all without compromising on native performance.
So fire up Android Studio, spin up Xcode, and start building smarter, cleaner, and faster apps with KMM.