How to use Dagger2 with WorkManager

Yuki Nanri
2 min readSep 19, 2019

--

This article helps you understand how to used Dagger2 with WorkManager in Android.

My bios are here
Sample Codes are here.

What is Dagger2?

Dagger2 is dependency injection library for Android developed by Google.
There are other equivalent libs, but Dagger is still widely used.

What is WorkManager?

WorkManager is Android Framework class, which takes responsibility for scheduling tasks.

Motivation

Dagger is dependency injection but there are some problems when it comes to injection for Android Framework.

Activity, Fragment, ContentProvider, BroadcastReceiver, Service are that kind of Framework.

Dagger supports all the Android Framework components above, but not WorkManager.
This is because WorkManager is newly added to Android Framework compared to other Android Framework classes.

So I’ll talk about how to handle with dependency injection for WorkManager with Dagger.

Steps

  1. add libs dependencies(WorkManager, Dagger, AssistedInject)
  2. create AppWorkerFactory class for supporting injections where Dagger2 hasn’t been able to support.
  3. create ChildWorkerFactory class for concrete Worker instances.
  4. define a concrete Worker class
  5. define bindings for Workers
  6. Use it anywhere you want to user WorkManager!!

Step1. add libs dependencies(WorkManager, Dagger, AssistedInject)

First, add the dependencies for libraries.
Let’s see the code.

app build gradle

There are no difficult points we cannot understand.
This is just following the way that each of libraries provides build.gradle statement.

AssistedInject is literally assisting the Dagger DI.

In order to allow Dagger to use the generated factory, define an assisted dagger module anywhere in the same gradle module:

Step2. create AppWorkerFactory class for supporting injections where Dagger2 hasn’t been able to support.

Let’s create AppWorkerFactory class.
This class is the base of passing Worker parameters injected into WorkManager.
You can just copy and paste this code below.

settings for WorkManager Injection

You should get errors because you haven’t created the ChildWorkerFactory yet.

Step3. create ChildWorkerFactory class for concrete Worker instances.

ChildWorkerFactory is in charge of making a concrete Worker with AssistedInject lib.

create interface(ChildWorkerFactory)

Step4. define a concrete Worker class

Then, you can create a concrete Worker class. (sample is BisonWorker)
For this sample, Repository is a class which I would like to inject into BisonWorker class from outside.

Worker settings

All annotations with Assisted are from AssistedInject library.
As I mentioned, Dagger injection doesn’t support injection for Worker, so AssistedInject complements that part.

Step5. define bindings for Workers

Let’s define all the bindings with Workers.
AssistedInject_AppAssistedInjectModule is auto-generated after compiling.
Other things are all the similar to Dagger.

DI settings

Step6. Use it anywhere you want to user WorkManager!!

Before you start to enjoy implementation, you need to init in Application class.

Init Dagger and WorkerFactory

You also need to initialize WorkManager.
This is because WorkManager mechanism is using ContentProvider inside.

WorkManger initialization

Add the implementation like below.

WorkManager use in MainActivity

Then you can finish and you can use injected components inside WorkManager like below.

Success of constructor injection

Put AssistedInject annotation before constructor and Assisted annotation before pre-defined arguments.(Context, WorkerParameters)

You don’t have to put any annotations before Repository in constructor and finally Repository instance is correctly injected!!

Enjoy your Dagger & WorkManager life.

--

--