This document provides an overview of Dagger 2 for dependency injection in Android applications. It discusses key concepts like components and modules, dependency injection via constructor parameters versus field injection, and best practices like favoring constructor injection and static @Provides methods. It also mentions alternatives like Koin for dependency injection in Kotlin applications and recommends some resources for further reading on using Dagger 2 in Java and Kotlin projects.
45. Favor constructor injection
over field injection
● Field injection requires non final and non private fields
● Forget an @Inject - get NullPointerException on a field
● Constructor injection = immutable and thread safe
objects. Protect from partially constructed state
● For objects, constructed by the system (Activities,
Fragments) - still use field injection
48. Favor @Inject over @Provides
● @Provides methods should not duplicate the
constructor boilerplate
● Code is easier to understand when coupled concerns
are in one place
50. Singletons should be extremely rare
● Singletons are useful when we need a centralized access
to a mutable state
● Immutable objects doesn’t need to be a singleton
52. Favor static @Provides methods
● The generated code can directly invoke the method
instead of having to create a module instance. That
method call can be inlined by the compiler
● All bindings being static will result in a sizable
performance increase
● Make your modules abstract and Dagger will fail at
compile time if one of the @Provides methods isn’t static
56. Home readings for Java
● Py “Keeping the Daggers Sharp”
● Mert Simsek “New Android Injector with Dagger 2”
● James Shvarts “Demystifying the new Dagger Android
Injection API”
● Fernando Cejas “Tasting Dagger 2 on Android”
● Miroslaw Stanek “Dagger2 - graph creation
performance”
● CodePath’s guide to Dagger2
● Xoxol_89 @ Habrahabr “Dagger 2. Часть третья.
Новые грани возможного”
● Dagger2 official page
57. Home readings for Kotlin
Mobile Architecture Club
● Juan Ignacio Saravia, Keddit
https://android.jlelse.eu/keddit-part-10-kotlin-dagger-
2-dependency-injection-a620052a7384
● Antonio Leiva
https://antonioleiva.com/dagger-android-kotlin/
● Koin https://github.com/Ekito/koin