Skip to content
DeveloperMemos

Fixing the 'Specify a Valid Range of Job IDs for WorkManager to Use' Warning in Android Studio

Android Studio, WorkManager, Kotlin1 min read

If you're working on an Android app that uses WorkManager, you may have come across the warning message "Specify a valid range of job id's for WorkManager to use". This warning indicates that WorkManager is trying to schedule jobs using invalid or out-of-range job IDs.

WorkManager relies on the Android system's JobScheduler API to schedule and run background tasks, and JobScheduler requires that each scheduled job has a unique integer ID. By default, WorkManager generates job IDs automatically, but you can also specify your own IDs if needed.

To avoid the "Specify a valid range of job id's for WorkManager to use" warning, you can use the setJobSchedulerJobIdRange() method in Kotlin to set a valid range for job IDs that WorkManager can use. Here's how:

  1. Open your app's build.gradle file and add the following dependency:
1dependencies {
2 implementation "androidx.work:work-runtime-ktx:2.7.0"
3}
  1. In your app's Application class or another appropriate place, initialize WorkManager with a configuration that sets the valid job ID range:
1import androidx.work.Configuration
2import androidx.work.WorkManager
3import androidx.work.WorkRequest
4
5class MyApp : Application() {
6
7 override fun onCreate() {
8 super.onCreate()
9
10 val config = Configuration.Builder()
11 .setMinimumLoggingLevel(android.util.Log.INFO)
12 .setJobSchedulerJobIdRange(1000, 2000) // Set the job ID range here
13 .build()
14
15 WorkManager.initialize(this, config)
16 }
17
18}

Or alternatively(you can read more about custom configuration for WorkManager in the documentation here):

1class MyApp : Application(), Configuration.Provider {
2 override fun getWorkManagerConfiguration() =
3 Configuration.Builder()
4 .setMinimumLoggingLevel(android.util.Log.INFO)
5 .setJobSchedulerJobIdRange(1000, 2000) // Set the job ID range here
6 .build()
7}

In this example, we set the valid job ID range to be between 1000 and 2000. You can choose any valid range of integers that suits your needs.

Once you've set the job ID range using setJobSchedulerJobIdRange(), WorkManager will only generate job IDs within that range, which will prevent the "Specify a valid range of job id's for WorkManager to use" warning from appearing in Android Studio.