How to resolve "Missing PendingIntent mutability flag" lint warning in android api 30+?
AndroidAndroid PendingintentLintAndroid Problem Overview
As soon as I updated the target SDK to 30+ (Android R or later), a lint warning Missing PendingIntent mutability flag
appeared on my PendingIntent.FLAG_UPDATE_CURRENT
flag when I want to define PendingIntent
.
How should I handle this lint with no effect on the app functionality?
Android Solutions
Solution 1 - Android
You can set your pending intent as
val updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag
)
According to the docs here: https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability
> 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.
Choose your flag accordingly.
If you want to read more about this i would suggest that you read this great article here: https://medium.com/androiddevelopers/all-about-pendingintents-748c8eb8619
Solution 2 - Android
If you're not using the latest version of WorkManager, you'll see this issue. It's been fixed in version 2.7.0-alpha02:
> Make PendingIntent mutability explicit, to fix a crash when targeting Android 12
Keep in mind that 2.7.0-alpha02 is only compatible with the Android 12 Developer Preview 1 SDK. So you may want to wait until it hits the beta or RC.
Update April 21, 2021 -- Adding to this answer for anyone googling the issue, the bug you may encounter may look something like this:
java.lang.IllegalArgumentException: com.myapp.myapp: Targeting S+ (version 10000 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:386)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:657)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:644)
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)
You do not have to be actually directly using WorkManager in your app to see this crash.
The solution, as outlined here, is to add a dependency to your build.gradle
file for Android 12 builds:
implementation 'androidx.work:work-runtime-ktx:2.7.0-alpha05'
Note that this dependency is different whether you are using Java only, Kotlin + coroutines, RxJava2, GCMNetworkManager, etc. So be sure to check the dox above.
Obviously replace the version number above with the latest. And as mentioned, it is NOT compatible with pre-android-13 builds.
Solution 3 - Android
If you let your app to run in android 12, there is a new PendingIntent mutability flag. If you don't want your PendingIntent to be mutated, use
PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
}else {
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
If you want your PendingIntent to be mutated use the following:
PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
}else {
pendingIntent = PendingIntent.getActivity(this,
0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
}
In Google documentation says, Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable. The change should be straightforward. Also, make sure you add the following work manager dependency if you are using AdMob 20.4.0 or lower in your app:
//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'
Note that currently work manager dependency version is 2.7.1. You can update the version to the latest one if you want.
Solution 4 - Android
If your app is targeting Android 12 (targetSdkVersion = 31)
, and uses an old version of the WorkManager directly OR by any of the third-party libraries then you require to update it to the latest to resolve it.
dependencies {
def work_version = "2.7.1"
// (Java only)
implementation "androidx.work:work-runtime:$work_version"
// Kotlin + coroutines
implementation "androidx.work:work-runtime-ktx:$work_version"
}
Solution 5 - Android
In my case it was also by third party libraries which were using old WorkManager versions, to force the new Android Work version on all dependencies use this in your root build.gradle file:
allproject {
project.configurations.all {
resolutionStrategy {
force 'androidx.work:work-runtime:2.7.0'
}
}
}
Solution 6 - Android
If you using Java and ADMOB you experience the PendingIntent Error wtih SDK S or Android 12. Here is a fix so ADMOB uses the correct work-runtime.
implementation 'com.google.android.gms:play-services-ads:19.5.0'
constraints {
implementation('androidx.work:work-runtime:2.7.0-alpha05') {
because 'previous versions have a bug impacting this application'
}
}
Solution 7 - Android
This crash is resolved with : implementation 'androidx.work:work-runtime:2.7.1'
Solution 8 - Android
If you let your app to run in android 12, there is a new PendingIntent mutability flag. If you don't want your PendingIntent to be muted, use
Java
PendingIntent updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag
)
Kotlin
val updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag
)
If you want your PendingIntent to be muted use the following:
Java
PendingIntent updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag
)
Kotlin
val updatedPendingIntent = PendingIntent.getActivity(
applicationContext,
NOTIFICATION_REQUEST_CODE,
updatedIntent,
PendingIntent.FLAG_MUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag
)
At the Last implement this Dependecy
//Work Manager dependency
implementation 'androidx.work:work-runtime:2.7.1'
Solution 9 - Android
This is issue with Work library. Even the latest version is affected 2.7.0-alpha04
https://issuetracker.google.com/issues/194108978
As temporary workaround - comment out including "work" dependency in gradle and remove using that class through the project. At least in this way you may run app normally and work on another features and areas....
Solution 10 - Android
For Java developers
PendingIntent pendingIntent = PendingIntent.getActivity(context, PENDING_INTENT_REQUEST_CODE,
notificationIntent,
Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ?
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE : PendingIntent.FLAG_UPDATE_CURRENT);
Solution 11 - Android
I had crashes like Fatal Exception: java.lang.IllegalArgumentException. Not posted. PendingIntents attached to actions with remote inputs must be mutable
.
I wrote this util method, which allows sending mutability as a param. Sometimes its required to get mutable flags, for example for reply actions in notifications.
private fun getPendingIntentFlags(isMutable: Boolean = false) =
when {
isMutable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S ->
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
!isMutable && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ->
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
else -> PendingIntent.FLAG_UPDATE_CURRENT
}
Usage example:
val quickReplyPendingIntent = PendingIntent.getBroadcast(
context, notificationId, replyIntent,
getPendingIntentFlags(true)
)
Solution 12 - Android
As I had four different PendingIntents in my code, I started by adding FLAG_IMMUTABLE to all of them. However the problem remained. After spending a lot of time analyzing my 4 intents, it dawned on me that the problem might come from one of my libraries.
In build.gradle, libraries are normally highlighted when old, but this is not the case for the Firebase BOM.
I had:
implementation platform('com.google.firebase:firebase-bom:26.1.1')
It turned out this was very old. After updating to
implementation platform('com.google.firebase:firebase-bom:29.0.4')
all was fine. No more FLAG_IMMUTABLE errors
Solution 13 - Android
in my project this line worked
PendingIntent pendingIntent = PendingIntent.getActivity(this,0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Solution 14 - Android
I updated my work-runtime-ktx
version to 2.7.1
After the above change i got into another error
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type: ErrorScope{Error scope for class <ERROR CLASS> with arguments: org.jetbrains.kotlin.types.IndexedParametersSubstitution@14ac19e7}
Look how i solved the above error by updating kotlin-gradle-plugin
version here.