# WorkerFactory

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

*** ** * ** ***


```
public abstract class WorkerFactory
```

<br />

Known direct subclasses [DelegatingWorkerFactory](https://developer.android.com/reference/androidx/work/DelegatingWorkerFactory), [HiltWorkerFactory](https://developer.android.com/reference/androidx/hilt/work/HiltWorkerFactory)

|---|---|
| `https://developer.android.com/reference/androidx/work/DelegatingWorkerFactory` | A `https://developer.android.com/reference/androidx/work/WorkerFactory` which delegates to other factories. |
| `https://developer.android.com/reference/androidx/hilt/work/HiltWorkerFactory` | Worker Factory for the Hilt Extension |

*** ** * ** ***

A factory object that creates `https://developer.android.com/reference/androidx/work/ListenableWorker` instances. The factory is invoked every time a work runs. You can override the default implementation of this factory by manually initializing `https://developer.android.com/reference/androidx/work/WorkManager` (see `https://developer.android.com/reference/androidx/work/WorkManager#initialize(android.content.Context,androidx.work.Configuration)` and specifying a new WorkerFactory in `https://developer.android.com/reference/androidx/work/Configuration.Builder#setWorkerFactory(androidx.work.WorkerFactory)`.

## Summary

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/work/WorkerFactory#WorkerFactory()()` |

| ### Public methods |
|---|---|
| `abstract https://developer.android.com/reference/androidx/work/ListenableWorker` | `https://developer.android.com/reference/androidx/work/WorkerFactory#createWorker(android.content.Context,kotlin.String,androidx.work.WorkerParameters)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html appContext, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html workerClassName, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/WorkerParameters workerParameters )` Override this method to implement your custom worker-creation logic. |

## Public constructors

### WorkerFactory

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

```
public WorkerFactory()
```

## Public methods

### createWorker

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

```
public abstract 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://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html appContext` | The application context |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html workerClassName` | The class name of the worker to create |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/work/WorkerParameters workerParameters` | Parameters for worker initialization |

| Returns |
|---|---|
| `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 |