google / dagger

A fast dependency injector for Android and Java.
https://dagger.dev
Apache License 2.0
17.45k stars 2.02k forks source link

[KSP][Android] WorkManagers cannot be created when using Hilt + KSP #4058

Closed SimoneBari-BS closed 1 year ago

SimoneBari-BS commented 1 year ago

Hello!

We recently tried to switch from KAPT to KSP using the latest release, but run into an issue preventing us from switching.

In particular, it seems that, using KSP, custom WorkManagers annotated with @HiltWorker are not processed correctly and cannot be instantiated at runtime, due to the following error:

Could not instantiate com.example.workmanager.DummyWorkManager
java.lang.NoSuchMethodException: com.example.workmanager.DummyWorkManager.<init> [class android.content.Context, class androidx.work.WorkerParameters]
    at java.lang.Class.getConstructor0(Class.java:2363)
    at java.lang.Class.getDeclaredConstructor(Class.java:2201)
    at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:95)
    at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:243)
    at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:145)
    at androidx.work.impl.utils.SerialExecutorImpl$Task.run(SerialExecutorImpl.java:96)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
    at java.lang.Thread.run(Thread.java:1012)

This doesn't happen when using KAPT.

I have prepared a simple repo to reproduce the issue: https://github.com/SimoneBari-BS/HiltKSPWorkerManager.

Any help in fixing this issue is appreciated!

kuanyingchou commented 1 year ago

You need to update your hilt-common, hilt-compiler, and hilt-work to 1.1.0-alpha01.

G00fY2 commented 1 year ago

@kuanyingchou Maybe worth mentioning in the latest release notes.

JohannesPtaszyk commented 1 year ago

Seems like Room also has to be on 2.6.0-alpha for everything to work, if using room :)

javimar commented 1 year ago

I am getting the error for the combo KSP /WorkManager annotated with @HiltWorker. However, KSP seems to be working OK with Room 2.5.2.

Raulsc9 commented 1 year ago

You need to update your hilt-common, hilt-compiler, and hilt-work to 1.1.0-alpha01.

I have the same problem as SimoneBari-BS and it is not solved by upgrading to "androidx.hilt:hilt-work:1.1.0-alpha01".

These are my dependencies relative to hilt;

//Dagger - Hilt implementation "com.google.dagger:hilt-android:2.48" ksp "com.google.dagger:hilt-compiler:2.48" implementation 'androidx.hilt:hilt-work:1.1.0-alpha01'

Best regards

kuanyingchou commented 1 year ago

Hi, @Raulsc9 , it looks like you're missing ksp "androidx.hilt:hilt-compiler:1.1.0-alpha01".

Raulsc9 commented 1 year ago

Thanks for your reply, but when I add that line, now I get error in the [@Insert, @Upsert, @Update, @Delete] annotations of Room, when I try to compile.

[ksp] C:/path_project.kt:27: Methods annotated with [@Insert, @Upsert, @Update, @Delete] shouldn't declare nullable parameters (com.example.models.MyFile).

It seems that the dependency "ksp "com.google.dagger:hilt-compiler:2.48", prevents nullable parameters being passed to DAO methods. Currently my hilt and room dependencies are like this;

//Room
    implementation "androidx.room:room-runtime:2.6.0-rc01"
    ksp "androidx.room:room-compiler:2.6.0-rc01"
    implementation "androidx.room:room-ktx:2.6.0-rc01"

    //Dagger - Hilt
    implementation "com.google.dagger:hilt-android:2.48"
    ksp "com.google.dagger:hilt-compiler:2.48"
    ksp "androidx.hilt:hilt-compiler:1.1.0-alpha01"
    implementation 'androidx.hilt:hilt-work:1.1.0-alpha01'

I've tried removing this line; ksp "com.google.dagger:hilt-compiler:2.48"

But then I get this error; The Hilt Android Gradle plugin is applied but no com.google.dagger:hilt-compiler dependency was found.

Everything was working fine before I migrated to KSP. A more detailed explanation would be appreciated, best regards!

kuanyingchou commented 1 year ago

Hi, @Raulsc9 , it looks like the Room error is working as intended since 2.6.0-alpha01:

https://issuetracker.google.com/issues/68198444

Sorry about the naming but com.google.dagger:hilt-compiler and androidx.hilt:hilt-compiler are different things. You'll need both for hilt-work to work.

G00fY2 commented 1 year ago

You need to update your hilt-common, hilt-compiler, and hilt-work to 1.1.0-alpha01.

hilt-compiler-1.1.0-alpha01 transitively depends on Room 2.6.0-alpha03 as you can see in the POM. Therefore you also need to migrate to Room 2.6.0 if you like to use WorkManager with Hilt + KSP.

adonese commented 1 year ago

have the same error as raulsc9's and still not solved -- using hilt versions 2.48.1 and room version of 2.6.0-alpha03 EDIT: i ended up having to fix my room models for the nullable [@insert, @upsert, https://github.com/update, @delete] -- after updating room version

Sutirth commented 11 months ago

I am using hilt version: 2.49, Work manager = 2.9.0. Trying to use this in multi module project facing the crash on lateinit workerFactory. Anybody has any recommendation or solution for the same?

khattab-dev commented 7 months ago

@Sutirth hey did you find any solution ?

Sutirth commented 7 months ago

@AhmedKhattab01 I have downgraded the work manager to 2.7.0 and everything seems to be working fine

taleroangel commented 4 months ago

I'm having the same issue with WorkManager 2.9.0, downgrading to 2.8.0 fixes the problem.

When using the 2.8.0 version the overriden method on the Configuration.Provider is called getWorkManagerConfiguration. in version 2.9.0 it is an overriden variable override val workManagerConfiguration

kuanyingchou commented 4 months ago

Hi, @taleroangel , Hilt doesn't do WorkManager initialization so this is more of a WorkManager issue. Since Configuration.Provider got rewritten in Kotlin in 2.9.0, so instead of a downgrade I think you can just update your Kotlin implementation and override the val instead.