Home » Android » When to use Dagger in android application?

When to use Dagger in android application?

Posted by: admin April 23, 2020 Leave a comment


I know that Dagger is a dependency injection framework, but I have not still used it in my projects.

I am starting a new project and wondering if Dagger is suitable for it. As far as I understand, using of Dagger leads to a lot of boilerplate code and annotations.
So I am not sure if it is not an overkill for my relatively simple project.

A bit about the project. It is focused on image processing and main part of functionality is built around it. However, it will also probably have a simple backend for data storage.

In general, I would like to know some basic principles that I can use to choose using Dagger for a project or not.

How to&Answers:

Basic Understanding:

Suppose, you want to test your application that deals with Credit Card service. For testing purpose you must not want to Access a real RPCCreditCardService as it will need real transaction and other stuffs that you don’t want to perform during development. In that case you must had to create a clone fake service that will mimic the same thing that real CreditCardService does but not transact anything. If you use the dependency injection framework you can define common tasks in a dependency and inject it in both fake and real service. It will minimize coding complexity as well as helps to make each module independent.

From the documentation:

By using dependency injection framework, each class is easy to test. You don’t need a bunch of boilerplate just to swap the RpcCreditCardService out for a FakeCreditCardService.

Dependency injection isn’t just for testing. It also makes it easy to create reusable, interchangeable modules. You can share the same AuthenticationModule across all of your apps. And you can run DevLoggingModule during development and ProdLoggingModule in production to get the right behavior in each situation.


For more detailed understanding you can check this discussion.