docs/setup/compiler-plugin.md
The Koin Compiler Plugin is the recommended approach for all new Kotlin 2.x projects. It provides auto-wiring, compile-time safety, and a cleaner DSL syntax.
The Koin Compiler Plugin is a native Kotlin Compiler Plugin (K2) that:
See Introduction to Koin Compiler Plugin for details on features and benefits.
First, check latest versions:
Then, in your gradle/libs.versions.toml:
[versions]
koin = "<KOIN_VERSION>"
koin-plugin = "<KOIN_PLUGIN_VERSION>"
[libraries]
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
koin-annotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin" }
[plugins]
koin-compiler = { id = "io.insert-koin.compiler.plugin", version.ref = "koin-plugin" }
In your settings.gradle.kts:
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
In your module's build.gradle.kts:
plugins {
alias(libs.plugins.koin.compiler)
}
dependencies {
implementation(libs.koin.core)
implementation(libs.koin.annotations) // For annotation support
}
[versions]
koin = "<KOIN_VERSION>"
koin-plugin = "<KOIN_PLUGIN_VERSION>"
[libraries]
koin-core = { module = "io.insert-koin:koin-core", version.ref = "koin" }
koin-annotations = { module = "io.insert-koin:koin-annotations", version.ref = "koin" }
[plugins]
koin-compiler = { id = "io.insert-koin.compiler.plugin", version.ref = "koin-plugin" }
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
plugins {
alias(libs.plugins.koin.compiler)
}
dependencies {
implementation(libs.koin.core)
implementation(libs.koin.annotations)
}
Import from the compiler plugin package:
import org.koin.plugin.module.dsl.*
import org.koin.dsl.module
val appModule = module {
single<Database>()
single<ApiClient>()
single<UserRepository>()
viewModel<UserViewModel>()
}
:::info
The Compiler Plugin DSL is in package org.koin.plugin.module.dsl. Classic DSL remains in org.koin.dsl.
:::
Use annotations on your classes:
@Singleton
class Database
@Singleton
class ApiClient
@Singleton
class UserRepository(
private val database: Database,
private val apiClient: ApiClient
)
@KoinViewModel
class UserViewModel(private val repository: UserRepository) : ViewModel()
@Module
@ComponentScan("com.myapp")
class AppModule
With the Compiler Plugin, use typed APIs to start Koin - no generated code needed:
@KoinApplication
@ComponentScan("com.myapp")
class MyApp
// Start Koin with typed API
startKoin<MyApp>()
// Or with additional configuration
startKoin<MyApp> {
androidContext(this@MyApplication)
printLogger()
}
Available typed APIs:
| API | Description |
|---|---|
startKoin<T>() | Start Koin globally with application T |
startKoin<T> { } | Start Koin with application T and configuration block |
koinApplication<T>() | Create isolated KoinApplication with T |
koinConfiguration<T>() | Create KoinConfiguration from T (for Compose KoinApplication, Ktor, etc.) |
Where T is a class annotated with @KoinApplication.
Configure the compiler plugin in your build.gradle.kts:
koinCompiler {
userLogs = true
debugLogs = false
dslSafetyChecks = true
}
| Option | Description | Default |
|---|---|---|
userLogs | Enable logs for component detection and DSL/annotation processing | false |
debugLogs | Enable verbose debug logs for internal plugin processing | false |
dslSafetyChecks | Validate that create() calls inside lambdas are the only instruction | true |
:::tip
Set userLogs = true during development to see which components are detected and processed by the plugin.
:::
The Koin Compiler Plugin will provide compile-time dependency verification - validating that all your dependencies can be resolved at build time rather than failing at runtime.
:::note Work in Progress
Compile-time safety for both DSL and Annotations is currently in development. This will replace the KSP-based KOIN_CONFIG_CHECK option with native Kotlin compiler integration.
:::
For projects with multiple Gradle modules:
// feature/build.gradle.kts
plugins {
alias(libs.plugins.koin.compiler)
}
dependencies {
implementation(libs.koin.core)
implementation(libs.koin.annotations)
}
// feature/src/main/kotlin/FeatureModule.kt
@Module
@ComponentScan("com.myapp.feature")
class FeatureModule
// app/build.gradle.kts
plugins {
alias(libs.plugins.koin.compiler)
}
dependencies {
implementation(project(":feature"))
implementation(libs.koin.core)
implementation(libs.koin.annotations)
}
// app/src/main/kotlin/MyModule.kt
@Module
@Configuration
class MyModule
// app/src/main/kotlin/MyApp.kt
@KoinApplication
class MyApp
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin<MyApp>()
}
}
Use @KoinApplication for the main application class with typed startup APIs.
The Compiler Plugin works with KMP projects:
// shared/build.gradle.kts
plugins {
id("org.jetbrains.kotlin.multiplatform")
alias(libs.plugins.koin.compiler)
}
kotlin {
sourceSets {
commonMain.dependencies {
implementation(libs.koin.core)
implementation(libs.koin.annotations)
}
}
}
Ensure the plugin is in your plugin repositories:
// settings.gradle.kts
pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
}
}
The Compiler Plugin requires Kotlin 2.3.20+. Check your Kotlin version:
// build.gradle.kts
plugins {
kotlin("jvm") version "2.3.20-Beta1" // Requires 2.3.20+
}
Make sure you're importing from the correct package:
// Compiler Plugin DSL
import org.koin.plugin.module.dsl.*
// Classic DSL
import org.koin.dsl.*
org.koin.plugin.module.dsl.*single { Class(get() ...) } or singleOf(::Class) with single<Class>()See Migrating from DSL to Compiler Plugin.
startKoin { modules(...) } to startKoin<MyApp>()See Migrating from KSP to Compiler Plugin for the complete guide.