Whilst I’ve used the dependency injector Dagger 2 a few times before, my knowledge was very much gained “in the field”, i.e. from seeing it in the wild. I recently decided to learn how to use it properly, so worked through a few of the more popular tutorials inside a repo. This GitHub repo is available here, and contains completed versions of all of the tutorials listed here.
In this post I’ll briefly cover the 5 tutorial series used. They’re all very high quality, and I’m extremely grateful to the authors for helping improve my understanding!
PS: The /r/AndroidDev Discord server has a
#dependency-injection channel if you need any Dagger 2 help.
This series by Elye was my personal favourite mentioned in this post. It ramped the difficulty up very gently, keeping all implementations as simple as possible to avoid confusion. The writing style is very informal, the memes / typos of which can be a little distracting, but the clear content outweighs this.
All of the posts contain plenty of diagrams, which really aid understanding of the more abstract concepts of Dagger. Intuitive names (e.g. “MagicBox”) are used, along with notes on the more common naming standards.
This tutorial from 2013 is not Android, or even Java specific. It instead introduces the concepts of dependency inversion, inversion of control, and dependency injection, essential to Dagger. Whilst it is written in C#, the concepts are all simple enough that rewriting them in Kotlin was not a particularly tricky barrier. Admittedly, without having prior C# knowledge this would have been more challenging, but the extremely high quality of the writing convinced me to work through it.
All of the concepts are worked through completely, with the surrounding logic / concepts described in a very straightforward manner. The example used throughout is to do with logging system messages.
This single-page tutorial is very short and simple, and effectively shows all of the core Dagger concepts. However, this conciseness and text-only style risks alienating some devs who aren’t already familiar with the concepts within.
The post is written in Java, and uses a intuitive “Car with an Engine and Brand” example.
This tutorial is another single-page one, that provides the minimum knowledge needed to get started with Dagger. Unfortunately this tutorial doesn’t dive very deep, and uses an unnecessarily confusing example (injecting blood types into a body). Still, it provides the reader with the essentials, and would be useful as a refresher article.
This 2 part tutorial (part 1, part 2) suffers a little bit from jumping around many files, risking the reader losing track of what the core concepts actually are, and getting caught up in boilerplate code. The posts are very code heavy, which can be beneficial for some readers, but personally I would have preferred more explanation along the way.