A pragmatic lightweight dependency injection framework for Kotlin developers.

Apache-2.0KotlinComposeKMP

Quick Demo

1Add Dependencyterminal
Add implementation 'io.insert-koin:koin-android:3.5.0' to your app's build.gradle.kts file.
2Create a Modulecode
val myModule = module {
    single { MyRepository() }
    factory { MyViewModel(get()) }
}
3Initialize Koincode
startKoin {
    androidContext(applicationContext)
    modules(myModule)
}
4Use in UIphone

In your Composable or Activity, use 'by inject()' to get dependencies and update the UI accordingly.

Quick Start

Maven Coordinates
io.insert-koin:koin-core

Setup Steps

  1. Add Koin dependency to your build.gradle.kts or build.gradle
  2. Define Koin modules with your dependencies
  3. Start Koin in your Application class or entry point
  4. 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

Lightweight and fast with no code generation
Easy to learn using Kotlin DSL and functional APIs
Supports Android, Kotlin Multiplatform (KMP), and Jetpack Compose
Built-in scoping and lifecycle management
Simple testing utilities for overriding dependencies

Use Cases

Dependency injection in Android applications
Modularizing large codebases with shared dependencies
Testing with mocked or fake dependencies
Kotlin Multiplatform projects requiring cross-platform DI
Rapid prototyping with minimal setup

When Not to Use

Projects requiring extensive compile-time safety and validation
Very simple apps with few or no dependencies
When already using other DI frameworks like Dagger Hilt
Java-based projects without Kotlin integration

Usage Examples

Define a Koin Module
val appModule = module {
    single { Repository() }
    factory { ViewModel(get()) }
}

Defines a module with a singleton Repository and factory-scoped ViewModel.

Start Koin in Android
class MyApp : Application() {
    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidContext(this@MyApp)
            modules(appModule)
        }
    }
}

Initializes Koin with Android context and the defined module.

Inject Dependency in Activity
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

Jetpack ComposeKotlin CoroutinesRetrofit for networkingRoom DatabaseAndroid ViewModel
  • 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.

Maintenance & Health

  • Latest version not specified; check the official repository or Maven Central for current releases.
  • SDK versions (minSdk, compileSdk, targetSdk) may vary; refer to Koin documentation for Android compatibility details.
  • Links provided are based on common knowledge; verify for any changes.

Related libraries