The learning curve for Dependency Injection with Dagger is too steep. Dagger 2.11 brings us a big improvement, save us from a lot of boilerplate code and makes it even harder to learn :) . I would like to help you to implement the new API.

Before we start:

I assume that you already know about MVP and Dagger.

We will make a simple application with only one Activity that will load Mock simple data from Model (Repository).

All code from this article you can find on github.

Let’s start with making a new project and importing libraries that we need:

Once, when you make project go to build.gradle (app) and add dependencies for Dagger 2.11:

note: Probably, you will need to add this line too:

androidTestCompile 'com.google.code.findbugs:jsr305:3.0.1'

As you can see, we have three new dependencies for Dagger Android. Third one provides us with DaggerApplication, DaggerActivity, DaggerFragment etc. so we don’t have to think about injection in our activities and fragments at all. At the end I will show you how it works. In Dagger Android our activities and fragments don’t know how things are injected. That is (finally) right way to do DI.

Make main component, AppComponent:

Module AndroidSupportInjectionModule is required by Dagger Android and It always has to be added to the main component in your project.

AndroidInjector will be explained later.

Application instance is available at the time we are building AppComponent, so we can bind it using Builder:

@Component.Builder

interface Builder {



@BindsInstance

AppComponent.Builder application(Application application);



AppComponent build();

}

Let’s now make MyApplication class that extends DaggerApplication:

Note: don’t forget to specify the name property in the the application tag node in AndroidManifest.xml:

<application

android:name=".MyApplication"

android:allowBackup="true"

... >

...

</application>

As we extend DaggerApplication, we must override:

protected AndroidInjector<? extends DaggerApplication> applicationInjector()

Application Injector = the main component AppComponent. So this is the reason why AppComponent extends AndroidInjector <DaggerApplication> .

Let’s make AppModule that will bind application context, cause we should not downstream application:

Add this Module to AppComponent:

@Singleton

@Component(modules = {AndroidSupportInjectionModule.class,

AppModule.class})

public interface AppComponent extends AndroidInjector<DaggerApplication> { ...

We will need PerActivity scope annotation:

@Scope

@Retention(RUNTIME)

public @interface PerActivity {}

And the simplest contract for MVP for MainActivity:

public interface MainContract {



interface View {

void showNewText(String newText);

}



interface Presenter {

void subscribe();

void unsubscribe();

}

}

Implementation of Presenter:

MainContract.View, that we made, has to be implemented by MainActivity:

It is the simplest case where you just have to set text.

Now, we have to make two more modules MainActivityModule that will take MainContract.View and provide PresenterImpl and ActivityBindingModule that will be responsible for binding all activities in application:

Note that MainActivityModule will live in @PerActivity scope. Also note that we have to add ActivityBindingsModule to AppComponent and ActivityBindingsModule will live in @Singleton scope.

Now we have prepared everything that we need to inject Presenter:

There is no code about injecting in activity cause we are extending DaggerAppCompatActivity from Dagger support library. That class does injecting for us.

That’s all. I hope you liked it. :)

References:

https://github.com/googlesamples/android-architecture/tree/todo-mvp-dagger/