Providing refers to the availability of an object in the Dagger graph. If something provides a class, it adds it to the graph. A class requires its dependencies to be provided in order to injected using Dagger.
-
@Inject
constructor@Inject public MyClass(Parameter p1) { this.p1 = p1; }
- Used to provide instances of MyClass
- This is the preferred way of injecting things
- Tries to inject parameters; parameters must be provided
- Requires that you assign fields manually
- Cannot be used for external dependencies
- Cannot be used in Android Application, Activity, or Fragment classes
-
@Inject
fieldsclass MyClass { @Inject Field f1; @Inject Field f2; }
- Does not allow for injecting instances of MyClass
- Injects fields directly
- All fields must be provided
- Cannot be used for external dependencies
-
@Provides
@Provides MyClass provideMyClass(Parameter p1) { return new MyClass(p1); }
- Allows for specific implementations of a class to be provided
- Works with external dependencies
- Parameters must be provided
- Must be in a Module
-
@Binds
@Binds abstract MyClass bindMyClass(Parameter p1);
- Used to provide simple instances of MyClass where the parameters of the method directly refer to the parameters required by MyClass
- Works with external dependencies. Probably would never be used for internal dependencies since if this works, then
@Inject
constructor will work too, and@Inject
constructor is preferred. - Parameters must be provided
- Must be in a Module
-
@BindsInstances
@Component.Builder interface Builder { @BindsInstance Builder foo(Foo foo); @BindsInstance Builder bar( @Blue Bar bar); } // or @Component.Factory interface Factory { MyComponent newMyComponent( @BindsInstance Foo foo, @BindsInstance @Blue Bar bar ); }
- Not entirely sure how this one works tbh
-
AndroidInjection.inject()
(Android only)class MyActivity extends Activity { public void onCreate(Bundle savedInstanceState) { AndroidInjection.inject(this); super.onCreate(savedInstanceState); } }
- Used to inject dependencies into an Activity or Fragment.
- Requires a
Subcomponent
that extendsAndroidInjector
with this activity as the generic. - In an Activity, it must be called in onCreate, before the super call.
- In a Fragment, it must be called in onAttach. It does not matter whether it is called before or after the super call.
-
AndroidInjector
(Android only)@Subcomponent interface MyActivitySubcomponet extends AndroidInjector<MyActivity> { @Subcomponent.Factory public interface Factory extends AndroidInjector.Factory<MyActivity> {} }
- Use this to enable
AndroidInjection.inject(this)
on an Activity. - Require a base Application that implements
HasAndroidInjector
- Use this to enable
-
@ContributesAndroidInjector
(Android only)@Module abstract class MyModule { @ContributesAndroidInjector abstract MyActivity contributeMyAndroidInjector(); }
- Generates an AndroidInjector Subcomponent within a Module.
- Accepts Module dependencies
-
DaggerActivity
(Android only)class MyActivity extends DaggerActivty { @Override void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
- Removes the need to call AndroidInjection.inject(this);
- Implements HasAndroidInjector, so it provides an injector for all children (Fragments, etc.)
-
DaggerAppCompatActivity
(Android only)- see DaggerActivty
-
DaggerApplication
(Android only)- see DaggerActivity
- provides AndroidInjector at Application level
- still needs AppComponent
public class MyApplication extends DaggerApplication { @Override protected AndroidInjector<? extends AndroidSampleApp> applicationInjector() { return DaggerMyApplicationComponent.builder().create(this); } }
DaggerMyApplicationComponent
is a generated class. This will probably show as an error until you compile the project as Dagger is a compile-time dependency.
-
DaggerFragment
(Android only)- see DaggerActivity
-
DaggerAppCompatFragment
(Android only)- see DaggerActivity
-
@Module
@Module abstract class MyClassModule { @Binds abstract MyClass1 bindMyClass1(); @Provides MyClass2 provideMyClass2(Parameter p1) { return new MyClass2(p1); } }
- Container for @Binds and @Provides methods
- Needs a Component to access non-static methods
- May inherit from other Modules
- Modules are usually abstract classes
-
@Component
@Component(modules = {MyClassModule.class}) interface MyClassComponent { }
-
Allows you access non-static methods in a Module.
-
To create an instance, you must use the generated builder:
MyClassComponent myClassComponent = DaggerMyClassComponent.builder() .myClassModule(new MyClassModule()) .build();
-
If all dependencies have 0-arg constructors, then you can also do:
MyClassComponent myClassComponent = DaggerMyClassComponent.create()
-
Components are usually interfaces
-
-
@Subcomponent
- They're basically just components that are children of other components. May be attached to modules in the Module annotation.
- Typically set up like this (I think):
@Subcomponent interface MySubComponent { } @Module(subcomponents = {MySubComponent.class}) abstract class MyModule { } @Component(modules = {MyModule.class}) interface MyComponent { }
- Subcomponents are usually interfaces
-
HasAndroidInjector
(Android only)public class YourApplication extends Application implements HasAndroidInjector { @Inject DispatchingAndroidInjector<Object> dispatchingAndroidInjector; @Override public void onCreate() { super.onCreate(); DaggerYourApplicationComponent.create() .inject(this); } @Override public AndroidInjector<Object> androidInjector() { return dispatchingAndroidInjector; } }
- Used to allow injection into and Android project at the Application level.
- May also be used in Activities or Fragments
- Implementation must override androidInjector() and supply and instance of DispatchingAndroindInjector.
- This is standard when using the dagger-android library.
- A class may both implement HasAndroidInjector and use AndroidInjection.inject().
-
Lazy
Lazy<MyClass> getMyClass();
- Provides a single instance of a class. The instance is initialized until the first call to the method.
-
Provider
Provider<MyClass> getMyClass();
- Provides a new instance of a class everytime this is called.
- This one's especially helpful for dagger-android-support: https://github.com/iammert/dagger-android-injection/tree/dagger-support-lib (Related: https://android.jlelse.eu/new-android-injector-with-dagger-2-part-3-fe3924df6a89) However, note that it is a bit old and uses the deprecated Component.Builder instead of Component.Factory.
- There's a few branches on this repo that use Dagger. Note that it's in Kotlin and some things won't apply to Java. https://github.com/android/architecture-samples
- This sample also uses Dagger (and is also in Kotlin): https://github.com/android/architecture-components-samples/tree/master/GithubBrowserSample
- The Dagger website is also very helpful, though it lacks details with regards to dagger-android-support. https://dagger.dev/dev-guide/
- Using Dagger in Android
- Dagger basics