DI with Koin — Introduction and Implementation

It is a smart Kotlin Dependency Injection Library.

Its features are:

  • Light weight
  • Easy to learn


  • module — it creates a module in Koin which would be used by Koin to provide all the dependencies.
  • single — it creates a singleton that can be used across the app as a singular instance.
  • factory — it provides a bean definition, which will create a new instance each time it is injected.
  • get() — it is used in the constructor of a class to provide the required dependency.


// koin
// Koin main features for Android (Scope,ViewModel ...)
implementation "io.insert-koin:koin-android:${koin_version}"
// Koin Android - experimental builder extensions
implementation "io.insert-koin:koin-android-ext:${koin_version}"
// Koin for Jetpack WorkManager
implementation "io.insert-koin:koin-androidx-workmanager:${koin_version}"
// Current version
koin_version= "3.0.1-beta-2"


interface KoinService {
fun provideService(): String


class KoinRepository : KoinService {
override fun provideService(): String = "Provide Koin Service 1"


class KoinViewModel(val repo: KoinRepository) : ViewModel(){
fun doSomethingWithService() = repo.provideService()
val appModule = module {    // single instance of KoinRepository
single<KoinService> { KoinRepository() }
// KoinViewModel
viewModel { KoinViewModel(get()) }
class MyApp : Application() {
override fun onCreate() {
startKoin {
class KoinActivity : AppCompatActivity() {    //lazy inject viewmodel
val myViewModel: KoinViewModel by viewModel()
override fun onCreate(savedInstanceState: Bundle?) {
// use your viewmodel here



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Raviraj Desai

Senior Manager at Axis Bank (Android — Kotlin lover. Tech stack: MVVM, Dagger2, Coroutines, RxJava2, Android Architecture Component ,Flutter Enthusiast ,Ejabber