Back to Koin

Start Koin Reference

docs/reference/koin-core/start-koin.md

4.2.13.2 KB
Original Source

Quick reference for starting Koin. For detailed guide see Core - Starting Koin.

Starting Methods

MethodUse Case
startKoin { }Standard apps - registers in GlobalContext
koinApplication { }Testing, SDKs - isolated instance
koinConfiguration { }Configuration for Compose, Ktor
startKoin<T>()Typed startup with Compiler Plugin

Basic Startup

kotlin
startKoin {
    modules(appModule)
}

Complete Configuration

kotlin
startKoin {
    logger(Level.INFO)
    environmentProperties()
    fileProperties()
    properties(mapOf("env" to "production"))
    modules(coreModule, networkModule)
    lazyModules(analyticsModule)
    createEagerInstances()
    allowOverride(false)
}

Configuration Options

OptionDescription
logger()Set logging level and implementation
modules()Load modules immediately
lazyModules()Load modules in background
properties()Load properties from map
fileProperties()Load from koin.properties file
environmentProperties()Load from system/environment
createEagerInstances()Create all createdAtStart singletons
allowOverride()Enable/disable definition overriding

Typed Startup (Compiler Plugin)

Requires Koin Compiler Plugin and @KoinApplication:

kotlin
@KoinApplication
class MyApp

// Start
startKoin<MyApp>()

// With configuration
startKoin<MyApp> {
    printLogger()
}

Dynamic Module Management

kotlin
// Load after startup
loadKoinModules(featureModule)

// Unload
unloadKoinModules(featureModule)

Stopping Koin

kotlin
stopKoin()  // Global instance

// Isolated instance
koinApp.close()

Logging

LoggerPlatformDescription
EmptyLoggerAllNo logging (default)
PrintLoggerAllConsole output
AndroidLoggerAndroidLogcat
SLF4JLoggerJVMSLF4J
kotlin
startKoin {
    logger(Level.DEBUG)  // or androidLogger() for Android
}

Properties

kotlin
startKoin {
    environmentProperties()
    fileProperties()  // koin.properties
    properties(mapOf("key" to "value"))
}

// In module
single {
    ApiClient(url = getProperty("server_url"))
}

Platform Examples

Android

kotlin
class MainApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidLogger()
            androidContext(this@MainApplication)
            modules(appModule)
        }
    }
}

Compose

kotlin
@Composable
fun App() {
    KoinApplication(
        configuration = koinConfiguration { modules(appModule) }
    ) {
        MainScreen()
    }
}

Ktor

kotlin
fun Application.module() {
    install(Koin) {
        slf4jLogger()
        modules(appModule)
    }
}

See Also