How to use Dagger2 with WorkManager
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
- add libs dependencies(WorkManager, Dagger, AssistedInject)
- create AppWorkerFactory class for supporting injections where Dagger2 hasn’t been able to support.
- create ChildWorkerFactory class for concrete Worker instances.
- define a concrete Worker class
- define bindings for Workers
- 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.
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.
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.
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.
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.
Step6. Use it anywhere you want to user WorkManager!!
Before you start to enjoy implementation, you need to init in Application class.
You also need to initialize WorkManager.
This is because WorkManager mechanism is using ContentProvider inside.
Add the implementation like below.
Then you can finish and you can use injected components inside WorkManager like below.
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.