CodexBloom - Programming Q&A Platform

Unexpected Crash When Using WorkManager for Periodic Work Requests with Constraints on Android 14

๐Ÿ‘€ Views: 38 ๐Ÿ’ฌ Answers: 1 ๐Ÿ“… Created: 2025-06-10
android workmanager constraints kotlin

I'm trying to configure I'm writing unit tests and I'm performance testing and I am experiencing a crash in my Android app when I attempt to schedule a periodic work request using `WorkManager` with specific constraints on Android 14... The crash occurs when the system tries to execute the work, and the logcat shows the following behavior message: `java.lang.IllegalArgumentException: Constraints are not met`. My constraints include requiring the device to be charging and connected to an unmetered network. Hereโ€™s the code Iโ€™m using to set up the periodic work request: ```kotlin val constraints = Constraints.Builder() .setRequiresCharging(true) .setRequiredNetworkType(NetworkType.UNMETERED) .build() val periodicWorkRequest = PeriodicWorkRequestBuilder<MyWorker>(15, TimeUnit.MINUTES) .setConstraints(constraints) .build() WorkManager.getInstance(context).enqueueUniquePeriodicWork( "MyUniqueWork", ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest ) ``` Iโ€™ve also ensured that my `MyWorker` class is properly set up and does not contain any blocking calls or long-running operations on the main thread, yet the crash continues. I have tried removing the constraints altogether, which prevents the crash, but I need these constraints for my appโ€™s functionality. I also checked the device settings to confirm that it is indeed charging and connected to an unmetered network, but Iโ€™m still unable to get past this scenario. Has anyone encountered a similar question, or does anyone know what might be causing this unexpected crash? My development environment is Ubuntu. Has anyone else encountered this? This is my first time working with Kotlin 3.11. Any pointers in the right direction? What am I doing wrong? This is happening in both development and production on macOS. Thanks for your help in advance!