Koin for Kotlin Multiplatform and Compose Multiplatform: Quick Start Guide
Quick start guide on how to add Koin dependency injection for KMP projects, specifically for those using Compose Multiplatform for the view layer.

This article will guide you through adding Koin dependency injection for Kotlin Multiplatform (KMP) projects, specifically for those using Compose Multiplatform (CMP) for the view layer.
Koin Dependencies
Adding Koin dependencies to your KMP and CMP project is quite easy as nowadays we have a BOM available. You can check latest version on Maven Central Repository.
Define versions and dependencies in your .toml
file
At the moment of writing this article, the latest stable version of the Koin BOM is 4.1.0
, but make sure to double check on the maven central repository page.
# your project > gradle > libs.versions.toml
[versions]
koin-bom = "4.1.0"
[libraries]
koin-bom = { module = "io.insert-koin:koin-bom", version.ref = "koin-bom" }
koin-core = { module = "io.insert-koin:koin-core" }
koin-compose = { module = "io.insert-koin:koin-compose" }
koin-compose-viewmodel = { module = "io.insert-koin:koin-compose-viewmodel" }
Add dependencies to your build.gradle
file
For KMP projects using purely Compose Multiplatform for the UI layer, you only need to add these dependencies into your common source set:
// your project > composeApp > build.gradle.kts
kotlin {
sourceSets {
commonMain.dependencies {
implementation(project.dependencies.platform(libs.koin.bom))
implementation(libs.koin.core)
implementation(libs.koin.compose)
implementation(libs.koin.compose.viewmodel)
}
}
}
Define a dependency injection module
To initialise Koin you need to define a dependency injection module. Create a common dependency injection module by simply creating a kotlin file in your commonMain
app module and using the Koin DSL:
import org.koin.core.module.dsl.viewModel
import org.koin.dsl.module
val commonModule = module {
// dependencies will go here
}
Your common dependencies will be added here and we will use this module in the next step for initialising Koin.
Koin Initialisation in KMP and CMP
Once again, initialisation is easier to do for Compose Multiplatform projects. Usually you would have to implement initialisation for each platform you target, but for CMP projects you only need to initialise in a single place.
Open the main App.kt
file for your KMP project and wrap your Compose app in the Koin initialisation:
import org.koin.compose.KoinApplication
@Composable
@Preview
fun App() {
KoinApplication(application = {
modules(commonModule)
}) {
AppTheme {
// your app composables, navigation host etc.
}
}
}
How to inject ViewModels using Koin
Assuming you have a composable screen such as this:
import androidx.compose.runtime.Composable
@Composable
internal fun YourComposableScreenRoot(
modifier: Modifier,
viewModel: YourComposableViewModel,
) {
...
}
And you've defined a viewModel
like so:
import androidx.lifecycle.ViewModel
class YourComposableViewModel : ViewModel() {
...
}
You can make the view model injectable by first defining how to inject it via the common module we've declared before:
import org.koin.core.module.dsl.viewModel
import org.koin.dsl.module
val commonModule = module {
viewModel { YourComposableViewModel() }
// or constructor DSL
viewModelOf(::YourComposableViewModel)
}
And then inject an instance using the dedicated Koin function when calling your composable screen in your navigation host or wherever else you are using it:
import org.koin.compose.viewmodel.koinViewModel
YourComposableScreenRoot(
modifier = Modifier.fillMaxSize(),
viewModel = koinViewModel<YourComposableViewModel>(),
)
This will work for CMP view models along all targeted platforms.