10. Implementing DI in KMP with Koin
Jun 23, 2024
--
To integrate Koin for Dependency Injection in a Kotlin Multiplatform Project (KMP), you need to add the core dependency to the commonMain
source set in your shared module's build.gradle.kts
file. Here’s the setup:
…/shared/build.gradle.kts
kotlin {
...
sourceSets {
commonMain.dependencies {
...
// koin dependencies
implementation(project.dependencies.platform(libs.koin.bom))
implementation(libs.koin.core)
}
}
}
Why Only koin.core
?
- Platform Independence:
koin.core
works across all platforms, making it perfect for thecommonMain
source set. - Essential Features: It provides all necessary DI features like module definitions, singletons, and factories.
- Lean Shared Codebase: Keeping the shared codebase lean avoids unnecessary platform-specific dependencies.
By adding only koin.core
, you ensure a robust and platform-independent DI setup for your shared code in KMP.