Dagger 2 Dependency Injection for Android Developers

Some Background and Alternative Material

While Reading The Code in this Post

The Basics of Dagger 2

Constructing Dependencies

/**
* A module that provides parts of a given subsystem.
*/
@Module
public class SubsystemModule {
@Provides
public SubsystemPartOne provideSubsystemPartOne() {
return new SubsystemPartOneImplementation();
}
@Provides
public SubsystemPartTwo provideSubsystemPartTwo() {
return new SubsystemPartTwoImplementation();
}
}
@Module
public class DependencyModule {
// The secondary dependency will be generated and passed in
// by dagger automatically.
@Provides
public Dependency provideDependency(
SecondaryDependency secondaryDependency) {
return new DependencyImplementation(secondaryDependency);
}
// Dagger will construct the secondary dependency
// using this method.
@Provides
public SecondaryDependency provideSecondaryDependency() {
return new SecondaryDependencyImplementation();
}
}
@Module
public class DependencyModule {
public RuntimeParameter runtimeParameter; public DependencyModule(RuntimeParameter runtimeParameter) {
this.runtimeParameter = runtimeParameter;
}
@Provides
public Dependency provideDepedency() {
return new Dependency(runtimeParameter);
}
}

Components and the Object Graph

The Object Graph

Component Definition

/**
* Provides a dependency out of module one.
*
* This is a case where ModuleOneDependency has a secondary
* dependency on a class provided by DependencyModulesTwo.
*
* The component ties the two modules together so that the
* secondary dependency can be resolved. The component then
* exposes the dependency it provides via method signature.
*/
@Component(modules = {
DependencyModuleOne.class,
DependencyModulesTwo.class
}
public interface DependencyComponent {
ModuleOneDependency moduleOneDependency();
}
DaggerDependencyComponent.class
DaggerDependencyComponent.builder()
// Creating a component from the dagger generated builder.
DependencyComponent dependencyComponent =
Dagger_DependencyComponent.builder()
.dependencyModuleTwo(
new DependencyModuleTwo(
RuntimeParameter runtimeParameter)
.build();
// Fetching dependencies out of the component.
ModuleOneDependency moduleOneDependency = dependencyComponent
.moduleOneDependency();

Automated Injection

@Component()public interface DependencyComponent {
Dependency moduleOneDependency();
void inject(DependencyReceiver dependencyReciever);
}
public class DependencyReceiver {
@Inject Dependency dependency;
public DependencyReceiver() {
...
}
}
public class ControllerClass { DependencyReceiver dependencyReceiver; public ControllerClass() {
DaggerDependencyComponent.Builder()
.build()
.inject(dependencyReceiver);
}
public class DependencyReceiverActivity extends AppCompatActivity {    @Inject
Dependency dependency;
@Override
public void onCreate(Bundle savedInstanceState) {
DaggerDependencyComponent.Builder()
.build()
.inject(this);
}
}

Component Interactions

@Component(
modules = {
...
}
)
public interface DependencyComponent {
DependencyOne dependencyOne();
DependencyTwo dependencyTwo();
DependencySubcomponent dependencySubcomponent(
DependencyModule dependencyModule);
}
public class DependencyReceiverActivity extends AppCompatActivity {// comes from the subcomponent
@Inject
Dependency dependency;
// comes from the subcomponent's parent, even if not explicitly
// exposed by the subcomponent's parent
@Inject
ParentComponentDependency ParentComponentDependency;
@Override
public void onCreate(Bundle savedInstanceState) {
DaggerDependencyComponent.Builder()
.build()
.subcomponent(DependencyModule module)
.inject(this);
}
}
@Component (
dependencies = {
ParentComponent.class
}
)
public interface Component {
ParentComponentProvidedDependency
parentComponentProvidedDependency();
}

Scopes

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Randall Mitchell

Randall Mitchell

More from Medium

How to switch from one activity to another in Android Studio

Kotlin vs Java for android development: Who wins the battle?

Kotlin Android For Beginners

Kotlin⚡️: Loops | Erselan Khan