Dependency Injection the android way
Tags: Android App Development, Dependency Injection

Before jumping into DI in android, let's recall some concepts like what is Dependency, Injection, and DI in simple programming terminology.

What is Dependency? In Android Studio, dependencies allows us to include external library or local jar files or other library modules in our Android project. For example: Suppose I want to show some images in ImageView. But I’m using Glide Library to enhance the smoothness of application

What is Injection?

The injection is a technique that passes the dependency to dependent i.e Object to a class that wants to use them.

DI?

Dependency injection (DI) is a technique widely used in programming and well suited to Android development. By following the principles of DI, you lay the groundwork for good app architecture. Implementing dependency injection provides you with the following advantages: Reusability of code. Ease of refactoring Instead of creating dependencies, dependent uses some third-party framework to do.

Example :

public class A{

//Dependency

B obj=new B(“DI”);

}

Here A depends on B to function properly as A depends upon the instance of B class.

In other words, DI is the Decoupling of classes from the dependent Or Inject dependencies i.e making your design to be Loosely Coupled and follow Dependency Inversion and Single Responsibilities Principles.

So we can say asking a framework or third party to create the object of dependency instead of dependent creating the object of dependency is referred to as DI.

How to accomplish DI in Android?

In Android, we use third-party libraries like

Dagger Android Annotations Roboguice ButterKnife

to achieve Dependency injection depending upon our requirements.

Now let's get started with Dagger2…

What is Dagger2?

It is a dependency framework based on annotations. It uses code generation to access fields, methods.

Note: No private fields are allowed for injection by Dagger.

To implement include below dependency:

// App build.gradle

dependencies {

compile 'com.google.dagger:dagger:+'

annotationProcessor "com.google.dagger:dagger-compiler:2.4"

}

Major terms and various questions In Dagger to understand :

What is a component? What are the module and its purpose? What provides annotation does? Scope Annotations? what does Inject do?

Component Typically, Dagger2 uses this to generate code which uses Modules.

Or

In Simple words, Component is an interface class which contains all the places i.e Activities, fragment where you want to inject/use a particular object.

Code Snippet :

@Component(modules = { MyDemoModule.class})

public interface MyDemoComponent { void inject(MyActivity myActivity);

}

@Module Annotation

Classes with @Module annotations are used to provide objects which are to be Injected or to be used by Dependent classes. These classes contain methods defined with @Provides annotation which are used for dependency injection

In simple words, @Provides and .@Modules contains methods and classes which provide dependencies.

@Module

public class MyDemoModule { @Provides

@Singleton

User getUserObj() {

return new User();

}

}

Below is the User class to handle various user-specific values :

public class User { private String firstName;

private String lastName; public User(String firstName, String lastName) {

this.firstName = firstName;

this.lastName = lastName;

}}

Provides Annotation

These Annotations are used on methods that are defined in classes annotated with @Module.

Methods with @Provides annotation are used for dependency injection.

Example :

@Provides

@Singleton

User getUserObj() {

return new User();

}

Scope Annotations?

To define a scope of a method or class, we use these Scope Annotations. These annotations come under java. inject package.

By default there is no scope annotation, a new instance is created whenever there is an injection.

Commonly used scope annotation is :

Singleton Scope Annotation A Dependency using Singleton scope annotation gets instantiated just once throughout the app session.

Consider the below example to understand more about scope annotations in Dagger.

@Provides

@Singleton

User getUserObj() {

return new User();

}

Here, we have used @Singleton annotation to make sure that only a single object is created by getUserObj() for the User class.

Inject Annotation

Using these annotations we inject dependency independent classes or request dependencies.

We can use this annotation on Method, Constructor, or field.

Next will try to grow on the same concept using hilt and then learn the difference of both.