Configuration.Builder

class Configuration.Builder


A Builder for Configurations.

Summary

Public constructors

Creates a new Configuration.Builder.

Public functions

Configuration

Builds a Configuration object.

Configuration.Builder
setClock(clock: Clock)

Sets a Clock for WorkManager to calculate schedules and perform book-keeping.

Configuration.Builder
setContentUriTriggerWorkersLimit(contentUriTriggerWorkersLimit: Int)

Specifies the maximum number of Workers with Constraints.contentUriTriggers that could be enqueued simultaneously.

Configuration.Builder

Designates the primary process that WorkManager should schedule work in.

Configuration.Builder
setExecutor(executor: Executor)

Specifies a custom Executor for WorkManager.

Configuration.Builder

Specifies a Consumer<Throwable> that can be used to intercept exceptions caused when trying to initialize {@link WorkManager}, that usually happens when WorkManager cannot access its internal datastore.

Configuration.Builder

Specifies a custom InputMergerFactory for WorkManager.

Configuration.Builder
setJobSchedulerJobIdRange(minJobSchedulerId: Int, maxJobSchedulerId: Int)

Specifies the range of android.app.job.JobInfo IDs that can be used by WorkManager.

Configuration.Builder
setMaxSchedulerLimit(maxSchedulerLimit: Int)

Specifies the maximum number of system requests made by WorkManager when using android.app.job.JobScheduler or android.app.AlarmManager.

Configuration.Builder
setMinimumLoggingLevel(loggingLevel: Int)

Specifies the minimum logging level, corresponding to the constants found in android.util.Log.

Configuration.Builder

Specifies the RunnableScheduler to be used by WorkManager.

Configuration.Builder
setSchedulingExceptionHandler(
    schedulingExceptionHandler: Consumer<Throwable>
)

Specifies a Consumer<Throwable> that can be used to intercept exceptions caused when trying to schedule WorkRequests.

Configuration.Builder
setTaskExecutor(taskExecutor: Executor)

Specifies a Executor which will be used by WorkManager for all its internal book-keeping.

Configuration.Builder

Specifies a custom WorkerFactory for WorkManager.

Public constructors

Builder

Added in 1.0.0
Builder()

Creates a new Configuration.Builder.

Public functions

build

Added in 1.0.0
fun build(): Configuration

Builds a Configuration object.

Returns
Configuration

A Configuration object with this Builder's parameters.

setClock

Added in 2.9.0
fun setClock(clock: Clock): Configuration.Builder

Sets a Clock for WorkManager to calculate schedules and perform book-keeping.

This should only be overridden for testing. It must return the same value as System.currentTimeMillis in production code.

Parameters
clock: Clock

The Clock to use

Returns
Configuration.Builder

This Builder instance

setContentUriTriggerWorkersLimit

Added in 2.9.0
fun setContentUriTriggerWorkersLimit(contentUriTriggerWorkersLimit: Int): Configuration.Builder

Specifies the maximum number of Workers with Constraints.contentUriTriggers that could be enqueued simultaneously.

Unlike the other workers Workers with Constraints.contentUriTriggers must immediately occupy slots in JobScheduler to avoid missing updates, thus they are separated in the its own category.

setDefaultProcessName

Added in 2.5.0
fun setDefaultProcessName(processName: String): Configuration.Builder

Designates the primary process that WorkManager should schedule work in.

Parameters
processName: String

The String process name.

Returns
Configuration.Builder

This Builder instance

setExecutor

Added in 1.0.0
fun setExecutor(executor: Executor): Configuration.Builder

Specifies a custom Executor for WorkManager.

Parameters
executor: Executor

An Executor for running Workers

Returns
Configuration.Builder

This Builder instance

setInitializationExceptionHandler

Added in 2.8.0
fun setInitializationExceptionHandler(
    exceptionHandler: Consumer<Throwable>
): Configuration.Builder

Specifies a Consumer<Throwable> that can be used to intercept exceptions caused when trying to initialize {@link WorkManager}, that usually happens when WorkManager cannot access its internal datastore.

This exception handler will be invoked on a thread bound to Configuration.taskExecutor.

Parameters
exceptionHandler: Consumer<Throwable>

an instance to handle exceptions

Returns
Configuration.Builder

This Builder instance

setInputMergerFactory

Added in 2.3.0
fun setInputMergerFactory(inputMergerFactory: InputMergerFactory): Configuration.Builder

Specifies a custom InputMergerFactory for WorkManager.

Parameters
inputMergerFactory: InputMergerFactory

A InputMergerFactory for creating InputMergers

