1

Worker factories are used to construct workers for WorkManager on demand (when a task is ready to run). I am using dynamic feature modules: i.e. 2 modules, :app and :feature, where :feature depends on :app, and my worker is in :feature.

To inject a worker, I need to inject its factory class, because Workers are created by android, not us. Therefore, I tried to DelegatingWorkerFactory docs to add more factories to the workManager configuration, but I cannot import the worker (or its factory) since dependencies are reversed in dynamic feature modules (:feature depends on :app): modified from here

@Singleton
class MyWorkerFactory @Inject constructor(): DelegatingWorkerFactory() {
    init {
      addFactory(Class.forName(FULLY_QUALIFIED_CLASS_NAME).getConstructor().newInstance() as WorkerFactory)
    }
}

How do I support workers/ worker factories in dynamic feature modules? It seems like dynamic feature modules are never available at start up (onCreate). Maybe this is a related issue.


WorkManager : How to set-up different WorkManager Configurations in same App may be relevant to you to gain more context. My existing workaround uses reflection to get the classname instead of ConferenceDataWorker::class.java.name, and a bit dissapointed that dynamic feature modules requires it. Reflection is said to be slow by the great Derek Banas.

I am not using Hilt. Also, apparently Hilt doesn't support dynamic feature modules.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167

1 Answers1

0

After trying reflection and ClassNotFoundExceptions, I have just removed the dynamic feature module, and bundled it with the app. Would still be interested if there is a solution.

Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167