18

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);

 }

6 Answers6

19

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">
  • Even after adding the FLAG_IMMUTABLE and the dependency, I'm still getting the error on some Samsung devices: Galaxy Note20 5G, Galaxy S20 FE 5G, and others. – John Doe May 06 '22 at 12:27
  • Yes I also found it on Samsung and HTC devices. I will post solutions once I solve it – HaryanviDeveloper May 07 '22 at 18:05
  • Please take a look at this comment https://github.com/facebook/react-native/issues/33375#issuecomment-1080979397 – Hamza Hmem May 19 '22 at 16:24
  • Thanks @HamzaHmem is this working for you ? did you try? – HaryanviDeveloper May 27 '22 at 12:41
  • @HaryanviDeveloper I've tried your solution and it's worked. However, there is a fix developed by the react native core team itself but it will be released with the 0.69 version. – Hamza Hmem May 29 '22 at 09:46
9

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);
  }
Saksham Pruthi
  • 206
  • 1
  • 6
  • Cool, but why should we check the version 23 and above when the error message is "Targeting S+ (version 31 and above)" – Tarek Feb 08 '22 at 06:49
  • @Tarek PendingIntent.FLAG_IMMUTABLE is not present in API below 23. The error message is because android has made it mandatory to add PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_IMUTABLE to all the pending intents from API 31(S) onwards – Saksham Pruthi Feb 08 '22 at 14:15
2

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)
Kevin ABRIOUX
  • 14,731
  • 9
  • 87
  • 84
0

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

Ajay
  • 1,077
  • 1
  • 15
  • 29
0

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.

antaki93
  • 506
  • 4
  • 8
0

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
    ...
  }
  ...
}
Pawara Siriwardhane
  • 1,394
  • 8
  • 21
  • 31