Codelab to teach and demonstrate usage of dependency injection with Dagger 2
- About dependency injection(DI) and Inversion of Control(IoC)
- Introduction to the problem, why it matters.
- Why we use DI frameworks - which problems they solve for us.
- Brief history of DI. Spring -> Guice -> Dagger 1 -> Dagger 2
Initial presentation slides
- Basics of Dagger
- How to setup Dagger in your project.
- How to use
@Inject
annotation, creating simple@Component
- Adding
@Module
. Discussion when to use modules and when@Inject
annotation. - Usage of simple
@Singleton
scope. - Exploring Dagger generated source code and learning from it.
- Wiring together with Android
- Where to create and hold instances of components.
- Injecting Activities, Injecting Fragments.
- Using
@BindsInstance
to bind objects we don't own.
- Multi-module project setup
- How to compose modules together within
AppComponent
- Injecting Activity/Fragment and other dependencies within separate module.
- Sharing instances with other modules.
- How to compose modules together within
- Multibindings
- Composing
@IntoMap
and@IntoSet
. - Plugin based development within multi-module project.
- Discuss which problems can multibinding solve.
- Composing
- Instrumented Testing
- How to replace dependencies within
androidTest
. - Using separate component to replace dependencies in tests.
- How to assert on your analytic events.
- How to replace dependencies within
- Dagger Hilt migration
- Setting up Dagger Hilt in the project.
- How to Migrate project to Hilt
- Instrumented testing with Dagger Hilt
- Setting up testing in project.
- Replacing dependencies in tests
- Discussing Hilt pros and cons.
- Primarily this should be a learning resource for any engineer who wants to learn or recap Dagger usage.
- The codelab format is chosen with the belief, that first hand experience leaves stronger memories and the skills are truly learned.
- Dagger became more complex over years - making onboarding of new engineers harder.
- Dagger became also misused or overused. New engineers see already brownfield and Dagger seems like one of the devils responsible for this leading to lower trust in the tool. Dagger can be almost invisible in your project.
- We already forgot about the problems we had before Dagger like runtime DI resolution. It is important to be reminded about these problems.
- Codelab is split into few smaller sections - each of them can be run independently.
- Each section has a branch with a format
{section number}-{area of focus}
e.g.01-basics
. The branch with higher number is always the solution of the previous section.master
branch is the final solution of present tasks. - You can run the codelab yourself, by following the instructions within each sections.
- In case of any question, please feel free to create an issue in this repo.
If you would like to see how would the current project look like in Dagger Hilt check the extra exercises:
With solution at 08a-hilt-option-solution
- Android docs,
- Google Codelab
- Dagger docs
- Android setup explains why we use field injection.
- Official tutorial
- Multibindings
@Binds
/@Provides
discussion
- Dagger Hilt
- Square - Anvil Kotlin compiler plugin to automatically generate components for the applcation modules.
- Gregory Kick - DAGGER 2 - A New Type of dependency injection (2014) - Recommended resource to understand the history of DI and why Dagger 2 exists.
- Gregory Kick - Dagger to on MCE^3 (2016) - Introduction of main Dagger features as we know them now like Multibindings and the rationale behind them.
- Josef Raska - 3 Years of Happy Marriage With Dagger 2 (2018) - Recap of which problems Dagger solved and how it moved Android development forward, even if it might not seem obvious now.
- Jake Wharton - Helping Dagger help you (2018) - Showcase how far you can go with Dagger features and improvements.