Dagger2 Cheat Sheet #1

Murat Karaöz
5 min readSep 10, 2018
  1. What is dependency injection?
  2. What is Dagger2?
  3. Should I use it everywhere?
  4. Dagger Fundamentals: What is Inject, Module, Provider, Component?
  5. What is constructor injection?
  6. What is Field Injection?
  7. What is Method Injection?
  8. How do I add Dagger to an Android project?
  9. Can I use base classes in inject methods of Component interface?
  10. What is @Named annotation?
  11. I did not like @Named annotation, is there an alternative?
  12. How many Module classes do I need?
  13. Does Dagger support Assisted Injection?
  14. Is there a basic example project?

Check out Part 2 for more.

Image from https://distillery.com

1. What is dependency injection?

“Dependency Injection” is a 25-dollar term for a 5-cent concept. It means giving an object its instance variables. Really. That’s it.

3. Should I use it everywhere?

[This one is subjective]

DI frameworks should be used in order to resolve critical dependencies in “top level” components (Service, Activity, Fragment, etc.) right after creation (because you can’t use “constructor” injection in Android’s “top-level” components), and that’s it.

4. Dagger Fundamentals: What is Inject, Module, Provider, Component?

@Inject: Marks those dependencies which should be provided by Dependency Injection framework.

@Module: Marks classes which provide dependencies.

@Provides: Used inside module classes to mark methods that return dependencies.

@Component: Used to build an interface that Dagger will use to generate the code that will do the dependency injection for us.

5. What is constructor injection?

  • Mark your constructor with @Inject annotation and all of its dependencies are provided by Dagger. Best way to use dependency injection.
  • In a class you can only mark one constructor with @Inject.
  • An injected constructor magically makes the class injectable too!

6. What is Field Injection?

Mark a field with @Inject annotation and Dagger will initialize the field for you. An injected field cannot be final or private because Dagger must be able to assign it a value outside of the class.

Image from @laaptu9
Image from @laaptu9

7. What is Method Injection?

If a method is markes with @Inject annotation all of its parameters are provided by Dagger. Only valid use case is passing this instance to one of the injected parameters.

8. How do I add Dagger to an Android project?

Basic Dagger dependencies for gradle are:

implementation 'com.google.dagger:dagger-android:2.11'
implementation 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1'

If you want to use dagger-android components:

implementation 'com.google.dagger:dagger:2.14.1'
implementation
'com.google.dagger:dagger-android-support:2.13'
annotationProcessor
'com.google.dagger:dagger-compiler:2.14.1'
annotationProcessor
'com.google.dagger:dagger-android-processor:2.13'

9. Can I use base classes in inject methods of Component interface?

Inject method has a strong type association with the injection target so you cannot. If you want a work around you can check out Annoyance #1 on Dagger 2: Even sharper, less square post.

10. What is @Named annotation?

Sometimes the type alone is insufficient to identify a dependency. For example if you need a Refrofit instance with GsonConverterFactory and another one ScalarConverterFactory you will end up with 2 provide methods that have the same return type: Retrofit. In this case, you can use @Named annotation to differentiate two Retrofit instances.

Then when you need the Retrofit you can request it with its name.

11. I did not like @Named annotation, is there an alternative?

You can create your own qualifiers annotations and use them.

12. How many Module classes do I need?

Google creates a module for each Activity.

An alternative is to create a module for app level dependencies, a module for Activities, a module for services… you get the idea. Then you can create component interfaces for module groups.

13. Does Dagger support Assisted Injection?

No. Assisted injection means you can send a method parameter to a provides annotated method that is not supplied by Dagger. In the below example first method provides a Validator instance. Second method provides a Util instance using Validator instance. When a Util is needed dagger automatically finds the Validator because it knows a method that provides a Validator instance. But the 3rd methods is not valid because Dagger does not know where to get the String object.

// Module class
@Provides
@Singleton
Validator provideValidator() {
return Validator.getInstance();
}
@Provides
Util provideUtil(Validator validator) {
return new Utils(validator);
}
@Provides
Util provideUtil(Validator validator, String address) {
return new Utils(validator, address);
}

Subject discussed here:

14. Is there a basic example project?

Try these

[Turkish] Blog post and repo:

[English] Blog post and repo:

--

--