Dagger 2 is a library which helps the developer to implement a pattern of Dependency Injection (one specific form of Inversion of control). Dagger build constructor arguments to inject objects at runtime uses @Qualifiers to differentiate multiple clients with same return type and @Scope to prevent multiple objects creation of the same type and make Singleton.
This is a process to provide any program component with the external dependence.
- The modules of top levels shouldn’t depend on modules of the lower levels. The modules of all levels should depend on abstractions.
- The abstractions shouldn’t depend on details. The details should depend on abstractions.
- Rigidity. If we change one module the other modules are changed too.
- Fragility. If we change one part of program the other parts will have got uncontrolled errors.
- Immobility. The single module can be hardly separated from the rest part of the application to be used again.
Here you can find full article about Inversion Of Control.
- @Inject — base annotation whereby the “dependency is requested”
- @Module — classes which methods “provide dependencies”
- @Provide — methods inside @Module, which “tell Dagger how we want to build and present a dependency“
- @Component — bridge between @Inject and @Module
- @Scope — enables to create global and local singletons
- @Qualifier — if different objects of the same type are necessary.
Need to add dependency in your Gradle.
//dagger2
compile 'com.google.dagger:dagger-android:2.11'
compile 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
// if you use the support libraries
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
//retrofit
compile 'com.squareup.retrofit2:retrofit:2.3.0'
//Gson converter
compile 'com.google.code.gson:gson:2.8.1'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'org.parceler:parceler-api:1.1.9'
annotationProcessor 'org.parceler:parceler:1.1.9'
//ok http
compile 'com.squareup.okhttp3:logging-interceptor:3.9.1'
compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
//picasso
compile 'com.squareup.picasso:picasso:2.5.2'