DSL Reference
This page lists every Koin DSL keyword in tables, with the package to import and a link to the page that explains it. It covers the Compiler Plugin DSL and the Classic DSL (lambda and constructor-reference forms). For annotations, see the Annotations Reference.
DSL styles
| Style | Example | Import |
|---|---|---|
| Compiler Plugin DSL | single<UserRepository>() | org.koin.plugin.module.dsl.* |
| Classic DSL, constructor reference | singleOf(::UserRepository) | org.koin.core.module.dsl.* |
| Classic DSL, lambda | single { UserRepository(get()) } | org.koin.dsl.* |
The Compiler Plugin DSL needs the Koin Compiler Plugin. The plugin reads the constructor, generates the get() calls and checks the graph at build time. Without the plugin, a call such as single<T>() throws at runtime: Error while using 'single<T>()'. The Koin Compiler plugin is missing. Please fix your setup to use 'io.insert-koin.compiler.plugin' Koin Gradle Plugin.
The constructor-reference form (singleOf(::T)) is also checked by the Compiler Plugin when it is applied. A hand-written lambda is not: the plugin tracks the type it provides, not what the lambda body does. To pick a style, see Choosing your style.
Application DSL
Functions that create and start a Koin container. See Starting Koin.
| Function | Package | What it does |
|---|---|---|
startKoin { } | org.koin.core.context | Creates a KoinApplication and registers it in the global context. Throws KoinApplicationAlreadyStartedException if one is already started |
startKoin<T>() | org.koin.plugin.module.dsl | Compiler Plugin: starts Koin with the modules of the @KoinApplication class T |
koinApplication { } | org.koin.dsl | Creates a standalone KoinApplication, not registered globally. See Context Isolation |
koinApplication<T>() | org.koin.plugin.module.dsl | Compiler Plugin: standalone application with the modules of T |
koinConfiguration { } | org.koin.dsl | Declares a reusable KoinConfiguration without starting Koin |
koinConfiguration<T>() | org.koin.plugin.module.dsl | Compiler Plugin: configuration with the modules of T |
stopKoin() | org.koin.core.context | Closes the global container and all its instances |
loadKoinModules(module) | org.koin.core.context | Loads modules into the started global container |
unloadKoinModules(module) | org.koin.core.context | Unloads modules and drops their instances |
Inside startKoin { } and koinApplication { }
| Function | What it does |
|---|---|
modules(...) | Loads modules: one Module, a vararg or a List<Module> |
modules(UserModule::class, ...) | Compiler Plugin: loads the modules generated from @Module classes |
module<UserModule>() | Compiler Plugin: loads the module generated from one @Module class |
withConfiguration<T>() | Compiler Plugin: adds the modules of @KoinApplication class T to an existing application |
includes(configuration) | Applies one or more KoinConfiguration or KoinAppDeclaration blocks |
printLogger(level) | Logs to standard output. Default level Level.INFO |
logger(logger) | Uses your own Logger implementation. The default is EmptyLogger (no output) |
properties(map) | Loads properties from a Map<String, Any> |
fileProperties(fileName) | JVM only. Loads properties from a file, default /koin.properties |
environmentProperties() | JVM only. Loads system properties and environment variables |
allowOverride(Boolean) | Allows or forbids definition override. Default true. See Definitions |
createEagerInstances() | Creates createdAtStart definitions. startKoin and koinApplication already call it |
options(...) | Sets Koin options, such as viewModelScopeFactory(). See ViewModel |
lazyModules(...) | Loads lazy modules in the background (koin-core-coroutines). See Lazy Modules |
modules(A::class), module<T>() and withConfiguration<T>() go in a plain startKoin { } or koinApplication { }, not inside startKoin<T> { }.
Platform extensions add more functions to this block:
| Function | Artifact | See |
|---|---|---|
androidContext(context), androidLogger(level), androidFileProperties() | koin-android | Android Start |
fragmentFactory() | koin-android | Fragment Factory |
workManagerFactory() | koin-androidx-workmanager | WorkManager |
slf4jLogger(level) | koin-logger-slf4j | Ktor |
Module DSL
See Modules and Lazy Modules.
| Function | Package | What it does |
|---|---|---|
module { } | org.koin.dsl | Declares a module |
module(createdAtStart = true) { } | org.koin.dsl | Every single of the module is created at start |
lazyModule { } | org.koin.dsl | Declares a module loaded later, in the background (koin-core-coroutines) |
includes(otherModule, ...) | org.koin.dsl | Includes other modules, as vararg or Collection<Module> |
moduleA + moduleB | org.koin.dsl | Builds a List<Module> |
Definitions
What each keyword creates is explained in Definitions.
| Definition | Compiler Plugin DSL | Classic DSL, constructor reference | Classic DSL, lambda |
|---|---|---|---|
| Single | single<T>() | singleOf(::T) | single { T(get()) } |
| Factory | factory<T>() | factoryOf(::T) | factory { T(get()) } |
| Scoped (in a scope block) | scoped<T>() | scopedOf(::T) | scoped { T(get()) } |
| ViewModel | viewModel<T>() | viewModelOf(::T) | viewModel { T(get()) } |
| Worker (Android) | worker<T>() | workerOf(::T) | worker { T(get(), get()) } |
| Fragment (Android) | Not available | fragmentOf(::T) | fragment { T(get()) } |
| Function or builder | single { create(::buildT) } | singleOf(::buildT) | single { buildT(get()) } |
Packages:
| Keywords | Compiler Plugin DSL | Classic DSL |
|---|---|---|
single, factory, scoped, create | org.koin.plugin.module.dsl | org.koin.dsl (lambda), org.koin.core.module.dsl (singleOf, factoryOf, scopedOf, new) |
viewModel, viewModelOf (artifact koin-core-viewmodel) | org.koin.plugin.module.dsl | org.koin.core.module.dsl |
worker, workerOf (artifact koin-androidx-workmanager) | org.koin.plugin.module.dsl | org.koin.androidx.workmanager.dsl |
fragment, fragmentOf (artifact koin-android) | org.koin.androidx.fragment.dsl |
viewModel and viewModelOf in org.koin.androidx.viewmodel.dsl and org.koin.compose.viewmodel.dsl are deprecated: import them from org.koin.core.module.dsl.
create(::buildT) must be the only instruction in its lambda. Otherwise the Compiler Plugin reports create(::buildT) must be the only instruction in the lambda. See Definitions.
Lambda parameters
The lambda definitions have this signature:
| Keyword | Parameters |
|---|---|
single(qualifier, createdAtStart) { } | qualifier: Qualifier? = null, createdAtStart: Boolean = false |
factory(qualifier) { } | qualifier: Qualifier? = null |
scoped(qualifier) { } | qualifier: Qualifier? = null |
viewModel(qualifier) { } | qualifier: Qualifier? = null |
worker(qualifier) { } | qualifier: Qualifier = named<T>() |
Inside a definition lambda
The lambda runs with a Scope receiver and a ParametersHolder argument (params):
| Function | Returns | See |
|---|---|---|
get<T>(), get<T>(qualifier) | The instance. Throws NoDefinitionFoundException if missing | Injection |
getOrNull<T>() | The instance or null | Injection |
inject<T>() | Lazy<T> | Injection |
getAll<T>() | All instances bound to T | Advanced Patterns |
getProperty(key), getProperty(key, default), getPropertyOrNull(key) | A Koin property | Starting Koin |
params.get<T>(), (id) -> ... destructuring | An injected parameter | Injected Parameters |
new(::T) | A new T with every constructor parameter resolved by get() | |
create(::T) | Compiler Plugin: the same, with build-time checks | Definitions |
getSource<T>() | The scope source object | Scopes |
androidContext(), androidApplication() | The Android Context or Application | Android Start |
Definition options
Options apply after a definition, with an infix operator or in a withOptions { } block. They work on every definition keyword.
| Option | Form | What it does | See |
|---|---|---|---|
bind | single<T>() bind I::class | Adds a secondary type | Definitions |
binds | single<T>() binds arrayOf(I::class, J::class) | Adds several secondary types | Definitions |
onClose | single<T>() onClose { it?.close() } | Callback when the instance is released | Definitions |
withOptions { } | single<T>() withOptions { ... } | Opens the option block below | |
.override() | single<T> { ... }.override() | Marks an allowed override when allowOverride(false) is set | Definitions |
In withOptions { }, or in the trailing block of singleOf(::T) { }, factoryOf, scopedOf and viewModelOf:
| Option | What it does |
|---|---|
named("name") | String qualifier. See Qualifiers |
named<Type>() | Type qualifier |
bind<I>() | Adds a secondary type |
binds(listOf(I::class, J::class)) | Adds several secondary types |
createdAtStart() | Creates the single when Koin starts |
onClose { } | Release callback |
val appModule = module {
single<UserRepositoryImpl>() withOptions {
named("local")
bind<UserRepository>()
createdAtStart()
}
// Classic DSL, constructor reference
singleOf(::UserRepositoryImpl) {
named("local")
bind<UserRepository>()
}
}
Scope DSL
A scope block groups the definitions that live as long as a scope. See Scopes.
| Function | Package | What it does |
|---|---|---|
scope<T> { } | Module member | Scope identified by the type T, usually the type of the scope source |
scope(named("session")) { } | Module member | Scope identified by a qualifier |
Inside a scope block:
| Definition | Compiler Plugin DSL | Classic DSL, constructor reference | Classic DSL, lambda |
|---|---|---|---|
| Scoped | scoped<T>() | scopedOf(::T) | scoped { T(get()) } |
| Factory | factory<T>() | factoryOf(::T) | factory { T(get()) } |
| ViewModel | viewModel<T>() | viewModelOf(::T) | viewModel { T(get()) } |
| Worker (Android) | worker<T>() | workerOf(::T) | worker { T(get(), get()) } |
val sessionModule = module {
scope<UserSession> {
scoped<SessionCache>()
factory<SessionPresenter>()
}
}
Scope archetypes
An archetype is a scope block for a type of scope source rather than one class. Every source of that kind gets the definitions.
| Function | Artifact | Scope of | See |
|---|---|---|---|
viewModelScope { } | koin-core-viewmodel | Each ViewModel | ViewModel |
activityScope { } | koin-android | Each Activity | Android Scopes |
activityRetainedScope { } | koin-android | Each Activity, kept across configuration changes | Android Scopes |
fragmentScope { } | koin-android | Each Fragment | Android Scopes |
navigationScope { } | koin-compose-viewmodel-navigation | Each navigation destination (NavBackStackEntry) | Compose Scopes |
requestScope { } | koin-ktor | Each Ktor call | Ktor Scopes |
Related
- Definitions: what each definition type does and when to use it.
- Modules: organize and compose modules.
- Scopes: scope lifecycle, linking and closing.
- Annotations Reference: the annotation equivalent of this page.
- Compiler Plugin Setup: install the plugin that the Compiler Plugin DSL needs.