I am using Android's WorkManager for background tasks and everything works great until I am trying to use it in a minified build. When the service should be initialized I am getting the following exception:
Could not instantiate *REDACTED*Worker
java.lang.NoSuchMethodException: *REDACTED*Worker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
at java.lang.Class.getConstructor0(Class.java:2328)
at java.lang.Class.getDeclaredConstructor(Class.java:2167)
at e1.y.b(SourceFile:65)
at f1.j.k(SourceFile:358)
at f1.j.run(SourceFile:17)
at n1.i$a.run(SourceFile:3)
I have followed the required steps for Custom WorkManager Configuration and Initialization, but still getting this issue for minified build.
I am using Dagger2 (not Hilt) and RxWorker
I am providing a custom WorkerFactory implementation.
Gradle:
...
// workVersion is 2.7.1
implementation "androidx.work:work-rxjava2:$workVersion"
implementation "androidx.work:work-runtime-ktx:$workVersion"
<application
>
...
<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="remove" />
I found a similar question which refers to older version and suggests to add the following to the proguard file:
-keepclassmembers class * extends androidx.work.Worker {
public <init>(android.content.Context,androidx.work.WorkerParameters);
}
But that doesn't solve the issue.
After debugging this issue I found out that my custom factory was invoked, but it threw an exception that I caught inside a try-catch block (thus I didn't see it in the logs) and returned null
. When the custom factory returns null, it falls back to use reflection.