Login Example using Efficient Dagger2 — Android Injector

Umang Burman
3 min readSep 23, 2018

--

The Efficient Dagger 2.10+ Approach

This is a very simple Login Example of Dagger2 — An Efficient Approach in Android. It takes the input from the UI, uses Dagger2 and displays back on the UI efficiently.

This example is for those beginners who want to learn this efficient approach a.k.a Android Injector. One of the primary advantages of Dagger 2 over most other dependency injection frameworks is that its strictly generated implementation (no reflection) means that it can be used in Android applications. However, there are still some considerations to be made when using Dagger within Android applications.

So Let’s Get Started:

  1. What is Dependency Injection (Efficient)?
  2. What are ComponentInjector, AndroidInjector and other components?
  3. How does it work?
  4. Implementation Step-by-Step
  5. Conclusion

What is Dependency Injection (Efficient)?

Answer: Dependency Injection in build upon the concept of Inversion of Control. Which says that a class should get its dependencies from outside. In simple words, no class should instantiate another class but should get the instances from a configuration class.

If a java class creates an instance of another class via the new operator, then it cannot be used and tested independently from that class and is called a hard dependency.

Before and After Applying DI

What are ComponentInjector, AndroidInjector and other components?

Answer: One of the central difficulties of writing an Android application using Dagger is that many Android framework classes are instantiated by the OS itself, like Activity and Fragment, but Dagger works best if it can create all the injected objects. Instead, you have to perform members injection in a lifecycle method.

This has a few problems:

  1. Copy-pasting code makes it hard to refactor later on. As more and more developers copy-paste that block, fewer will know what it actually does.
  2. More fundamentally, it requires the type requesting injection (FrombulationActivity) to know about its injector. Even if this is done through interfaces instead of concrete types, it breaks a core principle of dependency injection: a class shouldn’t know anything about how it is injected.

To overcome these, Dagger has introduced dagger.android from version — 2.10 onwards. The classes in dagger.android offer one approach to simplify this pattern.

  1. AndroidInjectionModule: Contains bindings to ensure the usability of dagger.android framework classes. This module should be installed in the component that is used to inject the Application class.
  2. ContributesAndroidInjector: Generates an AndroidInjector for the return type of this method. The injector is implemented with a Subcomponent and will be a child of the Module’s component. This annotation must be applied to an abstract method in a Module that returns a concrete Android framework type (e.g. FooActivity, BarFragment, MyService, etc). The method should have no parameters.
  3. HasActivityInjector: Provides an AndroidInjector of Activitys.
  4. DispatchingAndroidInjector: Performs members-injection on instances of core Android types (e.g. Activity, Fragment) that are constructed by the Android framework and not by Dagger. This class relies on an injected mapping from each concrete class to an AndroidInjector.Factory for an AndroidInjector of that class. Each concrete class must have its own entry in the map, even if it extends another class which is already present in the map. Calls Object.getClass() on the instance in order to find the appropriate AndroidInjector.Factory.
  5. AndroidInjection: Injects core Android types.

How does it work?

Answer: AndroidInjection.inject() gets a DispatchingAndroidInjector from the Application and passes your activity to inject(Activity). The DispatchingAndroidInjector looks up the AndroidInjector.Factory for your activity’s class (which is YourActivitySubcomponent.Builder), creates the AndroidInjector (which is YourActivitySubcomponent), and passes your activity to inject(YourActivity).

Implementation Step-by-Step

Please refer to the be low Repository for Step-by-Step Implementation:

Click on the GitHub Repository of Dagger2 Android Efficient Approach

Conclusion

Hopefully this guide introduced you to a lesser known yet useful form of Android application dependency injection known as Dagger2 — Efficient Approach.

--

--

Umang Burman

Android and iOS App Developer: Passionate about mobile development and always excited to learn new updates. Feel free to connect anytime.