10. Implementing DI in KMP with Koin

--

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?

  1. Platform Independence: koin.core works across all platforms, making it perfect for the commonMain source set.
  2. Essential Features: It provides all necessary DI features like module definitions, singletons, and factories.
  3. 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.

Unlisted

--

--