Koin Dependency Injection

Jaspreet Singh
McAppMedia
Published in
2 min readDec 20, 2019

Koin is a dependency injection for Kotlin Users.

We can use koin in Android as a dependency injection framework as it is Light weight than Dagger2, and easy to understand. It is built up by IntelliJ team which has built android studio , Kotlin and other development tools.

Note : We are working with Koin android module here , Koin view model is diffrent dependency, we will talk later about it.

Koin Set Up

// Add Jcenter to your repositories if needed
repositories {
jcenter()
}
dependencies {
// Koin for Android
compile 'org.koin:koin-android:$koin_version
}

So lets think about repositories ( HelloRepositories), I think every one has heard about this if they have gone through New Architectural Pattern. if any one not heard, Please make a comment so i can make a separate tutorial over this.

Lets think we have one class name MySimplePresenter and they dependent on some other class, so what we do. Wheater we make new object inside MySimplePresenter or get it from somewhere else . So We will get this with the help of Dependency Injection.

class MySimplePresenter(val repo: HelloRepository) {

fun sayHello() = "${repo.giveHello()} from $this"
}

Below is Hello Repository interface and their Implementation.

interface HelloRepository {
fun giveHello(): String
}

class HelloRepositoryImpl() : HelloRepository {
override fun giveHello() = "Hello Koin"
}

Now , We have to define these thing in our module .

Create a new kotlin file and paste

val appModule = module {

// single instance of HelloRepository
single<HelloRepository> { HelloRepositoryImpl() }

// Simple Presenter Factory
factory { MySimplePresenter(get()) }
}

Note: factory will create new instance every time, whenever activity will need .

Now Make your Application class or paste the below code for registering the module inside application class.

class MyApplication : Application(){
override fun onCreate() {
super.onCreate()
// Start Koin
startKoin{
androidLogger()
androidContext(this@MyApplication)
modules(appModule)
}
}
}

Lets use MySimplePresenter in our Activity, we can use get( ) or by inject( ) to retrive koin instance .

get( ) is used to get instance directly

by inject ( ) is used to get instance lazy

class MySimpleActivity : AppCompatActivity() {

// Lazy injected MySimplePresenter
val firstPresenter: MySimplePresenter by inject()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

// firstPresenter.sayHello()
}
}

So this is how we can easy to built our app with koin. I will cover koin with view model in coming tutorial.

Contact me for Android projects or job at preetjaspreet90@gmail.com

My github profile : https://github.com/jaspreet1990

You can check my app here : https://play.google.com/store/apps/details?id=com.mcappmedia.scanner

--

--

Jaspreet Singh
McAppMedia

Working in Android Developemen and Tech Mentor .My Github profile is: https://github.com/jaspreet1990 , Contact me at preetjaspreet90@gmail.com for more info.