Returns
Configuration.Builder

This Builder instance

setJobSchedulerJobIdRange

Added in 1.0.0
fun setJobSchedulerJobIdRange(minJobSchedulerId: Int, maxJobSchedulerId: Int): Configuration.Builder

Specifies the range of android.app.job.JobInfo IDs that can be used by WorkManager. WorkManager needs a range of at least 1000 IDs.

JobScheduler uses integers as identifiers for jobs, and WorkManager delegates to JobScheduler on certain API levels. In order to not clash job codes used in the rest of your app, you can use this method to tell WorkManager the valid range of job IDs that it can use.

The default values are 0 and Integer#MAX_VALUE.

Parameters
minJobSchedulerId: Int

The first valid android.app.job.JobInfo ID (inclusive).

maxJobSchedulerId: Int

The last valid android.app.job.JobInfo ID (inclusive).

Returns
Configuration.Builder

This Builder instance

Throws
kotlin.IllegalArgumentException

when the size of the range is less than 1000

setMaxSchedulerLimit

Added in 1.0.0
fun setMaxSchedulerLimit(maxSchedulerLimit: Int): Configuration.Builder

Specifies the maximum number of system requests made by WorkManager when using android.app.job.JobScheduler or android.app.AlarmManager.

By default, WorkManager might schedule a large number of alarms or JobScheduler jobs. If your app uses JobScheduler or AlarmManager directly, this might exhaust the OS-enforced limit on the number of jobs or alarms an app is allowed to schedule. To help manage this situation, you can use this method to reduce the number of underlying jobs and alarms that WorkManager might schedule.

When the application exceeds this limit, WorkManager maintains an internal queue of WorkRequests, and schedules them when slots become free.

WorkManager requires a minimum of Configuration.MIN_SCHEDULER_LIMIT slots; this is also the default value. The total number of slots also cannot exceed 50.

Parameters
maxSchedulerLimit: Int

The total number of jobs which can be enqueued by WorkManager when using android.app.job.JobScheduler.

Returns
Configuration.Builder

This Builder instance

Throws
kotlin.IllegalArgumentException

if maxSchedulerLimit is less than Configuration.MIN_SCHEDULER_LIMIT

setMinimumLoggingLevel

Added in 1.0.0
fun setMinimumLoggingLevel(loggingLevel: Int): Configuration.Builder

Specifies the minimum logging level, corresponding to the constants found in android.util.Log. For example, specifying android.util.Log.VERBOSE will log everything, whereas specifying android.util.Log.ERROR will only log errors and assertions.The default value is android.util.Log.INFO.

Parameters
loggingLevel: Int

The minimum logging level, corresponding to the constants found in android.util.Log

Returns
Configuration.Builder

This Builder instance

setRunnableScheduler

Added in 2.4.0
fun setRunnableScheduler(runnableScheduler: RunnableScheduler): Configuration.Builder

Specifies the RunnableScheduler to be used by WorkManager.

This is used by the in-process scheduler to keep track of timed work.

Parameters
runnableScheduler: RunnableScheduler

The RunnableScheduler to be used

Returns
Configuration.Builder

This Builder instance

setSchedulingExceptionHandler

Added in 2.8.0
fun setSchedulingExceptionHandler(
    schedulingExceptionHandler: Consumer<Throwable>
): Configuration.Builder

Specifies a Consumer<Throwable> that can be used to intercept exceptions caused when trying to schedule WorkRequests.

It allows the application to handle a Throwable throwable typically caused when trying to schedule WorkRequests.

This exception handler will be invoked on a thread bound to Configuration.taskExecutor.

Parameters
schedulingExceptionHandler: Consumer<Throwable>

an instance to handle exceptions

Returns
Configuration.Builder

This Builder instance

setTaskExecutor

Added in 2.1.0
fun setTaskExecutor(taskExecutor: Executor): Configuration.Builder

Specifies a Executor which will be used by WorkManager for all its internal book-keeping.

For best performance this Executor should be bounded.

For more information look at androidx.room.RoomDatabase.Builder.setQueryExecutor.

Parameters
taskExecutor: Executor

The Executor which will be used by WorkManager for all its internal book-keeping

Returns
Configuration.Builder

This Builder instance

setWorkerFactory

Added in 1.0.0
fun setWorkerFactory(workerFactory: WorkerFactory): Configuration.Builder

Specifies a custom WorkerFactory for WorkManager.

Parameters
workerFactory: WorkerFactory

A WorkerFactory for creating ListenableWorkers

Returns
Configuration.Builder

This Builder instance