# HiltWorkerFactory

Artifact: [androidx.hilt:hilt-work](https://developer.android.com/jetpack/androidx/releases/hilt) [View Source](https://cs.android.com/search?q=file:androidx/hilt/work/HiltWorkerFactory.java+class:androidx.hilt.work.HiltWorkerFactory) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/hilt#1.0.0)

*** ** * ** ***


```
public final class HiltWorkerFactory extends WorkerFactory
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.work.WorkerFactory](https://developer.android.com/reference/androidx/work/WorkerFactory) ||
|   | ↳ | [androidx.hilt.work.HiltWorkerFactory](https://developer.android.com/reference/androidx/hilt/work/HiltWorkerFactory) |

*** ** * ** ***

Worker Factory for the Hilt Extension

A provider for this factory will be installed in the `https://dagger.dev/api/latest/dagger/hilt/components/SingletonComponent.html`.

## Summary

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/work/ListenableWorker` | `https://developer.android.com/reference/androidx/hilt/work/HiltWorkerFactory#createWorker(android.content.Context,java.lang.String,androidx.work.WorkerParameters)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html appContext, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html workerClassName, @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/WorkerParameters workerParameters )` Override this method to implement your custom worker-creation logic. |

| ### Inherited methods |
|---|
| From [androidx.work.WorkerFactory](https://developer.android.com/reference/androidx/work/WorkerFactory) |---|---| | `final https://developer.android.com/reference/androidx/work/ListenableWorker` | `https://developer.android.com/reference/androidx/work/WorkerFactory#createWorkerWithDefaultFallback(android.content.Context,java.lang.String,androidx.work.WorkerParameters)( https://developer.android.com/reference/android/content/Context.html appContext, https://developer.android.com/reference/java/lang/String.html workerClassName, https://developer.android.com/reference/androidx/work/WorkerParameters workerParameters )` Returns a new instance of the specified `workerClassName` given the arguments. | |

## Public methods

### createWorker

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/hilt#1.0.0)

```
public @Nullable ListenableWorker createWorker(
    @NonNull Context appContext,
    @NonNull String workerClassName,
    @NonNull WorkerParameters workerParameters
)
```

Override this method to implement your custom worker-creation logic. Use `https://developer.android.com/reference/androidx/work/Configuration.Builder#setWorkerFactory(androidx.work.WorkerFactory)` to use your custom class.

Throwing an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-exception/index.html` here and no `https://developer.android.com/reference/androidx/work/ListenableWorker` will be created. If a `https://developer.android.com/reference/androidx/work/WorkerFactory` is unable to create an instance of the `https://developer.android.com/reference/androidx/work/ListenableWorker`, it should return `null` so it can delegate to the default `https://developer.android.com/reference/androidx/work/WorkerFactory`.

Returns a new instance of the specified `workerClassName` given the arguments. The returned worker must be a newly-created instance and must not have been previously returned or invoked by WorkManager. Otherwise, WorkManager will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html`.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html appContext` | The application context |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/lang/String.html workerClassName` | The class name of the worker to create |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/androidx/work/WorkerParameters workerParameters` | Parameters for worker initialization |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html https://developer.android.com/reference/androidx/work/ListenableWorker` | A new `https://developer.android.com/reference/androidx/work/ListenableWorker` instance of type `workerClassName`, or `null` if the worker could not be created |