How to Resolve "Missing Pendingintent Mutability Flag" Lint Warning in Android API 30+

How to resolve Missing PendingIntent mutability flag lint warning in android api 30+?

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

Missing PendingIntent mutability flag giving warning after checking sdk version

Your code seems OK, and I believe it's a bug in the Lint check as it's been

stated by @CommonsWare in comments. This could be fixed in the next releases of Android Studio

How can I remove this warning message?

If you just want to remove the annoying warning there is a hack in building the condition that clear the warning: by transferring the conditional to the flag:

val pendingIntent: PendingIntent = PendingIntent.getActivity(
this,
0,
intent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
else PendingIntent.FLAG_UPDATE_CURRENT
)

Or at the worse case you'd suppress it by @SuppressLint("UnspecifiedImmutableFlag"), which I don't recommend.

Android 12 Pending Intent

You need to do this:

pendingIntent = PendingIntent.getBroadcast(service, 0, new Intent(
action), PendingIntent.FLAG_UPDATE_CURRENT |
PendingIntent.FLAG_IMMUTABLE);

Since you are using AlarmManager you should be able to use the IMMUTABLE flag.

Android 12 Pending Intent Immutable flag not available under API 23

From the documentation of PendingIntent.FLAG_MUTABLE:

Up until Build.VERSION_CODES.R, PendingIntents are assumed to be mutable by default, unless FLAG_IMMUTABLE is set. Starting with Build.VERSION_CODES.S, it will be required to explicitly specify the mutability of PendingIntents on creation with either FLAG_IMMUTABLE or FLAG_MUTABLE. It is strongly recommended to use FLAG_IMMUTABLE when creating a PendingIntent. FLAG_MUTABLE should only be used when some functionality relies on modifying the underlying intent, e.g. any PendingIntent that needs to be used with inline reply or bubbles.

In summary, you should add the FLAG_IMMUTABLE flag to your PendingIntent when targeting API 31 or later, unless you need your PendingIntent to be mutable, in which case you need to use FLAG_MUTABLE.

Because FLAG_IMMUTABLE was introduced in API 23, you have to use FLAG_MUTABLE as a fallback for lower versions.

val flag =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE
else PendingIntent.FLAG_MUTABLE

You can combine this with your existing intent flags, if you have any, using a bitwise or operation. For example:

val flags = flag or PendingIntent.FLAG_ONE_SHOT

Missing mutability flags: Android 12 pending intents with NavDeepLinkBuilder

As per the Navigation 2.4.0-alpha04 release notes:

NavDeepLinkBuilder now adds PendingIntent.FLAG_IMMUTABLE to the PendingIntent returned by createPendingIntent(), ensuring that this API works as expected when targeting Android 12. (If8c52)

You'll need to upgrade to Navigation 2.4.0-alpha04 or higher (currently 2.4.0-beta01) if you want that fix.

FakeAppUpdateManager - No longer working on API 31+, does not specify mutability flag when creating PendingIntent, when fetching appUpdateInfo

Turns out the issue is fixed in

com.google.android.play:core:1.10.3

If you only had

com.google.android.play:core-ktx:1.8.1

in your dependencies, then you had this error. So make sure to add both dependencies to your gradle file.



Related Topics



Leave a reply



Submit