A pragmatic lightweight dependency injection framework for Kotlin developers.
Quick Demo
Add implementation 'io.insert-koin:koin-android:3.5.0' to your app's build.gradle.kts file.val myModule = module {
single { MyRepository() }
factory { MyViewModel(get()) }
}startKoin {
androidContext(applicationContext)
modules(myModule)
}In your Composable or Activity, use 'by inject()' to get dependencies and update the UI accordingly.
Quick Start
io.insert-koin:koin-coreSetup Steps
- Add Koin dependency to your build.gradle.kts or build.gradle
- Define Koin modules with your dependencies
- Start Koin in your Application class or entry point
- Inject dependencies using by inject() or get()
Overview
Koin is a lightweight dependency injection framework for Kotlin. It uses Kotlin's DSL and functional programming to provide a simple and pragmatic way to manage dependencies in Android, Kotlin Multiplatform, and Jetpack Compose projects.
Features
Use Cases
When Not to Use
Usage Examples
val appModule = module {
single { Repository() }
factory { ViewModel(get()) }
}Defines a module with a singleton Repository and factory-scoped ViewModel.
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
startKoin {
androidContext(this@MyApp)
modules(appModule)
}
}
}Initializes Koin with Android context and the defined module.
class MainActivity : AppCompatActivity() {
private val viewModel: MyViewModel by inject()
}Uses property delegation to inject a ViewModel instance.
Common Pitfalls
- Circular dependencies can cause runtime errors and must be avoided
- Overusing scopes or not cleaning up can lead to memory leaks
- Debugging missing bindings or incorrect modules might require careful logging
- Limited compile-time checks compared to annotation-based DI frameworks
Integration
Works well with
- Koin provides Android extensions for seamless lifecycle integration
- Use Koin's testing modules to easily swap dependencies in unit and instrumented tests
Alternatives
Dagger Hilt
Offers compile-time safety and is officially supported by Google for Android
Kodein
Another Kotlin-first DI framework with a similar declarative approach
Koin Annotations
Experimental annotation-based alternative from the Koin team for more type safety
FAQ
Is Koin better than Dagger?
Koin is lighter and easier to learn, while Dagger provides more compile-time safety. The choice depends on project complexity and team preference.
Does Koin support Java?
Koin is designed for Kotlin and leverages Kotlin-specific features, so it's best used in Kotlin projects. Java interoperability is limited.
How do I test with Koin?
Koin offers testing modules like koin-test that allow you to override dependencies and use mock providers in your tests.
Can I use Koin in multi-module projects?
Yes, Koin supports modular setups where you can define modules in different Gradle modules and load them together.
What is the performance impact of Koin?
Koin is lightweight with minimal runtime overhead, but dependency resolution happens at runtime, so ensure modules are optimized.
