Zoom Webinar: Learn and enhance your TypeScript skills | 28th March 2023 | 10 AM PST [10:30 PM IST] Register here

Koin- A Dependency Injection Framework for Kotlin



July 6, 2021


July 6, 2021

In this article, you will learn what dependency injection is, its importance in Android applications, what are the most popular DI solutions available, and how to use Koin to inject dependencies in your Android application.

What is Dependency Injection?

Dependency Injection is the technique that enables creating a dependent object outside of the class and making it available to the class in different ways.

To better understand the concept let us consider two classes A and B. When class A refers to class B using some methods of class B, it creates the dependency between the two classes. In this case, A will create its own instance for class B to get an object it needs.

Koin- A Dependency Injection Framework for Kotlin

Creating such instances inside the main class creates tight coupling between class A and B which is problematic.

There are other ways for a class to get an object from another class,

  1. One way to do that is by using some Android API.
  2. Another way is, the class A constructor can receive the class B object as a parameter.

The second approach is used in a dependency Injection called Constructor Injection.

Koin- A Dependency Injection Framework for Kotlin

Certain Android framework classes such as activities and framework are instantiated by the system so the construction Injection is not possible. In such a case, Field Injection is used to instantiate dependencies after creating a class.

Furthermore, in a smaller application, such instances can be manually created and managed, but in a larger application, the object must be automatically instantiated outside of the class using a mechanism known as automated dependency Injection.

Why is it Important to use Dependency Injection?

Managing dependencies in the application is getting more important than ever to maintain a level of abstraction among the classes. Dependency injection enables developers to avoid configuring dependencies statically by configuring them from the outside.

This contributes to class abstraction, keeps your application components loosely coupled, simplifies code testing and class inheritance. Overall, it helps developers to write clean code faster.

In Android, there are different approaches to manage dependencies using dependency injection. The most popular dependency injection frameworks are Dagger and Koin, which appear to be very similar but work differently.

Difference between Dagger2 and Koin

Dagger2 is the popular dependency injection library used for Java and Kotlin applications. It is written in Java and maintained by Google. It provides compile-time dependencies that address many issues of reflection-based solutions such as Guice (reflection-based solutions connect dependencies at run time).

Dagger2 uses generated code to access fields and uses annotations that increase the lines of code after compilation. Also, it is difficult to learn and implement.

Koin, on the other hand, is a more lightweight and sensible solution to dependency injection. It is written in Kotlin and eliminates a lot of boilerplate code. It generates significantly less code after compilation than Dagger and does not use annotation.

Using Koin to simplify Dependency Injection

Koin can be used by following three simple steps:

  1. Define a module:
  2. Define those entities that might be injected at some point in the application.
Koin- A Dependency Injection Framework for Kotlin
  1. start.Kotlin:
  2. Use the startKoin() function to start your DI process and specify which module should be available when needed.
Koin- A Dependency Injection Framework for Kotlin
  1. Perform an Injection:
  2. Koin features allow you to perform lazy injections most conveniently.
Koin- A Dependency Injection Framework for Kotlin

Koin allows injecting dependencies in the activity classes to inject dependencies in non-activity classes using KoinComponents.

Let’s get started with Koin in your application. First, create an empty activity project and name it. Wait for the project to build and add the following dependency on your app-level

build.gradle file.

Koin- A Dependency Injection Framework for Kotlin

Starting Koin in your application

According to official Koin documentation, you can start using Koin in your application with the help of the startKoin() function.

Koin- A Dependency Injection Framework for Kotlin

Stating Koin from another android Class

If you need to start Koin from another android class use startKoin() function and provide your Android Context instance.

Koin- A Dependency Injection Framework for Kotlin

Getting Android Context inside the module

The androidContext() & androidApplication() functions allow you to get the Context instance in a Koin module, to help you write an expression that requires the Application instance.

Koin- A Dependency Injection Framework for Kotlin

Inject Instances

Once you’ve declared some modules and started Koin, you can retrieve your instances in your Android Activity Fragments or Services using

Koin component extensions

  • by inject() – lazy evaluated instance from Koin container
  • get() – eager fetch instance from Koin container

In the case of a module that declares a presenter component.

Koin- A Dependency Injection Framework for Kotlin

Using by inject()

Using by inject()

Using get()

Using get()

Android Components and their lifecycle

Android components are managed by their lifecycle, therefore it is important to handle them carefully by providing scope.

You can’t directly instantiate an activity or fragment because of two reasons,

  1. To keep the same instance of a component, for all other components of the scop
  2. To keep the same instance alive, for a set amount of time

In such a case, we need to inject dependencies in the properties. Also, it is important to define the scope of each component according to its lifecycle. Koin Scope API helps to automatically handle application component scope.

If you are developing a mobile application, Koin is the best choice for DI.

Koin provides easy dependency injection for your application, allowing you to build a robust app more efficiently. Increase your productivity with Koin.

Build your application with DhiWise

DhiWise is the intelligent application development platform that follows a modern approach for agile development to achieve quality, reusability, and high performance in your web and mobile applications.

With DhiWise developers can efficiently use advanced technologies for their application tech stack. It provides the best features such as version control, easy maintenance and testing of applications, and cybersecurity to enable faster development.

For Kotlin development, it supports popular libraries like Koin, Retrofit, Glide, and Room. Also, it helps developers to build applications using clean code architecture.

“Are you planning to build your next Android application with Kotlin? Quickstart your application development with DhiWise

– Unlock your greatest potential”.