
New Android Injector with Dagger 2 — part 1
Dagger 2.10 released with android support module and android compiler. I think this was a huge change for us and all android developers should switch to new dagger android injection as soon as possible.
Before I start to explain new AndroidInjector class and dagger 2.11 library, If you are not familiar and never used dagger 2 before, I highly recommend you to read dagger 2 tutorials and understand what dependency injection is. Why am I saying that? Because android-dagger is all about annotations and I think It’s learning curve is a bit hard. In my opinion, Dagger 2 and dependency injection should be understood before we use dagger-android. Here is tutorials and blogpost about dependency injection and dagger 2 version. Blog 1 and Blog 2 about Dagger 2.
Old Way
Before Dagger 2.10 version, I used to use dagger 2 like,
((MyApplication) getApplication())
.getAppComponent()
.myActivity(new MyActivityModule(userId))
.build()
.inject(this);What is wrong with that? We want to use dependency injection. But what is dependency injection’s core principle?
A class shouldn’t know anything about how it is injected.
So we have to get rid of these builder methods and module instance creation.
Sample Project
I created sample project that does nothing. Yes. I wanted to keep it as simple as possible. It has MainActivity and DetailActivity. Both activities inject to their presenter implementations and make api call(not actually http call, I created a fake method.).
Setup
Add these dependencies to your build.gradle.
compile 'com.google.dagger:dagger:2.11-rc2'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11-rc2'
compile 'com.google.dagger:dagger-android-support:2.11-rc2'Project Package Structure

Application class build a graph using AppComponent. AppComponent has @Component annotation top of its class. When AppComponent is build with its modules, we have a graph with all provided instances in our graph. For instance, If app module provides ApiService, we will have ApiService instance when we build component which has app module.
If we want to attach our activity to dagger graph to get instances from ancestor, we simply create a @Subcomponent for it. In this case, DetailActivityComponent and MainActivityComponent classes are marked with @Subcomponent annotation. Then, last step we have to take, we need to tell ancestor about subcomponent info. So all subcomponents have to be known by its ancestor.
Don’t worry. I will explain @Subcomponent, @Component and what DispatchActivity means. I just wanted to give a warm welcome to you by component/subcomponent.
@Component and @Component.Builder
@Component(modules = {
AndroidInjectionModule.class,
AppModule.class,
ActivityBuilder.class})
public interface AppComponent {
@Component.Builder
interface Builder {
@BindsInstance Builder application(Application application);
AppComponent build();
}
void inject(AndroidSampleApp app);
}@Component: Component is a graph. We build a component. Component will provide injected instances by using modules.
@Component.Builder: We might want to bind some instance to Component. In this case we can create an interface with @Component.Builder annotation and add whatever method we want to add to builder. In my case I wanted to add Application to my AppComponent.
Note: If you want to create a Builder for your Component, your Builder interface has to has a build(); method which returns your Component.
Inject Into AppComponent
DaggerAppComponent
.builder()
.application(this)
.build()
.inject(this);As you see from code, we can bind our application instance to our Dagger graph.
I think we understood the concept behind @Component.Builder and @Component. Now I want to give skeleton structure of our project.
Component/Module Skeleton
We can think apps in three layer while using Dagger.
- Application Component
- Activity Components
- Fragment Components






