Dagger

https://medium.com/@harivigneshjayapalan/dagger-2-for-android-beginners-dagger-2-part-i-f2de5564ab25

Dependency injection

no class should instantiate another class but should get the instances from a configuration class.

Dagger

@Inject

the@Inject annotation will tell the Dagger what all the dependencies needed to be transferred to the dependant.

@Component

This annotation is used to build the interface which wires everything together.

@Component, in general, is a something like a bridge between @Module(Which we’ll be seeing later) and @Inject.

Dagger2 will implement functions we defined in interface with Component annotation according to injection associated with this function. Dagger2 use builder pattern to do it.

It can add module attribute to indicate this component contains which modules.

*Once we use module attribute, we can’t use DagggerXXXComponnet.create() cause we need to pass module dependency to dagger. So it becomes DagggerXXXComponnet.builder().xXXmodule(new XXXModule(…)).build()

@Module

mark modules/classes which can provide dependencies to other classes

@Provides

used in @module marks the methods which provide dependencies.

@Scope

@Scope annotation tells dagger to create single instance. It will make the dependency work as singleton.

To use it, we need first create a new interface(actually a custom annotation) use @interface to define

@Retention is used to describe this custom annotation, limit its usage

How to use it?

  1. create a new interface(actually a custom annotation) use @interface to define

  2. add our custom annotation to component

  3. add it to every method we need as singleton

Different context

we use ApplicationContext and Activity context, if we offer 2 context at same time, there is a problem.

we need @Named annotation to indicate Dagger choose the correct context for dependency.

  1. add @Named() before context construction

  2. we add @Named() too before where we use these contexts

Another way

@Qualifier annotation

  1. define a custom annotation with @Qualifier
  2. add our custom annotation before context construction just like @Named
  3. add this before where we use like @Named

Considering life-cycle

The best practice here is, when you’re injecting dependencies into clients who have different life-cycle from where dependencies are coming, it’s better to create a separate module and component for them.

Dependency Graph

The best practice of using a component is to expose only the top level dependency and keep other inner dependency under the hood.

top level dependencies are those who are not wanted by anyone.


  1. use inject define which dependencies we need to inject to its dependent. ex. if class A needs class B, which means A depends on B(B is A’s dependency), we need to use constructor injection.

  2. use inject to indicate these dependencies are used in which part in its dependent class. (To engage)

  3. use interface and component annotation to define a upper level provider which associate generated code and the dependencies. In this interface, we define some get function for dependent objects.

    *if we just define function for dependent objects, inject still doesn’t work, we need define corresponding functions for dependencies.

  4. click rebuilding, Dagger2 generate DagggerXXXComponnet class which help us inject dependent objects automatically.

  5. When we want to use the dependent object, we need first initialize a DagggerXXXComponnet class using create() function, then with the get function we defined in interface(generally it is inject function), we can get dependent instance.


  1. draw a Dependency graph, find top level dependencies in it
  2. create component and expose these top level dependencies
  3. separate different functions in different modules, create modules and add provides annotation for functions we need
  4. between different modules, there are also some dependent relations. So if a module needs another, we need to include it in parameters. Do it as @Module(includes = XXXModule.class) . Now we linked all modules
  5. we also need to tell component which dependencies it need, using modules attribute.