Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hilt and WorkManager error : java.lang.NoSuchMethodException:<init> [class android.content.Context, class androidx.work.WorkerParameters]

Tags:

java

android

I am using work manager with Hilt but getting below error.

I am referring Android tutorial https://developer.android.com/training/dependency-injection/hilt-jetpack

Error

java.lang.NoSuchMethodException: com.debug.check.TestWorker.<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:245)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:137)
        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:929)

gradle version :

implementation 'androidx.work:work-runtime-ktx:2.7.1'
    // When using Kotlin.
kapt("androidx.hilt:hilt-compiler:1.0.0")
implementation 'androidx.hilt:hilt-work:1.0.0'

TestWorker

@HiltWorker
class TestWorker @AssistedInject constructor(
    @Assisted context: Context,
    @Assisted chatParameters: WorkerParameters,
    private val mainRepository: MainRepository
) : CoroutineWorker(context, chatParameters) {


    companion object {
        const val KEY_RESULT = "KEYRESULT"
    }


    override suspend fun doWork(): Result = coroutineScope {
        try {
            println("test")
            mainRepository.refreshMovies()
        } catch (e: Exception) {
            Result.failure()
        }
        Result.success()
    }
}  

MyApplication

@HiltAndroidApp
class MyApplication : Application(), Configuration.Provider {
    @Inject
    lateinit var workerFactory: HiltWorkerFactory

    override fun getWorkManagerConfiguration() =
        Configuration.Builder()
            .setWorkerFactory(workerFactory)
            .build()
    override fun onCreate() {
        super.onCreate()
       
    }
}

MainActivity

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        WorkManager workManager = WorkManager.getInstance(this);
        PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(TestChatWorker.class
                , 5, TimeUnit.SECONDS)
                .addTag("test")
                .build();

        workManager.enqueueUniquePeriodicWork("test", ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest);
        workManager.getWorkInfoByIdLiveData(periodicWorkRequest.getId()).observe(this, new Observer<WorkInfo>() {
            @Override
            public void onChanged(WorkInfo workInfo) {
                if (workInfo != null) {
                    if (workInfo.getState() == WorkInfo.State.SUCCEEDED) {
                        String chatResult = workInfo.getOutputData().getString(TestWorker.KEY_RESULT);

                    }
                }
            }
        });
    }
}

It continues to fail in the initiate part. I tried to downgrade work manager to 2.6 but still not able to solve this issue.I already implemented in manifest.xml.

 <provider
            android:name="androidx.startup.InitializationProvider"
            android:authorities="${applicationId}.androidx-startup"
            android:exported="false"
            tools:node="merge">
            <!-- If you are using androidx.startup to initialize other components -->
            <meta-data
                android:name="androidx.work.impl.WorkManagerInitializer"
                android:value="androidx.startup"
                tools:node="remove" />
        </provider>
like image 937
adam Avatar asked Sep 11 '25 16:09

adam


1 Answers

Two things you should check:

Check that you are using the correct provider entry depending on the version that you are using the AndroidManifest, this one worked for me:

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

Check that you have this two kapt dependencies on your build.gradle (this is not mentioned on the doc, and this was the cause for me having the same issue):

kapt "com.google.dagger:hilt-android-compiler:2.42"
kapt "androidx.hilt:hilt-compiler:1.0.0"
like image 95
Chepech Avatar answered Sep 14 '25 05:09

Chepech