— Android Studio, WorkManager, Kotlin — 1 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:
1dependencies {2 implementation "androidx.work:work-runtime-ktx:2.7.0"3}
1import androidx.work.Configuration2import androidx.work.WorkManager3import androidx.work.WorkRequest4
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 here13 .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 here6 .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.