One of the central difficulties of writing an Android application using Dagger is that many Android framework classes are instantiated by the OS itself, like Activity and Fragment, but Dagger works best if it can create all the injected objects. Instead, you have to perform members injection in a lifecycle method.
2. Agenda
* Assumed you know Dagger and have used it.
● About Dagger 2
○ Dagger 1
○ Dependency Graph
● Before Dagger 2.10
● Android Injector
○ What is the issue
○ How to use
● More advanced way for Android Injector
3. About Dagger 2 - Dagger 1
You might have heard about Spring[1]
and Guice[2]
they are former Dependency
Injection solution. But Dagger 1[3]
is a fast dependency injector for Java (of course
and Android) made by Square. It spends much more effort in compile time to
increase efficiency. (Now already deprecated, use Dagger2 so)
4. About Dagger 2 - Dependency Graph
I think to understand Dagger and use it properly let’s see a notion of Dependency
Injection[4]
and what it can conduct.
Design principle: Inverse of Control[5]
(Dependency Injection)
5. About Dagger 2 - Dependency Graph
Imagine dependency and provider are two nodes and there should be a directed
edge.
Once you have done of many dependency injections in your application it will
become
A B
6. About Dagger 2 - Dependency Graph
In Dagger 2[6]
, as you known @Component, @Module, and @Scope help you to
make a good dependency graph.
There is actual tool[7]
to generate image of Dagger dependency graph: https://github.com/dvdciri/daggraph
Component and Scope
Module
dependent
7. Before Dagger 2.10
Before the version 2.10 of Dagger 2, you can just say Dagger is a tool for
application based on Java. But from version 2.10[8]
Dagger starts providing some
stuffs that more easy to build your Android application with Dagger.
We are used to do old Dagger like this:
8. Before Dagger 2.10
We have to do inject for any class where needs dependency injection. Normally
we create Application Component and Subcomponent for Activity or Fragment.
If use after 2.10 you can just
AndroidInjection.inject(this); // Or even none of statement
9. Android Injector
Let’s say we are going to build a graph like this:
For instance, If app module provides `ApiService`,
we will have `ApiService` instance when we build
component which has app module.
12. Android Injector
@Component and @Component.Builder for building the top component.
AndroidSampleApp
@Provides
Application provideApplication();
or
@Inject Application app
or
@Provides
Context provideContext(Application app);
AppComponent
14. Android Injector - What is the issue
In MainActivity
Why AndroidSampleApp is being here? It looks violate some design principle,
right? Put a thing not related with the class. And you need to every time it sounds
not a good idea.
15. Android Injector - How to use it
You can install the Dagger as usual or you only want to use dagger.android[9]
module then you can
16. Android Injector - How to use it
Update the SubComponent extens AndroidInjector with your Activity.
It allow Android Injection Module knows where to inject.
17. Android Injector - How to use it
Provide a bind module to for Android Injection Module[10]
. (You can put all needed
to bind Activities here)
@Module
public abstract class ActivityBuilder {
@Binds
@IntoMap
@ActivityKey(MainActivity.class)
abstract AndroidInjector.Factory<? extends Activity> bindMainActivity(MainActivityComponent.Builder builder);
@Binds
@IntoMap
@ActivityKey(DetailActivity.class)
abstract AndroidInjector.Factory<? extends Activity> bindDetailActivity(DetailActivityComponent.Builder builder);
}
18. Android Injector - How to use it
Application implements a helper interface: HasActivityInjector
19. Android Injector - How to use it
Finally in your Activity, you can just
Then you can get your dependencies, no need to know Component or
SubComponent.
20. Android Injector - How to use it
For Fragment, as well as in Application you should implement a helper interface
on your Activity
In your Fragment, you can just:
21. More advanced way for Android Injector
Now you know the graph is like this
From: New Android Injector with Dagger 2 [11]
22. More advanced way for Android Injector
We can simplify the as you can see the SubComponents just like bridge between
Modules and Activities (If there is no other methods), so it can be like this with
@ContributesAndroidInjector
From: New Android Injector with Dagger 2 [11]
23. More advanced way for Android Injector
Rid SubComponents and bind methods in ActivityBuilder, then create the methods
instead and replace the modules declaration from SubComponent in the
annotation @ContributesAndroidInjector like this,
You can do the same for your fragments.
24. More advanced way for Android Injector
One more, remember in your Application there is DispatchingAndroidInjector
25. More advanced way for Android Injector
If your application can extend DaggerApplication then you don’t need to depend
on it