Search code examples
androidkotlinandroid-workmanagerdagger-hilt

Inject WorkManager with Hilt but getting error "Dagger does not support providing @AssistedInject types."


While injecting WorkManager using HILT, compiler throwing below error 

"Dagger does not support providing @AssistedInject types"

Have followed all mentioned steps from below link

https://developer.android.com/training/dependency-injection/hilt-jetpack

PFB Coding snippet and build.gradle dependencies for HILT and Worker

  1. WorkerClass Using @HiltWorker and @AssistedInject

  2. Dependencies

enter image description here

To Resolve the issue have seen and implemented various S.O. post but no luck.

Whereas, if I didn't use both @HiltWorker and @AssistedInject and removed other dependencies from object constructor code compiles and run successfully but field injection for required dependencies won't work.

Therefore not sure whether its library issue or some implementation issue.

And thank you for your support and answer in advance!

Edit : (Adding WorkerModule.kt code)

@Module
@InstallIn(SingletonComponent::class)
object WorkerModule {

    @Singleton
    @Provides
    fun provideWorkerModule(context: Context,
        workerParameters: WorkerParameters
//        appNetworkService: AppNetworkService,
//        appDatabaseService: AppDatabaseService
    ): PaymentTrackerWorker {
//        return PaymentTrackerWorker(context, workerParameters,appNetworkService,appDatabaseService)
        return PaymentTrackerWorker(context, workerParameters)
    }
}

Solution

  • The error states that you are trying to @Provides a class with an @AssistedInject constructor. This makes sense: after all, there is no way to get the right instance of WorkerParameters in a singleton context.

    Fortunately, WorkerModule is also completely unnecessary, as HiltWorkerFactory already knows how to create any @HiltWorker-annotated class. Simply remove the module, and follow the tutorial to ensure that HiltWorkerFactory is installed in WorkManager.