Debug WorkManager

If you notice that your workers run too often or not at all, here are some debugging steps that can help you discover what is happening.

Enable logging

To determine why your workers aren't running properly, it can be very useful to look at verbose WorkManager logs. To enable logging, use custom initialization.

First, disable the default WorkManagerInitializer in your AndroidManifest.xml file by creating a new WorkManager provider with the manifest-merge rule remove applied:

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

Now that the default WorkManager initializer is disabled, you can use on-demand initialization. To do so, the android.app.Application class needs to provide an implementation for androidx.work.Configuration.Provider:

Kotlin

classMyApplication():Application(),Configuration.Provider{
overridefungetWorkManagerConfiguration()=
Configuration.Builder()
.setMinimumLoggingLevel(android.util.Log.DEBUG)
.build()
}

Java

publicclass MyApplicationextendsApplicationimplementsConfiguration.Provider{
@NonNull
@Override
publicConfigurationgetWorkManagerConfiguration(){
returnnewConfiguration.Builder()
.setMinimumLoggingLevel(android.util.Log.DEBUG)
.build();
}
}

When you define a custom WorkManager configuration, your WorkManager is initialized when you call WorkManager.getInstance(Context) rather than automatically at application startup. See Custom WorkManager Configuration and Initialization for more information, including support for versions of WorkManager before 2.1.0.

With DEBUG logging enabled, you see a lot more logs with the log-tag prefix WM-.

Use adb shell dumpsys jobscheduler

You can use adb to get more information about job scheduling on Android 6.0 (API level 23) or higher. If you are new to adb, see Command-line tools for more details.

Once you have adb installed, run the following command to look at the list of jobs that are attributed to your package:

adbshelldumpsysjobscheduler

The output looks something like this:

JOB#u0a172/4:6412553com.google.android.youtube/androidx.work.impl.background.systemjob.SystemJobService
u0a172tag=*job*/com.google.android.youtube/androidx.work.impl.background.systemjob.SystemJobService
Source:uid=u0a172user=0pkg=com.google.android.youtube
JobInfo:
Service:com.google.android.youtube/androidx.work.impl.background.systemjob.SystemJobService
Requires:charging=falsebatteryNotLow=falsedeviceIdle=false
Extras:mParcelledData.dataSize=180
Networktype:NetworkRequest[NONEid=0,[Capabilities:NOT_METERED&INTERNET&NOT_RESTRICTED&TRUSTED&VALIDATEDUid:10172]]
Minimumlatency:+1h29m59s687ms
Backoff:policy=1initial=+30s0ms
Hasearlyconstraint
Requiredconstraints:TIMING_DELAYCONNECTIVITY[0x90000000]
Satisfiedconstraints:DEVICE_NOT_DOZINGBACKGROUND_NOT_RESTRICTEDWITHIN_QUOTA[0x3400000]
Unsatisfiedconstraints:TIMING_DELAYCONNECTIVITY[0x90000000]
Tracking:CONNECTIVITYTIMEQUOTA
Implicitconstraints:
readyNotDozing:true
readyNotRestrictedInBg:true
Standbybucket:RARE
Baseheartbeat:0
Enqueuetime:-51m29s853ms
Runtime:earliest=+38m29s834ms,latest=none,originallatest=none
Lastrunheartbeat:0
Ready:false(job=falseuser=true!pending=true!active=true!backingup=truecomp=true)

When using WorkManager, the component responsible for managing worker execution is SystemJobService on API level 23 or higher. Look for instances of jobs that are attributed to your package name and androidx.work.impl.background.systemjob.SystemJobService.

For every job, the output from the command lists required, satisfied, and unsatisfied constraints. Check whether your worker’s constraints are fully satisfied.

The output also includes job history for recently executed jobs, so you can use it to check whether SystemJobService was invoked recently.

Jobhistory:
-1h35m26s440msSTART:#u0a107/9008com.google.android.youtube/androidx.work.impl.background.systemjob.SystemJobService
-1h35m26s362msSTOP-P:#u0a107/9008com.google.android.youtube/androidx.work.impl.background.systemjob.SystemJobServiceappcalledjobFinished

Request diagnostic information from WorkManager 2.4.0+

On debug builds of your app, you can request diagnostic information from WorkManager 2.4.0 and higher using the following command:

