Using Dagger 2 to manage dependencies
In this section, we will analyze the Dagger 2 library, how it handles DI, how it works, how it is integrated into an Android application, and what issues it might create.
The Dagger 2 library relies on code generation based on annotation processing, which will generate the boilerplate code that is required to perform DI. The library is written in Java, and it is used for various projects outside of Android applications. Because it is written in Java, it provides compatibility for apps written in Java, Kotlin, or both. The library is built using Java Specification Requests (JSR) 330, which provide a set of useful annotations for DI (@Inject
, @Named
, @Qualifier
, @Scope
, and @Singleton
).
When integrating Dagger 2, there are three main concepts that we will need to consider:
- Provider: This is represented by the classes responsible for providing the dependencies, using the
@Module
annotation for the classes and@Provides
for the methods...