Tech Blog

Android

Dependency injection using Dagger 2

By Ankit Srivastava2 min read
← Back to blog

In a project with multiple classes, if a class (Class X) is dependent on another class (Class Y), then Class Y is a dependency on Class X.

To access Class Y in Class X, it would be required to instantiate Class Y. This makes the code tightly coupled and difficult to test. To avoid these problems, a design pattern called Dependency Injection is used.

  • What is Dependency Injection?

Dependency Injection is a design pattern in which a class is provided with its dependencies from outside. In technical terms, the class that is dependent on another class is called a client, and its dependency is called a service. The class that injects (provides) the service to the client is called an injector.

In this pattern, the client does not determine the service it is going to use, but it is the injector that determines the service that will be used by the client.

  • What is Dagger 2?

Dagger  2 is a compile-time, fully static framework for dependency injection in Android.

Dagger 2 uses the generated code and is based on annotations. It uses the following annotations : 

  • @Module : It is used over the class which provides dependencies by constructing objects.
  • @Provides : It is used over the function in the ‘Module’ class that will provide the object.
  • @Inject : It is used over the fields/functions/constructors to indicate dependency requests.
  • @Component : It is used over an interface that will connect the client and the service.
  • Types of Dependency Injection : 
  • Constructor injection : In this method, the dependencies of a class are passed to its constructor.
  • Field injection : When using field injection, the dependencies are provided after constructing the class.
  • Advantages of Dependency Injection Pattern : 
  • Dependency injection de-couples the classes and its dependencies.
  • Increases readability and reusability by removing dependency implementations from the client code.
  • It is easier to unit test the client code.

Keep reading

You may be interested in

Browse all articles →
Android Download file with Data URL
July 23, 20241 min read

Android Download file with Data URL

What is a Data URL? Data URLs are the type of URL scheme that is used to embed usually small types of files. In this URL, inline files are prefixed with the data: MIME type. data:[<mediatype>][;base64],<data> To download files having Data URLs inside Android: Firstly, we need to get an extension of the file from […]

By Bansari PanditRead More →
Download the file with blob URL in Android WebView
July 23, 20242 min read

Download the file with blob URL in Android WebView

What is the Blob URL? Blob URLs can only be generated by the browser with use of internal mapping and used within the current document while it’s open which means it is valid for the same session (ie. the life of the page/document). The blob URL looks like this: blob:http://example.com/550e8400-e29b-41d4-a716-446655440000 Below is the sample HTML […]

By Bansari PanditRead More →
Key Terminologies in Android Enterprise
December 20, 20227 min read

Key Terminologies in Android Enterprise

Android Enterprise is a Google-led initiative to enable the use of Android devices and applications in a workspace. This program offers APIs and tools for developers to integrate support of Android devices into the EMM (enterprise mobility management) or UEM (Unified Endpoint Management) solutions. The following are the key terms associated with Android Enterprise. This […]

By Sandeep KadamRead More →