Upgrade Android WorkManager 2.5.0 to 2.6.0

Peter Nagy
1 min readSep 9, 2021

If you have a project where do you used Android WorkManager 2.5.0 or less version and now you updated it to latest stable version (to 2.6.0) you will be surprised. You can find this kind of error in your LogCat:

WorkerFactory: Could not instantiate com.xyz.MyWorker
java.lang.NoSuchMethodException: com.xyz.MyWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:2332)
at java.lang.Class.getDeclaredConstructor(Class.java:2170)
at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:244)
at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:136)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:923)

In my project I am using Dagger, it will initialise WorkManagerFactory. So what is wrong ? If you have a look documentation:

https://developer.android.com/jetpack/androidx/releases/work#version_260_3

You can find the answer -> "WorkManager now uses androidx.startup to initialize WorkManager. If you used tools:node="remove" the ContentProvider being used to initialize WorkManager in the past, then you need to do the following instead."

The problem is how WorkManager is initialised.

I used this initialisation in my AndroidManifest:

<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove"/>

And after the upgrade android:name=”androidx.work.impl.WorkManagerInitializer” line was underline with red colour.

WorkManagerInitializer was moved into other namespace. So this is one of the route of the problem.

And one more thing I am using androidx.startup too in my project, my solution was I put another meta-data into the provider:

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">

<meta-data android:name="com.xyz.TimberInitializer"
android:value="androidx.startup" />

<meta-data
android:name="androidx.work.WorkManagerInitializer"
android:value="androidx.startup"
tools:node="remove" />
</provider>

And after it was working without error.

Thanks for reading my article! If you found this post useful? Kindly tap the 👏 button below and follow :) .

--

--