WorkManager, one of the Jetpack features.

As many of you already know, Work Manager is the answer that the Android Dev Team gave to a frustrating fragmentation on the Job area.

WorkManager is easy, let’s quickly see it in action:

val myConstraints = Constraints.Builder()

.setRequiredNetworkType(NetworkType.CONNECTED)

.build()



val work = PeriodicWorkRequest.Builder(

CardsUpdaterWorker::class.java,

2,

TimeUnit.DAYS

)

.setConstraints(myConstraints)

.build()



WorkManager.getInstance().enqueue(work)

and boom! you just scheduled a periodic job where the logic is in CardsUpdaterWorker.

Everything looks very easy unless you run your app at least 100 times on an Android 7 Nougat device. Try it and you will see a crash like this:

Fatal Exception: java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs

at android.os.Parcel.readException(Parcel.java:1701)

at android.os.Parcel.readException(Parcel.java:1646)

at android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:158)

at android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:42)

at androidx.work.impl.background.systemjob.SystemJobScheduler.scheduleInternal(SystemJobScheduler.java:85)

at androidx.work.impl.background.systemjob.SystemJobScheduler.schedule(SystemJobScheduler.java:64)

...

...

...

This crash may occur on other OS or devices as well.

In my case, this crash happens when the app tries to enqueue jobs while being in background.

To reproduce, you can create this Makefile and run make test-jobs: