Quick Dagger 2 Implementation into your Android project

Vikram Hooda
3 min readDec 1, 2018

This story isn’t about the theory part. It’s more on quick implementation of Dagger 2 into your android project. I split it into two part:-
1. Dagger 2 basics which need to know first.
2. Steps to integrate Dagger2 into an Android project.

  1. Dagger 2 basics which need to know first:
    To use Dagger 2 Android for injection into your project, quick about some annotations which you will use:
    @Component, @Module, @Singleton, @Provide:
    From Dagger 2 Android module:
    Classes: AndroidInjectionModule And AndroidSupportInjectionModule
    Interfaces: HasActivityInjector, HasSupportFragmentInjector
    To know about all these basic terms, there is a beautiful blog which covers Dagger 2 basics which I love to read. Follow here Dagger 2 Basics.
  2. Steps for quick implementation of Dagger 2 into your project:
    A.
    Add given below all Dagger 2 dependencies and sync project:-
implementation 'com.google.dagger:dagger:2.18'
implementation 'com.google.dagger:dagger-android:2.18'
implementation 'com.google.dagger:dagger-android-support:2.18'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.18'
annotationProcessor 'com.google.dagger:dagger-compiler:2.18'

Create an interface with @Component annotation for your application. This will serve as Dagger Component for your application. Use @Singleton for the scope of this component.
To inject application instance, we can have different approaches to create Dagger builder.
1st Approach: It’s the default one which uses default @Component.Builder and creates default builder under the hood.

@Singleton
@Component(modules = {AModule.class, ActivityModule.class,
AndroidInjectionModule.class, AndroidSupportInjectionModule.class })
public interface AppComponent extends AndroidInjector<ABCDofDaggerApplication> {
}

2nd Approach: Using annotation @Component.Builder on interface.

@Singleton
@Component(modules = { AModule.class, ActivityModule.class,
AndroidInjectionModule.class, AndroidSupportInjectionModule.class })
public interface AppComponent extends AndroidInjector<ABCDofDaggerApplication> {

@Component.Builder
interface AppBuilder {
@BindsInstance
AppBuilder appBuilder(ABCDofDaggerApplication application);

AppComponent buildGraph();
}
void injectApplication(ABCDofDaggerApplication application);
}

3rd Approach: Using Abstract class Builder.

@Singleton
@Component(modules = { AModule.class, ActivityModule.class,
AndroidInjectionModule.class, AndroidSupportInjectionModule.class })
public interface AppComponent extends AndroidInjector<ABCDofDaggerApplication> {

@Component.Builder
abstract class Builder extends AndroidInjector.Builder<ABCDofDaggerApplication>{ }

void injectApplication(ABCDofDaggerApplication application);
}

Now on basis of these different approaches, we inject application instance to Dagger 2 graph. To know about these approaches in details how they are working, copy above snippet and build the app. It will generate DaggerAppComponent class where you can take a look at these builders.

To inject the application instance depends on the approach you chose. In application’s onCreate() method, use this line if you chose approaches:

1st Approach:
DaggerAppComponent.builder().build().inject(this);
/*This line equivalent to below line. create() returns new Builder().build()*/
DaggerAppComponent.create().inject(this);
2nd Approach:
DaggerAppComponent.builder().appBuilder(this).buildGraph().injectApplication(this);
3rd Approach:
DaggerAppComponent.builder().create(this).inject(this);

2. Create Modules (with @Module annotation) for activity and view model class. e.g ActivityModule.java

@Module
public abstract class ActivityModule {

@ContributesAndroidInjector
abstract AActivity provideAActivity();
}

View Model’s module:

@Module
public class AModule {

@Provides
@Singleton
AAbstractViewModel provideAViewModel() {
return new AViewModel();
}
}

This AAbstractViewModel is an interface and AViewModel is class which implements an AAbstractViewModel interface. Create an interface and class java files.

public interface AAbstractViewModel {

String getName();
}
public class AViewModel implements AAbstractViewModel {

private static final String TAG = AViewModel.class.getSimpleName();

@Override
public String getName() {
return TAG;
}
}

And at the end, you need to inject your activity into the component so use this line into AActivity’s onCreate() method.

AndroidInjection.inject(this);

You are all set with Dagger 2 implementation into your android project. Similar way you can inject other activities/fragments into dagger graph(component).

Complete project of ABCDOfDagger you can download from Github.

Let’s make happy coding journey with a clap. :-)

--

--

Vikram Hooda
Vikram Hooda

Written by Vikram Hooda

Love Your Work, World will love you:- VK

No responses yet