Starting Koin

Running your modules

One you have declared your dependencies with the Koin DSL, you just need to start Koin and inject!

The start function

The startKoin() function just need a list of Module to run and it will load every defintion from your modules. It is a function, it can be used every where, dependening on the used runtime. But it can be use only once.

startKoin(listOf(myModule))

Properties

External properties can be loaded while starting your modules.

Load from koin.properties

Koin will check if koin.properties is available, to load your properties. It has to be in src/main/resources Kotlin folder or asset Android folder.

External properties

For koin core/spark/ktor, you can also load environment properties with the bindSystemProperties parameter:

startKoin(listOf(myModule),bindSystemProperties = true)

Additional properties

You can also supply properties on the fly, with the Koin start function with the properties attribute:

startKoin(listOf(myModule),properties = HashMap(...properties))

Starting Koin

Depending on your chosen Koin module, Koin will be launched slightly differently.

Kotlin

For a Kotlin app, just start it in your main function:

fun main(vararg args: String) {
    start(listOf(myModule))
}

Android

For an Android app, you will start Koin in your Application class:

class MyApplication : Application(){
    override fun onCreate() {
        super.onCreate()
        // Start Koin
        startKoin(this, listOf(myModule))
    }
}

Spark

For a Spark web app, just run it from your main function:

fun main(vararg args: String) {
    // Spark
    start(modules = listOf(myModule)) {
        // Controllers
        runControllers()
    }
}

Loading Koin modules without starting Koin?

When developing library or sub modules, you don’t have the application entry point, but you can have the need to use Koin with some of your modules.

Use the loadKoinModules to load Koin modules (vararg). It will start Koin if not already started.

And now?

Your dependencies are delcared in modules. You have started Koin. Let’s go with injection!