| Start |
|---|
| Component setup for Application and Activity injection A basic A module to use together with the A subclassed application to setup dagger and the singleton component. Now an activity scoped component that depends on the And a reusable Putting everything together we're set up and can inject our activity and be sure to use the same |
Custom Scopes Scopes are just annotations and you can create your own ones where needed. |
| Constructor Injection Classes without dependencies can easily be created by dagger. This class can be provided by any component. It has no dependencies itself and is not scoped. There is no further code necessary. Dependencies are declared as parameters in the constructor. Dagger will call the constructor and supply the dependencies, as long as those dependencies can be provided. This class can be provided by every component iff this component can also provide all of its dependencies— You can use constructor injection whenever all of the dependencies can be provided by the component. A component can provide a dependency, if
|
Using @Subcomponent instead of @Component(dependencies={...}) |
| How to add Dagger 2 in build.gradle Since the release of Gradle 2.2, the use of the android-apt plugin is no longer used. The following method of setting up Dagger 2 should be used. For older version of Gradle, use the previous method shown below. For Gradle >= 2.2 For Gradle < 2.2 To use Dagger 2 it's necessary to add Then the application module's build.gradle should contain: |
| Creating a component from multiple modules Dagger 2 supports creating a component from multiple modules. You can create your component this way: The two references modules GeneralPurposeModule.java SpecificModule.java During the dependency injection phase, the component will take objects from both modules according to the needs. This approach is very useful in terms of modularity. In the example, there is a general purpose module used to instantiate components such as the Syntax
RemarksNot to confuse with dagger by square, the predecessor to dagger 2. |
Read Article
https://develop.horje.com/learn/1434/reference