Tutorial by Examples

A basic AppComponent that depends on a single AppModule to provide application-wide singleton objects. @Singleton @Component(modules = AppModule.class) public interface AppComponent { void inject(App app); Context provideContext(); Gson provideGson(); } A module to use to...
@Scope @Documented @Retention(RUNTIME) public @interface ActivityScope { } Scopes are just annotations and you can create your own ones where needed.
Classes without dependencies can easily be created by dagger. public class Engine { @Inject // <-- Annotate your constructor. public Engine() { } } This class can be provided by any component. It has no dependencies itself and is not scoped. There is no further code necessar...
@Singleton @Component(modules = AppModule.class) public interface AppComponent { void inject(App app); Context provideContext(); Gson provideGson(); MainActivityComponent mainActivityComponent(ActivityModule activityModule); } @ActivityScope @Subcomponent(modules = Act...
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 dependencies { // apt command comes from the android-apt plu...
Dagger 2 supports creating a component from multiple modules. You can create your component this way: @Singleton @Component(modules = {GeneralPurposeModule.class, SpecificModule.class}) public interface MyMultipleModuleComponent { void inject(MyFragment myFragment); void inject(MyServic...

Page 1 of 1