Starting Koin

How to start Koin from your Kotlin app

Koin is a DSL, a container & a pragamtic API to leverage your dependencies. The Koin DSL consists in:

  • KoinApplication DSL: describe how to configure your Koin application
  • Module DSL: describe your definitions

How to start Koin?

Starting Koin consists in using the startKoin fuction as below:

Starting Koin

In a classical Kotlin file:

fun main(vararg args: String) {

    startKoin {
        // enable Printlogger with default Level.INFO
        // can have Level & implementation
        // equivalent to logger(Level.INFO, PrintLogger())
        printlogger() 

        // declare properties from given map
        properties( /* properties map */)

        // load properties from koin.properties file or given file name
        fileProperties()

        // load properties from environment
        environmentProperties()

        // list all used modules
        // as list or vararg
        modules(moduleList) 
    }
}

Starting Koin for Android

In any Android class:

class MainApplication : Application() {

    override fun onCreate() {
        super.onCreate()

        startKoin {
            // use AndroidLogger as Koin Logger - default Level.INFO
            androidLogger()

            // use the Android context given there
            androidContext(this@MainApplication)

            // load properties from assets/koin.properties file
            androidFileProperties()

            // module list
            modules(offlineWeatherApp)
        }
    }
}

Starting Koin for Ktor

Starting Koin from your Application extension function:

fun Application.main() {
    // Install Ktor features
    install(Koin) {
        // Use SLF4J Koin Logger at Level.INFO
        slf4jLogger()

        // declare used modules
        modules(helloAppModule)
    }
}

KoinApplication builders

  • startKoin { } - Create and register following KoinApplication instance
  • koinApplication { } - create KoinApplication instance
// Create and register following KoinApplication instance
startKoin {
    logger()
    modules(coffeeAppModule)
}

// create KoinApplication instance
koinApplication {
    logger()
    modules(coffeeAppModule)
}

Logging

At start, Koin log what definition is bound by name or type (if log is activated):

[INFO] [Koin] bind type:'org.koin.example.CoffeeMaker' ~ [type:Single,class:'org.koin.example.CoffeeMaker']
[INFO] [Koin] bind type:'org.koin.example.Pump' ~ [type:Single,class:'org.koin.example.Pump']
[INFO] [Koin] bind type:'org.koin.example.Heater' ~ [type:Single,class:'org.koin.example.Heater']

KoinApplication DSL recap

A quick recap of the Koin DSL keywords:

  • startKoin { } - Create and register following KoinApplication instance
  • koinApplication { } - create KoinApplication instance
  • modules(...) - declare used modules
  • logger() - declare PrintLogger
  • properties(...) - declare map properties
  • fileProperties() - use properties from external file
  • environmentProperties() - use properties from environment
  • androidLogger() - declare Android Koin logger
  • androidContext(...) - use given Android context
  • androidFileProperties() - use properties file from Android assets
  • slf4jLogger(...) - use SLF4J Logger

Documentation Topics

Below are some further readings: