# HiltWorkerFactory

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

*** ** * ** ***

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

<br />

```
class HiltWorkerFactory : WorkerFactory
```

<br />

|---|---|-----------------------------------------------------------------------------------------------------------------------------|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)                                                |||
| ↳ | [androidx.work.WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory)                      ||
|   | ↳ | [androidx.hilt.work.HiltWorkerFactory](https://developer.android.com/reference/kotlin/androidx/hilt/work/HiltWorkerFactory) |

*** ** * ** ***

Worker Factory for the Hilt Extension

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

## Summary

|                                         ### Public functions                                         |
|------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)`?` | [createWorker](https://developer.android.com/reference/kotlin/androidx/hilt/work/HiltWorkerFactory#createWorker(android.content.Context,java.lang.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. |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         ### Inherited functions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From[androidx.work.WorkerFactory](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory) |------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ListenableWorker](https://developer.android.com/reference/kotlin/androidx/work/ListenableWorker)`!` | [createWorkerWithDefaultFallback](https://developer.android.com/reference/kotlin/androidx/work/WorkerFactory#createWorkerWithDefaultFallback(android.content.Context,java.lang.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)`!` `)` Returns a new instance of the specified`workerClassName`given the arguments. | |

## Public functions

### createWorker

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

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