Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

Custom WorkManager Configuration

If you require more control of how WorkManager manages and schedules work, you can customize the WorkManager configuration.

With a custom WorkManager configuration, you can:

Removing the default initializer

To provide your own configuration, you must first remove the default initializer. To do so, update the AndroidManifest.xml using the merge rule tools:node="remove" as shown below:

<provider
          android:name="androidx.work.impl.WorkManagerInitializer"
          android:authorities="${applicationId}.workmanager-init"
          tools:node="remove" />

To learn more about using merge rules in your manifest, check about the documentation on merging multiple manifest files.

Adding a custom configuration

Once the default initializer is removed, you can now manually initialize WorkManager as shown below :

Kotlin

// provide custom configuration
val config = Configuration.Builder()
        .setMinimumLoggingLevel(android.util.Log.INFO)
        .build()

//initialize WorkManager
WorkManager.initialize(this, config)

val workManager = WorkManager.getInstance()

Java

// provide custom configuration
Configuration myConfig = new Configuration.Builder()
        .setMinimumLoggingLevel(android.util.Log.INFO)
        .build();

//initialize WorkManager
WorkManager.initialize(this, myConfig);

WorkManager workManager = WorkManager.getInstance()

WorkManager needs to be initialized when the application starts, before you get an instance of the WorkManager singleton. Hence it should be called either during Application.onCreate() or in a ContentProvider.onCreate().

A complete list of customizations can be found in the Configuration.Builder() reference documentation.