Configs2026年4月29日·1 分钟阅读

Koin — Pragmatic Dependency Injection Framework for Kotlin

Koin is a lightweight dependency injection framework for Kotlin that uses a DSL to define modules and resolve dependencies at runtime, with first-class support for Android, Ktor, and Compose Multiplatform.

Introduction

Koin takes a pragmatic approach to dependency injection by avoiding code generation and annotation processing entirely. Instead, it uses a Kotlin DSL to declare modules and resolves dependencies at runtime through a lightweight service locator. This makes it fast to set up, easy to understand, and free of the compile-time overhead that annotation-based DI frameworks introduce.

What Koin Does

  • Defines dependency graphs using a Kotlin DSL with single, factory, and scoped declarations
  • Resolves dependencies at runtime via constructor injection or the by inject() delegate
  • Provides dedicated integrations for Android (ViewModel, WorkManager, Fragment), Ktor, and Compose Multiplatform
  • Supports scoped dependencies tied to Android lifecycle, navigation graphs, or custom scopes
  • Offers a verification API to check the entire dependency graph at test time

Architecture Overview

Koin maintains a global (or isolated) Koin instance that holds registered module definitions. Each definition specifies how to create a dependency and whether it is a singleton (single), a new instance per request (factory), or scoped to a lifecycle (scoped). When a dependency is requested via get() or inject(), Koin walks the registry, resolves constructor parameters recursively, and returns the instance. No reflection beyond standard Kotlin is used, and no code generation step is required.

Self-Hosting & Configuration

  • Add the koin-core dependency plus platform-specific modules (koin-android, koin-ktor, koin-compose)
  • Call startKoin {} in your application entry point with a list of modules
  • Use koinApplication {} for isolated Koin instances in libraries or testing
  • Organize dependencies into feature-specific modules for clean separation
  • Use the Koin Gradle plugin for compile-time verification of the dependency graph

Key Features

  • No annotation processing or code generation — pure Kotlin DSL
  • Android ViewModel injection with viewModel {} and koinViewModel() in Compose
  • Compose Multiplatform support for shared DI across Android, iOS, Desktop, and Web
  • Built-in test utilities: checkModules() verifies the entire graph without running the app
  • Lazy injection via by inject() delegate for on-demand initialization

Comparison with Similar Tools

  • Dagger/Hilt — compile-time DI with code generation; Koin is simpler to set up but validates at runtime
  • Kodein-DI — similar Kotlin DSL approach; Koin has broader Android and Compose Multiplatform support
  • Spring DI — full enterprise container; Koin is lightweight and targeted at Kotlin/Android projects
  • Guice — reflection-based Java DI; Koin's Kotlin DSL is more idiomatic for Kotlin codebases
  • kotlin-inject — compile-time Kotlin DI via KSP; Koin avoids code generation entirely

FAQ

Q: Is Koin a service locator or dependency injection framework? A: Koin uses a service locator pattern internally but provides constructor injection semantics through its DSL. Dependencies are declared in modules and resolved automatically via constructor parameters.

Q: How do I test with Koin? A: Use koinTest to start a test-specific Koin instance. Override production definitions with mock implementations using declareMock {} or by providing test modules.

Q: Does Koin support Kotlin Multiplatform? A: Yes. koin-core is a Kotlin Multiplatform library. Platform-specific modules (koin-android, koin-ktor) extend it for each target. Compose Multiplatform is supported via koin-compose.

Q: What happens if a dependency is missing at runtime? A: Koin throws a NoBeanDefFoundException. Use checkModules() in tests to verify the entire graph at build time and catch missing definitions before deployment.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产