Skip to content

Add @HiltWorker annotation processor support with @AssistedInject integration#5196

Open
Senthil455 wants to merge 1 commit into
google:masterfrom
Senthil455:hilt-worker-support
Open

Add @HiltWorker annotation processor support with @AssistedInject integration#5196
Senthil455 wants to merge 1 commit into
google:masterfrom
Senthil455:hilt-worker-support

Conversation

@Senthil455

Copy link
Copy Markdown

Summary

Fixes #4490

Add support for Hilt injection of WorkManager ListenableWorker classes via the @HiltWorker annotation. Previously, @AssistedInject constructors in @HiltWorker classes would fail with [Dagger/MissingBinding]\ errors after Kotlin/Gradle version updates.

Changes

New Annotations

  • @HiltWorker (\dagger.hilt.android.work.HiltWorker) - Marks a WorkManager ListenableWorker subclass for Hilt injection
  • @HiltWorkerMap (\dagger.hilt.android.internal.work.HiltWorkerMap) - Internal qualifier for the worker multibinding map

New Processor Files

  • HiltWorkerProcessor - Javac annotation processor for @HiltWorker
  • KspHiltWorkerProcessor - KSP annotation processor for @HiltWorker
  • HiltWorkerProcessingStep - Processing step that drives metadata validation and code generation
  • HiltWorkerMetadata - Validates @HiltWorker usage (extends ListenableWorker, has exactly one @Inject or @AssistedInject constructor, non-private, non-scoped)
  • HiltWorkerModuleGenerator - Generates:
    • \WorkerName_HiltModules\ with \BindsModule\ and \KeyModule\ inner classes
    • \WorkerName_AssistedFactory\ interface (for @AssistedInject workers) annotated with @AssistedFactory
  • HiltWorkerValidationPlugin - Prevents direct injection of @HiltWorker classes at graph validation time

Modified Files

  • AndroidClassNames.java - Added WorkManager-related class name constants

How It Works

For @HiltWorker with @Inject constructor:

The processor generates a module that binds the worker class into a multibinding map keyed by @LazyClassKey.

For @HiltWorker with @AssistedInject constructor:

The processor generates:

  1. A \WorkerName_AssistedFactory\ interface annotated with @AssistedFactory\ that defines a \create(Context, WorkerParameters)\ method
  2. A module that binds the assisted factory into the multibinding map
  3. Dagger's assisted injection processing handles the rest (generates _Impl\ class that resolves non-assisted dependencies)

This ensures that non-@assisted parameters (like ReminderProcessor) are properly injected from the Dagger component, while @assisted parameters (Context, WorkerParameters) are passed in by WorkManager.

…egration

Add support for Hilt injection of WorkManager ListenableWorker classes
via the @HiltWorker annotation. This enables Hilt to inject dependencies
into WorkManager workers, including support for @AssistedInject constructors.

Changes include:
- @HiltWorker annotation to mark Worker classes for Hilt injection
- HiltWorkerMap qualifier for internal multibinding map
- HiltWorkerProcessor (Javac) and KspHiltWorkerProcessor (KSP)
- HiltWorkerProcessingStep that processes @HiltWorker annotations
- HiltWorkerMetadata validation (checks: extends ListenableWorker,
  has @Inject/@AssistedInject constructor, non-private, non-scoped)
- HiltWorkerModuleGenerator generates:
  - _HiltModules class with @BINDS and @provides module bindings
  - _AssistedFactory interface (for @AssistedInject workers) annotated
    with @AssistedFactory so Dagger generates the factory implementation
- HiltWorkerValidationPlugin prevents direct injection of @HiltWorker classes
- Tests for @Inject and @AssistedInject worker scenarios

Fixes google#4490
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@AssistedInject doesn't work after gradle update

1 participant