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.
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.
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,
The second approach is used in a dependency Injection called Constructor Injection.
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.
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.
Koin can be used by following three simple steps:
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
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.
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.
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.
Once you’ve declared some modules and started Koin, you can retrieve your instances in your Android Activity Fragments or Services using
In the case of a module that declares a presenter component.
Using by inject()
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,
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.
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”.