Skip to main content
Version: 4.2

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​

StyleExampleImport
Compiler Plugin DSLsingle<UserRepository>()org.koin.plugin.module.dsl.*
Classic DSL, constructor referencesingleOf(::UserRepository)org.koin.core.module.dsl.*
Classic DSL, lambdasingle { 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.

FunctionPackageWhat it does
startKoin { }org.koin.core.contextCreates a KoinApplication and registers it in the global context. Throws KoinApplicationAlreadyStartedException if one is already started
startKoin<T>()org.koin.plugin.module.dslCompiler Plugin: starts Koin with the modules of the @KoinApplication class T
koinApplication { }org.koin.dslCreates a standalone KoinApplication, not registered globally. See Context Isolation
koinApplication<T>()org.koin.plugin.module.dslCompiler Plugin: standalone application with the modules of T
koinConfiguration { }org.koin.dslDeclares a reusable KoinConfiguration without starting Koin
koinConfiguration<T>()org.koin.plugin.module.dslCompiler Plugin: configuration with the modules of T
stopKoin()org.koin.core.contextCloses the global container and all its instances
loadKoinModules(module)org.koin.core.contextLoads modules into the started global container
unloadKoinModules(module)org.koin.core.contextUnloads modules and drops their instances

Inside startKoin { } and koinApplication { }​

FunctionWhat 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:

FunctionArtifactSee
androidContext(context), androidLogger(level), androidFileProperties()koin-androidAndroid Start
fragmentFactory()koin-androidFragment Factory
workManagerFactory()koin-androidx-workmanagerWorkManager
slf4jLogger(level)koin-logger-slf4jKtor

Module DSL​

See Modules and Lazy Modules.

FunctionPackageWhat it does
module { }org.koin.dslDeclares a module
module(createdAtStart = true) { }org.koin.dslEvery single of the module is created at start
lazyModule { }org.koin.dslDeclares a module loaded later, in the background (koin-core-coroutines)
includes(otherModule, ...)org.koin.dslIncludes other modules, as vararg or Collection<Module>
moduleA + moduleBorg.koin.dslBuilds a List<Module>

Definitions​

What each keyword creates is explained in Definitions.

DefinitionCompiler Plugin DSLClassic DSL, constructor referenceClassic DSL, lambda
Singlesingle<T>()singleOf(::T)single { T(get()) }
Factoryfactory<T>()factoryOf(::T)factory { T(get()) }
Scoped (in a scope block)scoped<T>()scopedOf(::T)scoped { T(get()) }
ViewModelviewModel<T>()viewModelOf(::T)viewModel { T(get()) }
Worker (Android)worker<T>()workerOf(::T)worker { T(get(), get()) }
Fragment (Android)Not availablefragmentOf(::T)fragment { T(get()) }
Function or buildersingle { create(::buildT) }singleOf(::buildT)single { buildT(get()) }

Packages:

KeywordsCompiler Plugin DSLClassic DSL
single, factory, scoped, createorg.koin.plugin.module.dslorg.koin.dsl (lambda), org.koin.core.module.dsl (singleOf, factoryOf, scopedOf, new)
viewModel, viewModelOf (artifact koin-core-viewmodel)org.koin.plugin.module.dslorg.koin.core.module.dsl
worker, workerOf (artifact koin-androidx-workmanager)org.koin.plugin.module.dslorg.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:

KeywordParameters
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):

FunctionReturnsSee
get<T>(), get<T>(qualifier)The instance. Throws NoDefinitionFoundException if missingInjection
getOrNull<T>()The instance or nullInjection
inject<T>()Lazy<T>Injection
getAll<T>()All instances bound to TAdvanced Patterns
getProperty(key), getProperty(key, default), getPropertyOrNull(key)A Koin propertyStarting Koin
params.get<T>(), (id) -> ... destructuringAn injected parameterInjected Parameters
new(::T)A new T with every constructor parameter resolved by get()
create(::T)Compiler Plugin: the same, with build-time checksDefinitions
getSource<T>()The scope source objectScopes
androidContext(), androidApplication()The Android Context or ApplicationAndroid Start

Definition options​

Options apply after a definition, with an infix operator or in a withOptions { } block. They work on every definition keyword.

OptionFormWhat it doesSee
bindsingle<T>() bind I::classAdds a secondary typeDefinitions
bindssingle<T>() binds arrayOf(I::class, J::class)Adds several secondary typesDefinitions
onClosesingle<T>() onClose { it?.close() }Callback when the instance is releasedDefinitions
withOptions { }single<T>() withOptions { ... }Opens the option block below
.override()single<T> { ... }.override()Marks an allowed override when allowOverride(false) is setDefinitions

In withOptions { }, or in the trailing block of singleOf(::T) { }, factoryOf, scopedOf and viewModelOf:

OptionWhat 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.

FunctionPackageWhat it does
scope<T> { }Module memberScope identified by the type T, usually the type of the scope source
scope(named("session")) { }Module memberScope identified by a qualifier

Inside a scope block:

DefinitionCompiler Plugin DSLClassic DSL, constructor referenceClassic DSL, lambda
Scopedscoped<T>()scopedOf(::T)scoped { T(get()) }
Factoryfactory<T>()factoryOf(::T)factory { T(get()) }
ViewModelviewModel<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.

FunctionArtifactScope ofSee
viewModelScope { }koin-core-viewmodelEach ViewModelViewModel
activityScope { }koin-androidEach ActivityAndroid Scopes
activityRetainedScope { }koin-androidEach Activity, kept across configuration changesAndroid Scopes
fragmentScope { }koin-androidEach FragmentAndroid Scopes
navigationScope { }koin-compose-viewmodel-navigationEach navigation destination (NavBackStackEntry)Compose Scopes
requestScope { }koin-ktorEach Ktor callKtor Scopes