Koin – Introduction

Reading Time: 3 minutes

In the introduction part of the tutorial, we will get to know Koin, a Dependency Injection (DI) framework. We will build up for this a simple ordering app. First without any DI, than we gonna modify it to use Koin.

Koin is a dependency injection framework for Android and Kotlin developers.

It is written completely in Kotlin, that’s why it is efficient and lightweight. It has also a very good support for Android.

Dependency Injection

First of all, we have to talk about dependency Injection. In software engineering, dependency injection is a technique, a design pattern whereby one object supplies the dependencies of another object.

We can create the dependent object outside of the class and provide those object to class from different ways. DI can help with, moving the creation and binding of the dependent objects outside of the class that depends on them.

Traditional Coding

Dependency Injected

For example, when using an Object Oriented Programming language (such as Java/Kotlin on Android), objects will call methods on other objects that they have references to. A dependency is when one of the objects depends on the concrete implementation of another object.

Our example

To show you how Koin works, we are going to modify a simple ordering app. Here you can order a burger and only one type of drink, which is a mineral water. To let the waiters know who ordered the burger, we also need to give our name.

When we are done with the order, we can send it to the waiters by tapping the blue Order button. If our order fits the requerements (eg our name is not empty), then it will be inserted to the ordering list.

The models

In this tutorial we are going to follow the MVVM architecture pattern, and in the Chapter 3 we are going to modify the ViewModel as well to use Koin.

If you are not familiar with MVVM, check out the guide made by Google: Android app architecture

We are going to have 4 models

      • Burger
      • Drink
      • Guest
      • Order

The Burger, Drink and Guest contains only a string property. The Order has all of this 3 data classes. When we order a burger, then we can set our name, choose a burger, and we can select only one type of drink. It means from all of this data classes the app is going to create a new instance. It will be add it to the list, what is available in the MainActivityViewModel.kt.

The ViewModel

In the MainActivityViewModel we can find the instances, the private list of the orders, and a LiveData.

When we insert a new order, then the event of the button’s setOnClickListener will send the data to the ViewModel. This ViewModel will populate the list of orders, and also the listOrdersLD LiveData. When the listOrdersLD has been modified, then it will tell the UI, that we have a new item. In the MainActivity the listOrdersLD’s observer will notify the RecyclerView to add the new item to the list on the user interface.


If you check the Project menu at the left side, then in the adapter folder you can find an interface. It contains to abstract method, so without body. This interface is used in the OrderAdapter.kt. Because of the usage of the RwAdapter.kt, we have to implement 2 more function also. They are the getData() and the insertData() methods.

We won’t use the getData() method in this example. It is here just to let you know how to implement more functions in the interface.

The second method, the insertData() will add the new item to the list in the OrderAdapter.

The project files

You can download the starter project files from GitHub.

Download Project


I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉

Have a nice a day! 🙂



Follow and like us:
Click to rate this post!
[Total: 1 Average: 5]

Leave a comment

stay informed!

Subscribe to receive exclusive content and notifications