Not to confuse with dagger by square, the predecessor to dagger 2.
Component setup for Application and Activity injection
A basic AppComponent that depends on a single AppModule to provide application-wide singleton objects.
A module to use together with the AppComponent which will provide its singleton objects, e.g. an instance of Gson to reuse throughout the whole application.
A subclassed application to setup dagger and the singleton component.
Now an activity scoped component that depends on the AppComponent to gain access to the singleton objects.
And a reusable ActivityModule that will provide basic dependencies, like a FragmentManager
Putting everything together we're set up and can inject our activity and be sure to use the same Gson throughout out app!
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—Engine in this case. Since Engine can also be constructor injected, any component can provide a Car.
You can use constructor injection whenever all of the dependencies can be provided by the component. A component can provide a dependency, if
it can create it by using constructor injection
a module of the component can provide it
it can be provided by the parent component (if it is a @Subcomponent)
it can use an object exposed by a component it depends on (component dependencies)
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 and SpecificModule can then be implemented as follows:
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 Retrofit object (used to handle the network communication) and a PropertiesReader (in charge of handling configuration files). There is also a specific module that handles the instantiation of specific controllers and service classes in relation to that specific application component.
Scopes are just annotations and you can create your own ones where needed.
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 android-apt plugin, add this to the root build.gradle:
Then the application module's build.gradle should contain: