Dependency Injection in KMP with Koin
Wire shared modules together using Koin in a multiplatform setup.
Why DI in KMP?
Dependency Injection decouples component creation from usage. In KMP, Koin is the go-to DI framework because it is pure Kotlin and works in commonMain without annotation processors.
// No annotation processing needed — Koin uses lambda DSL
// Add to build.gradle.kts:
// implementation("io.insert-koin:koin-core:3.5.0")
// androidMain: koin-android
// iosMain: no extra dep
fun main() { println("Koin works on all KMP targets") }Koin Module DSL
Define bindings in a module { } block using single, factory, or scoped.
import org.koin.core.module.dsl.*
import org.koin.dsl.*
val appModule = module {
single { HttpClient() } // one instance
single<UserRepository> { RemoteUserRepository(get()) }
factory { GetUserUseCase(get()) } // new instance each time
}All lessons in this course
- KMP Project Structure: commonMain, androidMain, iosMain
- expect/actual Mechanism for Platform APIs
- Sharing Repository and Use Case Layers
- Dependency Injection in KMP with Koin