Koin for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Koin: What It Is and Why You Need It



Koin is a lightweight, powerful, and intuitive Dependency Injection (DI) framework for Kotlin. Unlike heavyweight solutions like Dagger or Spring, Koin does not use reflection, annotations, or code generation. Everything is built on simple DSL (Domain Specific Language) constructs, making the code clean, readable, and easily testable.



Why do you need Koin? In modern complex application development, managing dependencies between classes is a key challenge. Without DI, you would create objects manually, leading to tight coupling, complicating testing and maintenance. Koin solves these problems by providing a simple way to register, configure, and retrieve dependencies.



Key advantages of Koin:

  • Lightweight: Minimal impact on APK size and build time.
  • Simplicity: No annotations, reflection, or code generation required.
  • Kotlin-first: Fully leverages the language's capabilities (DSL, lambdas, coroutines).
  • Modularity: Allows splitting dependencies into logical modules.
  • Testability: Easily swap dependencies in tests.
  • Android support: Has special modules for Android (ViewModel, Fragment, Compose).


Installing Koin



Koin is distributed via Maven Central. To add it to your project, include the dependencies in the build.gradle.kts (or build.gradle) file.



For Kotlin Multiplatform / JVM projects:

// build.gradle.ktsdependencies {    // Koin core module    implementation("io.insert-koin:koin-core:3.5.6")        // For testing    testImplementation("io.insert-koin:koin-test:3.5.6")}


For Android projects:

// build.gradle.kts (app module)dependencies {    // Core module    implementation("io.insert-koin:koin-android:3.5.6")        // For ViewModel    implementation("io.insert-koin:koin-androidx-viewmodel:3.5.6")        // For Jetpack Compose    implementation("io.insert-koin:koin-androidx-compose:3.5.6")        // For WorkManager    implementation("io.insert-koin:koin-androidx-workmanager:3.5.6")        // For testing    testImplementation("io.insert-koin:koin-test:3.5.6")    testImplementation("io.insert-koin:koin-test-junit5:3.5.6")}


Important: Versions may change. You can always check the latest version on the Koin GitHub repository.



Quick Start with Koin



Let's look at a simple example: we have a UserService that depends on UserRepository. Instead of creating the repository manually, we use Koin.



Step 1: Define the classes

// User repositoryclass UserRepository {    fun getUsers(): List<String> = listOf("Alice", "Bob", "Charlie")}

// Service that uses the repositoryclass UserService(private val repository: UserRepository) { fun printUsers() { repository.getUsers().forEach { println(it) } }}


Step 2: Create a Koin module

// Module — a container where we describe how to create dependenciesval appModule = module {    // single — creates one instance for the entire application (singleton)    single<UserRepository> { UserRepository() }        // factory — creates a new instance on each request    factory { UserService(get()) } // get() — requests the UserRepository dependency}


Step 3: Start Koin

// In the application entry point (main or Application)fun main() {    // Start Koi                    

Recommendations