Android Dependency Injection Using Dagger 2

Dagger 2 is a dependency injection library that relies heavily on annotation preprocessing to automatically generate a series of classes that provide dependencies to various parts of your application. The preprocessing occurs as part of compilation. The class files that are created are human readable — some are meant to be referenced just like any other java classes, others should just silently do their magic in the background. Dagger 2 uses the following annotations: @Module and @Provides: define classes and methods which provide dependencies @Component: enable selected modules and used for performing dependency injection @Inject: request dependencies. Can be used on a constructor, a field, or a method Module and Providers Get your bean ready with @Provides annotation on method level and remember to …

Back to Top