Short answer: with Kotlin Multiplatform (KMP) you write your networking, business rules and data logic once in Kotlin and reuse it on Android and iOS, while keeping each platform's native UI. If you already write Android apps in Kotlin with Clean Architecture, you are most of the way there.
This guide shows what to share, how to structure the project, and the code for a shared data and domain layer using Ktor, Koin, Kotlin Serialization and Flow.
I'm Padmakar Garg, a Senior Android Developer. I built Ritam News, a news app, with Kotlin Multiplatform, MVVM, Clean Architecture, the Repository pattern, Koin and Coroutines with Flow. The lessons below come from that kind of project.
What KMP is (and is not)
KMP lets you share Kotlin code between platforms. It is not a UI framework that replaces native screens by default. You choose how much to share:
| Layer | Share it? | Notes |
|---|---|---|
| Models, DTOs, mappers | Yes | Pure Kotlin |
| Networking | Yes | Ktor client |
| Repository and use cases | Yes | Your business logic |
| Local database | Often | SQLDelight, or Room for KMP |
| ViewModel / presentation | Optional | Possible with the multiplatform lifecycle library |
| UI | Optional | Native screens, or Compose Multiplatform |
A good starting point is to share everything below the UI. You get the biggest win with the lowest risk.
1. Project structure
A typical setup has a shared module and a native app per platform:
project/
androidApp/ // Android UI (Jetpack Compose)
iosApp/ // iOS UI (SwiftUI)
shared/
src/commonMain/ // shared Kotlin: models, API, repository, use cases
src/androidMain/ // Android-specific implementations
src/iosMain/ // iOS-specific implementations
Code in commonMain can only use multiplatform libraries. Anything platform-specific goes in androidMain or iosMain.
2. Gradle setup
// shared/build.gradle.kts
plugins {
alias(libs.plugins.kotlinMultiplatform)
alias(libs.plugins.kotlinSerialization)
alias(libs.plugins.androidLibrary)
}
kotlin {
androidTarget()
iosX64()
iosArm64()
iosSimulatorArm64()
sourceSets {
commonMain.dependencies {
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.content.negotiation)
implementation(libs.ktor.serialization.kotlinx.json)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.koin.core)
}
androidMain.dependencies {
implementation(libs.ktor.client.okhttp)
}
iosMain.dependencies {
implementation(libs.ktor.client.darwin)
}
}
}
Use a version catalog (libs.versions.toml) and always check the latest compatible versions of Kotlin, Ktor and Koin, because KMP libraries are sensitive to version mismatches.
3. Models and mappers (commonMain)
Keep the network model separate from the domain model, exactly like in a normal Clean Architecture app.
@Serializable
data class ArticleDto(
val id: String,
val title: String,
val summary: String,
val imageUrl: String? = null
)
data class Article(
val id: String,
val title: String,
val summary: String,
val imageUrl: String?
)
fun ArticleDto.toDomain() = Article(id, title, summary, imageUrl)
4. Networking with Ktor
Ktor works in common code. It picks the right engine for each platform (OkHttp on Android, Darwin on iOS) from the dependencies you added.
class NewsApi(private val client: HttpClient) {
suspend fun topArticles(): List<ArticleDto> =
client.get("https://api.example.com/news/top").body()
}
5. Repository, state and use case
sealed interface Resource<out T> {
data object Loading : Resource<Nothing>
data class Success<T>(val data: T) : Resource<T>
data class Error(val message: String) : Resource<Nothing>
}
class NewsRepository(private val api: NewsApi) {
fun topArticles(): Flow<Resource<List<Article>>> = flow {
emit(Resource.Loading)
try {
emit(Resource.Success(api.topArticles().map { it.toDomain() }))
} catch (e: Exception) {
emit(Resource.Error(e.message ?: "Something went wrong"))
}
}
}
class GetTopArticles(private val repository: NewsRepository) {
operator fun invoke(): Flow<Resource<List<Article>>> = repository.topArticles()
}
This is the same Repository pattern you already use on Android. In KMP it simply lives in commonMain. In production, rethrow CancellationException instead of catching it, so coroutine cancellation still works.
6. Dependency injection with Koin
Koin supports multiplatform, so one module can wire the whole shared layer:
val sharedModule = module {
single {
HttpClient {
install(ContentNegotiation) {
json(Json { ignoreUnknownKeys = true })
}
}
}
single { NewsApi(get()) }
single { NewsRepository(get()) }
factory { GetTopArticles(get()) }
}
fun initKoin(extra: KoinApplication.() -> Unit = {}) {
startKoin {
modules(sharedModule)
extra()
}
}
Android calls initKoin from its Application class. iOS calls it once at startup through the generated framework.
7. Using the shared code on Android
On Android the ViewModel just uses the shared use case:
class NewsViewModel(getTopArticles: GetTopArticles) : ViewModel() {
val state: StateFlow<Resource<List<Article>>> =
getTopArticles().stateIn(
viewModelScope,
SharingStarted.WhileSubscribed(5_000),
Resource.Loading
)
}
The Compose screen collects state with collectAsStateWithLifecycle() and renders each case. Nothing about the shared layer is visible to the UI except plain Kotlin classes and Flow.
8. expect / actual for platform differences
When shared code needs something platform-specific, declare it with expect and implement it with actual:
// commonMain
expect fun platformName(): String
// androidMain
actual fun platformName(): String = "Android"
// iosMain
actual fun platformName(): String = "iOS"
Typical uses are database drivers, secure storage, file paths, logging and device information. Prefer interfaces plus dependency injection when you can, and use expect/actual only where it is the simplest option.
9. The iOS side
The shared module compiles to a native framework that Swift can import. Two things to know:
- Kotlin
Flowandsuspendfunctions are not as friendly in Swift by default. Libraries such as SKIE or KMP-NativeCoroutines make them much nicer to use. - Keep the API surface of
sharedsmall and simple. Expose use cases and plain data classes rather than internal types.
Common mistakes
- Sharing too much too early, including UI, before the team is comfortable with KMP.
- Mixing library versions, which causes confusing build errors. Keep Kotlin, Ktor, Koin and Gradle plugins aligned.
- Putting Android classes (
Context,Log) incommonMain. Hide them behind interfaces orexpect/actual. - Making the shared API too large or too generic, which is painful to call from Swift.
- Ignoring testing. Shared code is easy to unit test in
commonTest, so write the tests once.
When KMP is a good fit
KMP works well when you already have Kotlin expertise, you want native UI on each platform, and your app has non-trivial business logic or networking that you do not want to write twice. It is less attractive for tiny apps with almost no logic, or when you need one UI codebase for everything and are happy with a different framework.
Summary
- Start by sharing everything below the UI: models, networking, repository, use cases.
- Use Ktor for networking, Kotlin Serialization for JSON and Koin for dependency injection.
- Keep Clean Architecture boundaries. The pattern you use on Android works unchanged in
commonMain. - Use
expect/actualonly for real platform differences. - Make the iOS API small, and use a helper library for Flow in Swift.
If you work with Android security or crypto as well, I've published a Kotlin cryptography SDK here: github.com/gargpadmakar/MTProtoCrypto.
About the author: Padmakar Garg is a Senior Android Developer with 8+ years of experience in Kotlin, Jetpack Compose, Kotlin Multiplatform and mobile security. Portfolio and blog: gargpadmakar.github.io. Connect on LinkedIn.
Top comments (2)
The rethrow-CancellationException note in section 5 is the detail I'd steal — catching it silently is exactly how a cancelled refresh turns into a loading spinner that never clears. One question from your Ritam News build: did you end up sharing the ViewModel with the multiplatform lifecycle library, or keep ViewModels native and share only the use cases? I'm weighing that exact boundary for an offline-first app, and the iOS Flow friction you mention in section 9 makes me lean toward keeping the ViewModel native.
Thanks for reading! Kotlin Multiplatform is a great opportunity for Android developers to expand beyond Android while leveraging their existing Kotlin knowledge. I particularly like sharing business logic, networking, dependency injection, and domain layers while keeping platform-specific UI and integrations flexible. The goal is not just code sharing, but maintaining clean architecture, scalability, and a consistent experience across platforms.