CodeBlanks Logo Icon CodeBlanks

Koin

Learn the Koin DI framework.

What is Koin?

Koin is a lightweight dependency injection (DI) library for Kotlin. It is written with a Kotlin DSL; it uses no reflection and offers compile time type safety.

An app starts with startKoin { modules(appModule) }. Modules are declared with a module { ... } block; dependencies are provided with single or factory. Classes get their dependencies with by inject(). Koin needs no annotation processor like Dagger; setup is simple. In this lesson you will set up the basic startup flow.

Starting Koin

To start Koin you call startKoin { modules(appModule) }. This call builds a KoinApplication.

Example: startKoin { modules(appModule) }. The modules function lists the modules the app will use. Multiple modules are allowed: modules(module1, module2). The startKoin call happens once at app start; on Android it is common in Application.onCreate. Without starting Koin, by inject() does not work.

Practice Start Koin and print

Open a startKoin { } block. Print println("Koin ready").

Starter code

Main.kt
// Koin
// write here
import org.koin.core.context.startKoin

fun main() {
    
}

Sample solution

Main.kt
fun main() {
    startKoin {
    }
    println("Koin ready")
}

Expected output

Koin ready

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is Koin used for?

  • Dependency injection Correct answer
  • Serializing JSON
  • Making HTTP requests
  • Database management

Explanation: Koin is a dependency injection (DI) library.

2 Complete the startKoin call

Question
____ {
    modules(appModule)
}
Answer
startKoin {
    modules(appModule)
}

Explanation: startKoin opens the Koin application.

3 Match Koin parts

Term Meaning
startKoin Startup
module Module block
single Singleton
by inject Gets dependency

All lessons in Advanced