'Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified

App crashes at runtime with the following error :

java.lang.IllegalArgumentException: maa.abc: 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 com.google.android.exoplayer2.ui.PlayerNotificationManager.createBroadcastIntent(PlayerNotificationManager.java:1373) at com.google.android.exoplayer2.ui.PlayerNotificationManager.createPlaybackActions(PlayerNotificationManager.java:1329) at com.google.android.exoplayer2.ui.PlayerNotificationManager.(PlayerNotificationManager.java:643) at com.google.android.exoplayer2.ui.PlayerNotificationManager.(PlayerNotificationManager.java:529) at com.google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:456) at com.google.android.exoplayer2.ui.PlayerNotificationManager.createWithNotificationChannel(PlayerNotificationManager.java:417)

I tried all solutions available but the app still crashing on Android 12.

 @Nullable
 @Override
 public PendingIntent createCurrentContentIntent(@NonNull Player player) {
        Intent intent = new Intent(service, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_SINGLE_TOP |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
        return PendingIntent.getActivity(service, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);

 }


Solution 1:[1]

If using java or react-native then paste this inside app/build.gradle

dependencies {
  // ...
  implementation 'androidx.work:work-runtime:2.7.1'
}

If using Kotlin then use this

dependencies {
  // ...
  implementation 'androidx.work:work-runtime-ktx:2.7.0'
}

and if anybody still facing the crash issue for android 12 then make sure you add following in AndroidMenifest.xml

 <activity 
   ...
   android:exported="true" // in most cases it is true but based on requirements it can be false also   
>   


   // If using react-native push notifications then make sure to add into it also

 <receiver   
android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationBootEventReceiver" android:exported="true">
 
   //  Similarly
 
 <service android:name="com.dieam.reactnativepushnotification.modules.RNPushNotificationListenerService" android:exported="true">

Solution 2:[2]

Check and update the dependency version of exoplayer to the latest one

android.app.PendingIntent.getBroadcast() previously used to return

@Nullable
@Override
private static PendingIntent createBroadcastIntent(
    String action, Context context, int instanceId) {
    Intent intent = new Intent(action).setPackage(context.getPackageName());
    intent.putExtra(EXTRA_INSTANCE_ID, instanceId);
    return PendingIntent.getBroadcast(
        context, instanceId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  }

If you observe carefully PendingIntent.FLAG_IMMUTABLE is missing here in the above snippet

It has now been updated to return the following

@Nullable
@Override
private static PendingIntent createBroadcastIntent(
      String action, Context context, int instanceId) {
    Intent intent = new Intent(action).setPackage(context.getPackageName());
    intent.putExtra(EXTRA_INSTANCE_ID, instanceId);

    int pendingFlags;
    if (Util.SDK_INT >= 23) {
      pendingFlags = PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE;
    } else {
      pendingFlags = PendingIntent.FLAG_UPDATE_CURRENT;
    }

    return PendingIntent.getBroadcast(context, instanceId, intent, pendingFlags);
  }

Solution 3:[3]

Solution for Kotlin, juste add this flag if you are with API M

val flags = when {
            Build.VERSION.SDK_INT >= Build.VERSION_CODES.M -> FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE
            else -> FLAG_UPDATE_CURRENT
        }
val toastPendingIntent = PendingIntent.getBroadcast(context, 0, providerIntent, flags)

Solution 4:[4]

I resolved this by adding below in the ...android/app/build.gradle

implementation 'androidx.work:work-runtime-ktx:2.8.0-alpha01'

https://github.com/react-native-maps/react-native-maps/issues/4083#issue-1119280606

Solution 5:[5]

If you use Chuck, you won't be able to using it after updating the SDK version. You can easily replace it with Chucker (Chuck's fork): https://github.com/jgilfelt/chuck/issues/101#issuecomment-869119599.

Solution 6:[6]

For react-native projects, the same error can be fixed by lowering the target SDK version.

  1. Go to android/build.gradle
  2. Decrease the targetSDK from 31 to 30, in ext of buildscript block.
buildscript{
  ext{
    ...
    targetSdkVersion = 30
    ...
  }
  ...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HaryanviDeveloper
Solution 2
Solution 3 Kevin ABRIOUX
Solution 4 Ajay
Solution 5 antaki93
Solution 6 Pawara Siriwardhane