adbshellambroadcast-a"androidx.work.diagnostics.REQUEST_DIAGNOSTICS"-p"<your_app_package_name>"

This provides information on:

  • Work requests that completed in the last 24 hours.
  • Work requests that are currently running.
  • Work requests that are scheduled.

Here is what it could look like (the output is visible through logcat):

adbshellambroadcast-a"androidx.work.diagnostics.REQUEST_DIAGNOSTICS"-p"androidx.work.integration.testapp"
adblogcat
...
2020-02-1314:21:37.99029528-29660/androidx.work.integration.testappI/WM-DiagnosticsWrkr:Recentlycompletedwork:
2020-02-1314:21:38.08329528-29660/androidx.work.integration.testappI/WM-DiagnosticsWrkr:IdClassNameStateUniqueNameTags
08be261c-2def-4bd6-a716-1e4410968dc4androidx.work.impl.workers.DiagnosticsWorkerSUCCEEDEDnullandroidx.work.impl.workers.DiagnosticsWorker
48ce04f1-8df9-450b-96ec-6eceabb9c690androidx.work.impl.workers.DiagnosticsWorkerSUCCEEDEDnullandroidx.work.impl.workers.DiagnosticsWorker
c46f4699-c384-440c-a10e-26d56ce02963androidx.work.impl.workers.DiagnosticsWorkerSUCCEEDEDnullandroidx.work.impl.workers.DiagnosticsWorker
ce125372-046e-484e-949f-9abb35ce62c3androidx.work.impl.workers.DiagnosticsWorkerSUCCEEDEDnullandroidx.work.impl.workers.DiagnosticsWorker
72887ddd-8ed1-4018-b798-fac218e95e16androidx.work.impl.workers.DiagnosticsWorkerSUCCEEDEDnullandroidx.work.impl.workers.DiagnosticsWorker
dcff3d61-320d-4996-8644-5d97944bd09candroidx.work.impl.workers.DiagnosticsWorkerSUCCEEDEDnullandroidx.work.impl.workers.DiagnosticsWorker
acab0bf7-6087-43ad-bdb5-be0df9195acbandroidx.work.impl.workers.DiagnosticsWorkerSUCCEEDEDnullandroidx.work.impl.workers.DiagnosticsWorker
23136bcd-01dd-46eb-b910-0fe8a140c2a4androidx.work.integration.testapp.ToastWorkerSUCCEEDEDnullandroidx.work.integration.testapp.ToastWorker
245f4879-c6d2-4997-8130-e4e90e1cab4candroidx.work.integration.testapp.ToastWorkerSUCCEEDEDnullandroidx.work.integration.testapp.ToastWorker
17d05835-bb61-429a-ad11-fe43fc320a54androidx.work.integration.testapp.ToastWorkerSUCCEEDEDnullandroidx.work.integration.testapp.ToastWorker
e95f12be-4b0c-4e64-88da-8ee07a31e42fandroidx.work.integration.testapp.ToastWorkerSUCCEEDEDnullandroidx.work.integration.testapp.ToastWorker
431c3ec2-4a55-469b-b50b-4072d35f1232androidx.work.integration.testapp.ToastWorkerSUCCEEDEDnullandroidx.work.integration.testapp.ToastWorker
883a388f-f911-4098-9143-37bd8fbc098aandroidx.work.integration.testapp.ToastWorkerSUCCEEDEDnullandroidx.work.integration.testapp.ToastWorker
b904163c-6822-4299-8d5a-78df49b7e53dandroidx.work.integration.testapp.ToastWorkerSUCCEEDEDnullandroidx.work.integration.testapp.ToastWorker
453fd7b9-2b16-45b9-abc5-3d2ce7b6a4baandroidx.work.integration.testapp.ToastWorkerSUCCEEDEDnullandroidx.work.integration.testapp.ToastWorker
2020-02-1314:21:38.08329528-29660/androidx.work.integration.testappI/WM-DiagnosticsWrkr:Runningwork:
2020-02-1314:21:38.08929528-29660/androidx.work.integration.testappI/WM-DiagnosticsWrkr:IdClassNameStateUniqueNameTags
b87c8a4f-4ac6-4e25-ba3e-4cea53ce468aandroidx.work.impl.workers.DiagnosticsWorkerRUNNINGnullandroidx.work.impl.workers.DiagnosticsWorker
...

Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.

Last updated 2024年01月03日 UTC.