I am writing an Android library that uses WorkManager. Somewhere in the code I call something like this:
val uploadTripRequest = OneTimeWorkRequest.Builder(UploadTripWorker::class.java)
.setConstraints(someConstraints)
.setBackoffCriteria(BackoffPolicy.EXPONENTIAL, OneTimeWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)
.build()
WorkManager.getInstance(context)
.enqueueUniqueWork(WORKER_NAME, ExistingWorkPolicy.REPLACE, uploadTripRequest)
In my build.gradle I have: implementation 'androidx.work:work-runtime:2.2.0'
Normally, everything works perfectly. Problems come when the client app calls https://developer.android.com/reference/kotlin/androidx/work/WorkManager.html#initialize, for example:
WorkManager.initialize(context, Configuration.Builder().setWorkerFactory(myWorkerFactory).build())
When this is called, WorkManager from my library is not working as expected. doWork
method is not called on my UploadTripWorker
, but it is executed on the worker provided by client's myWorkerFactory
.
From my perspective, the WorkManager
is unusable in the library, since it is a singleton and someone can change its behaviour via initialize
method. But I hope I am wrong.
Is there any way I can solve this problem? Of course, I can't say to all my clients not to use WorkManager.initialize()
method.