WorkManager - Unable to create Worker with custom argument and Hilt

Viewed 17

I am trying to make a Worker with custom constructor arguments. Moreover, the Worker is in a different module and not in the app module. Here is my code:

Module - order

@HiltWorker
class OrderFcmWorker @AssistedInject constructor(
    @Assisted context: Context,
    @Assisted params: WorkerParameters,
    private val userRepo: UserRepo            // This creates the problem
) : CoroutineWorker(context, params) {
    // Worker code goes here
}

Because of the additional argument, I get below error.

E  Could not instantiate io.chanse.locals.homeservices.android.customer.work.OrderFcmWorker
    E  java.lang.NoSuchMethodException: io.chanse.locals.homeservices.android.customer.work.OrderFcmWorker.<init> [class android.content.Context, class androidx.work.WorkerParameters]
    E      at java.lang.Class.getConstructor0(Class.java:2332)
    E      at java.lang.Class.getDeclaredConstructor(Class.java:2170)
    E      at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
    E      at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:245)
    E      at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:137)
    E      at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
    E      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    E      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    E      at java.lang.Thread.run(Thread.java:923)
E  Could not create Worker io.chanse.locals.homeservices.android.customer.work.OrderFcmWorker

By seeing the above error, my first guess was UserRepo not being able to be constructed. But I have the below code already:

Module - order

@Module
@InstallIn(SingletonComponent::class)
class HomeServiceWorkerModule {
    @Provides
    fun fetchUserRepo() = UserRepo()
}

Here is how the HiltWorkerFactory being configured:

Module - app

@HiltAndroidApp
class CerveApp : Application(), Configuration.Provider {

    @Inject
    lateinit var workerFactory: HiltWorkerFactory

    override fun getWorkManagerConfiguration() = Configuration.Builder()
        .setWorkerFactory(workerFactory)
        .build()

}

And here is the way WorkManager initiazation happens:

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

FYI, I have also tried the below way:

<provider
    android:name="androidx.startup.InitializationProvider"
    android:authorities="${applicationId}.androidx-startup"
    android:exported="false"
    tools:node="merge">
    <meta-data
        android:name="androidx.work.WorkManagerInitializer"
        android:value="androidx.startup"
        tools:node="remove" />
</provider>

Not sure what am I missing and how should I tell the Hilt to construct my Worker with UserRepo. Any lead will help me a lot.

0 Answers
Related