# WorkerFactory

# 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)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/work/WorkerFactory "View this page in Java")

<br />

```
abstract class WorkerFactory
```

<br />

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

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

*** ** * ** ***

A factory object that creates[ListenableWorker](https://developer.android.com/reference/kotlin/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[WorkManager](https://developer.android.com/reference/kotlin/androidx/work/WorkManager)(see[WorkManager.initialize](https://developer.android.com/reference/kotlin/androidx/work/WorkManager#initialize(android.content.Context,androidx.work.Configuration))and specifying a new WorkerFactory in[Configuration.Builder.setWorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/Configuration.Builder#setWorkerFactory(androidx.work.WorkerFactory)).

## Summary

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

|                                              ### Public functions                                               |
|-----------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `abstract `[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)`?` | [createWorker](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory#createWorker(android.content.Context,kotlin.String,androidx.work.WorkerParameters))`(` ` appContext: `[Context](https://developer.android.com/reference/android/content/Context.html)`,` ` workerClassName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)`,` ` workerParameters: `[WorkerParameters](https://developer.android.com/reference/kotlin/androidx/work/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)  

```
WorkerFactory()
```  

## Public functions

### createWorker

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

```
abstract fun createWorker(
    appContext: Context,
    workerClassName: String,
    workerParameters: WorkerParameters
): ListenableWorker?
```

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

Throwing an[Exception](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-exception/index.html)here and no[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)will be created. If a[WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)is unable to create an instance of the[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker), it should return`null`so it can delegate to the default[WorkerFactory](https://developer.android.com/reference/kotlin/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[IllegalStateException](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html).  

|                                                      Parameters                                                       |
|-----------------------------------------------------------------------------------------------------------------------|----------------------------------------|
| `appContext: `[Context](https://developer.android.com/reference/android/content/Context.html)                         | The application context                |
| `workerClassName: `[String](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html)                  | The class name of the worker to create |
| `workerParameters: `[WorkerParameters](https://developer.android.com/reference/kotlin/androidx/work/WorkerParameters) | Parameters for worker initialization   |

|                                               Returns                                                |
|------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)`?` | A new[ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)instance of type`workerClassName`, or`null`if the worker could not be created |