The New Dagger 2 Android Injector

How to use the new Dagger 2 Android Injector?

This article is all about implementing the new Dagger 2 Android Injector in an Android application.

In case, you are not familiar with Dagger 2, refer the below article.

Introduction to Dagger 2, Using Dependency Injection in Android

The new dagger 2 is released with the Android support module and the Android compiler.

The complete implementation can found in this project.

The above project and the below diagram are very important for the new Dagger 2 Android Injector.

From the above diagram, I will be describing till the MainActivityModule. For the complete implementation, you can check the project.

For using the new Dagger 2 Android Injector, you have to add the following dependencies:

compile "com.google.dagger:dagger:2.11"
annotationProcessor "com.google.dagger:dagger-compiler:2.11"
annotationProcessor "com.google.dagger:dagger-android-processor:2.11"
compile "com.google.dagger:dagger-android-support:2.11"

First of all, create the AppModule. This will provide the dependencies required for the overall application level.

@Module
public class AppModule {

    @Provides
    @Singleton
    Context provideContext(Application application) {
        return application;
    }

    @Provides
    @Singleton
    ApiService provideApiService() {
        return new ApiService();
    }
   @Provides
    @Singleton
    DBService provideDBService(Context context) {
        return new DBService(context);
    }
   ... // for brevity
}

Then, create the ActivityBuilder. Here, we have to use the new annotation ContributesAndroidInjector.

@Module
public abstract class ActivityBuilder {

    @ContributesAndroidInjector(modules = MainActivityModule.class)
    abstract MainActivity bindMainActivity();

}

Then, create the MainActivityModule. Here, the MainActivityModule provides the dependencies specific to the MainActivity.

@Module
public class MainActivityModule {

    @Provides
    MainViewModel provideMainViewModel(DBService service) {
        return new MainViewModel(service);
    }

}

Then, if the fragment modules are required. Refer this project for details.Then, create the AppComponent. It enables the selected modules and used for performing dependency injection.

@Singleton
@Component(modules = {AndroidInjectionModule.class, AppModule.class, ActivityBuilder.class})
public interface AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        AppComponent build();

    }

    void inject(MvvmApp app);

}

Then, when you build the project, it generates the DaggerAppComponent.After that, you have to implement the HasActivityInjector interface in your Application class and inject like below in the application class.

public class MvvmApp extends Application implements HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;

    @Override
    public void onCreate() {
        super.onCreate();

        DaggerAppComponent.builder()
                .application(this)
                .build()
                .inject(this);

    }

    @Override
    public DispatchingAndroidInjector<Activity> activityInjector() {
        return activityDispatchingAndroidInjector;
    }

}

Then, in the activity class for injecting, you have to do like below:You have to use the AndroidInjection.inject(this).

public class MainActivity extends BaseActivity {

    @Inject
    MainViewModel viewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
    }

}

Similarly, you can check the project to refer how to inject in the fragment.Believe me, the best way to learn Dagger is by playing with it. Clone, fork the above sample project and start playing with it.

Happy Learning :)

Also, Let’s become friends on Twitter, Linkedin, Github, and Facebook.