Dependency injection on Android with Dagger 2. Full learning path.

Dagger 2 is the most popular dependency injection framework in the Android world. The problem is that it is not that easy to master.

Here I decided to gather a number of different resources to cover everything related to learning Dagger 2, from the very basics to advanced usage in forms of text, video and code.

I. Basics.

If you never used Dagger 2 before and want to get it up and running in your project this article contains everything you need.

Dagger 2. Part I. Basic principles, graph dependencies, scopes.

Let’s check out some code. Here is a simplest Dagger 2 setup with single component.

OmarAflak/Dagger2-Sample

This level is enough for a small project, for example, a test when applying for a job.

II. Solid.

Next step would be the splitting of your component class. This level is enough to maintain clean DI in your application. Why would you want to do it? Here are 3 main reasons:

  1. Convert some of your global singletons into local singletons to save some memory.
  2. Decrease the size of your component class.
  3. Inject activity context.

Dagger 2. Part II. Custom scopes, Component dependencies, Subcomponents

To recap, there are two classic approaches to splitting the component class: Component Dependencies and Subcomponents. Those are two main approaches you need to become familiar with at this stage.

Let’s revisit subcomponents again, here is the comparison:

Dagger 2 – about (sub)components in a few words

And a code sample:

Dagger 2 subcomponents vs component dependencies

Some thoughts on how to split components:

Dagger- Should we create each component and module for each Activity/ Fragment

Now let’s check some code.

Dependent components example:

gk5885/dagger-android-sample

Subcomponents example:

srinurp/DaggerSubcomponents

III. Advanced.

Here you’ll learn features to make your DI code cleaner.

Following article covers some cool features available in Dagger 2.

Dagger 2. Part three. New possibilities

In the following part we are introducing more way of scoping, they are Android specific and more advanced.

  • First one is called Activity Multibinding.

Activities Subcomponents Multibinding in Dagger 2 – froger_mcs dev blog – Coding with love {❤️}

Code illustration:

frogermcs/Dagger2Recipes-ActivitiesMultibinding

  • Second one is Android Injector.

Android and Dagger 2.10 AndroidInjector

Code example:

googlesamples/android-architecture

IV. Rehearsal

It’s time to revisit everything that we learned above, check out the video covering most of the information.

And this awesome project showcasing and comparing all approaches.

msesma/DaggerPlayground

V. Testing

A huge advantage of using DI is increased code testability. Here we look at best practices of testing with Dagger 2.

Testing with Dagger

VI. Bonus

Now you should be very comfortable with using Dagger 2, but it is not the only framework. Check out other DI frameworks in this section. I’ll cover 3 in different formats: text, video and code, so you can pick whichever you prefer.

Koin article:

Moving from Dagger to Koin — Simplify your Android development

Kodein Github repo:

Kodein-Framework/Kodein-DI

Toothpick video:

Toothpick: A New Approach to Dependency Injection on Android

“Dependency injection on Android with Dagger 2. Full learning path.” Posted first on ” Android App Development on Medium “
Author: Alexey Verein

Author: Pawan Kumar

Leave a Reply

Close Menu
%d bloggers like this:
Skip to toolbar