[Android] Targeting S+ requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE
TL;TR
Set flags to your pending intents
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE)
and force usage of newest AndroidX work dependency on your module build.gradle
.
implementation 'androidx.work:work-runtime-ktx:2.7.0'
The problem
While I upgrade my app to targeting API 31 (Android 12) followed by testing on an Emulator with the latest version, the app crashes directly on launch with following exception:
java.lang.IllegalArgumentException: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:174)
at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:108)
at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:86)
at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
While the error message tell me a PendingIntent
needs to be flagged with FLAG_IMMUTABLE
or FLAG_MUTABLE
, my own code does not contain any PendingIntent
what let me confused about the crashing.
After more detailed look we cann see that AndroidX cause the crash.
at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:174)
Solution
PendingIntent
flags
If my app using PendingIntents
there is a new flag set required.
So the solultion is to add PendingIntent.FLAG_IMMUTABLE
or PendingIntent.FLAG_MUTABLE
to the PendingIntent
creation.
PendingIntent.getActivity(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE)
Libraries with PendingIntent
While AndroidX’s Workmanager already on version 2.7.0
that fullfill the Android 12 requirements some Play Service libraries use an older version of the WorkManager.
For example the Play Services Ads library (com.google.android.gms:play-services-ads:20.4.0
) use the version 2.1.0
of WorkManager.
So the simple fix is to force the usage of newest WorkManager version until the top-level libraries are up-to-date.
Add following line to your build.gradle
of your module, and the crash should be gone.
implementation 'androidx.work:work-runtime-ktx:2.7.0'
That’s it!
References
- Stackoverflow - MediaSessionCompat: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent
- Behavior changes: Apps targeting Android 12
Contact
If you have questions or corrections contact me at info@liveyourproject.com.
Author: Fabian Keunecke