Android Dependency Injection : What it is and How to do it?

Android Dependency Injection? You might already hear this thing. But if you are confused about what it is, then this post is for you. Today we will discuss what Dependency Injection is on Android? Why …

android dependency injection

Android Dependency Injection? You might already hear this thing. But if you are confused about what it is, then this post is for you. Today we will discuss what Dependency Injection is on Android? Why do we use Dependency Injection? What happens when we don’t use Dependency Injection?

If you want to learn every thing about “Android Dependency Injection” then you can follow This Complete Dependency Injection Course, where we will learn about Android Dependency Injection from Theory to Implementation. And we will also cover Dagger and Hilt Library for Dependency Injection here. 

What is Dependency Injection?

The term “Dependency Injection” or in short “DI” is independent of Android. It is a design pattern in Software Engineering.
The term is explaining itself as Inject Dependency.

dependency injection

To understand it in more detail, let’s break the term Dependency Injection. In two parts Dependency and Injection.

What is Dependency?

Consider the following Java class.

In the above class, we are creating the object of class Database inside the constructor of class User. So here, we can say that; the User is dependent on Database. So here, Database is a dependency for the class User.

What is Injection?

As we seen the User is Dependent on Database, now think how User can get its dependency, which is the Database.

We can consider the following ways.

Method #1

Create Database object inside the User Constructor. That is exactly, what we are doing in the above code. 

It is indeed a bad idea. We should keep the classes as independent from other classes. Now you can ask WHY?

  • If the classes are independent, you can reuse them.
  • For unit test each class, it is necessary that the classes are independent of each other.

Method #2

Instead of using new to instantiate the Database inside User’s constructor we can pas the Database as an argument to the User’s constructor.

So this is what we call as Dependency Injection. Means supplying the dependencies from outside the class. And more specifically for the above code, we can consider it as a Constructor Injection.

Now if we want to unit test the above class we can pass a dummy object of class Database to the constructor.

android dependency injection

I hope, now you have an idea that “What is Dependency Injection?”.

Definition of Dependency Injection

I guess you got the concept, so here is the definition for you of Dependency Injection.

Dependency Injection in build upon the concept of Inversion of Control. That means a class should get its dependencies from outside.
In more simpler words, your class cannot instantiate another class using a new keyword inside it. Instead, you have to supply the object from outside.

But, our main topic was Android Dependency Injection.

By Android Dependency Injection, here we mean that using the same Dependency Injection pattern while developing android apps.

Android Dependency Injection

In this post, we will not see any practical implementation. It was to clear the concept of Dependency Injection.

So let’s discuss DI in context of Android Application Development.

Consider a simple application, where we are fetching some data from a web server; let’s say a list of products (containing product title, product images, product price etc.). And then we are displaying the data in our android application. The same as we did in this Retrofit Android Example.

Assuming we are using Retrofit for the network request and Glide for loading images. See the following diagram.

android dependency injection

As you can see in the diagram, we have many dependencies. Now our task here is to follow the Dependency Injection here in the app to make everything easier. And for this, I am going to use a framework called Dagger 2.

If you are not aware then, Dagger 2 is now maintained by Google, and it is the most popular Dependency Injection framework available for Android and Java.

In the next post, we will learn using Dagger 2 in our Android Studio Project.

But remember, Dagger 2 is not only the option available. I will also tell you some other popular Android Dependency Injection Framework used by a lot of developers.

And I will try to tell you the practical implementation about many Android Dependency Injection Frameworks in my coming posts.

Popular Android Dependency Injection Frameworks

To make life easier, we have some Frameworks available to implement DI.

Dagger 2

Dagger is a fully static, compile-time dependency injection framework for both Java and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.

Square created this framework, but now Google is maintaining this framework. So I guess it is the best option to get started with Dependency Injection. At the same time, it is easy to learn, and you will find many documentation and tutorials available on the internet

ButterKnife

We use this library for injecting views into your Android Components. It is a lightweight library, and it uses annotation processing.
It can save you from using findViewById(). And it will do the ffindViewById() thing for you automatically.

So that’s it for this post friends. And in the next post (Daggger 2 Android Example), we will start with Dagger 2. Meanwhile, if you want to ask anything about Android Dependency Injection then leave your comments below. And if you found this post helpful, then please SHARE it with your friends to help us. Thank You 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x