67

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

 }

11 Answers11

71

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">
HaryanviDeveloper
  • 1,005
  • 1
  • 8
  • 15
24

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
  • 394
  • 1
  • 8
  • 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
  • 3
    @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
    Noticed that use of the `Util.SDK_INT` constants might not be very portable for most projects, and so would recommend use of the android "official" `Build.VERSION.SDK_INT` variants. – JWL Jul 05 '22 at 08:30
  • what is flutter kotlin version of code? – chanrlc Mar 22 '23 at 04:07
14

In my case for read tags using the foreground delivery system, its works..

If you let your app to run in android 12, use the following:

PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
   pendingIntent = PendingIntent.getActivity(this,
          0, new Intent(this, getClass()).addFlags(
   Intent.FLAG_ACTIVITY_SINGLE_TOP), 
   PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
}else {
   pendingIntent = PendingIntent.getActivity(this,
      0, new Intent(this, getClass()).addFlags(
   Intent.FLAG_ACTIVITY_SINGLE_TOP), 
   PendingIntent.FLAG_UPDATE_CURRENT);
}
Dean Amilush
  • 141
  • 1
  • 4
  • Worked for me, but i had to specify only FLAG_MUTABLE instead of both – Ayoub EL ABOUSSI Feb 09 '23 at 10:06
  • Too much copy/paste. Use flags variable instead. and the pendingIntent creation will be one command instead of two. – Access Denied Aug 23 '23 at 08:14
  • Or ternary operator pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()).addFlags( Intent.FLAG_ACTIVITY_SINGLE_TOP), (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)? PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE: PendingIntent.FLAG_UPDATE_CURRENT); – Access Denied Aug 23 '23 at 08:22
6

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
  • 16,507
  • 12
  • 93
  • 99
6

Fixed by adding in app/build.gradle

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

For Future reference

add implementation 'androidx.work:work-runtime:2.7. to app/build.gradle

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

add | PendingIntent.FLAG_IMMUTABLE to line where you define PendingIntend

 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Bilal Şimşek
  • 5,453
  • 2
  • 19
  • 33
  • 1
    worth noting that if you have issues with metro this fixes the problem by setting the flag. Only good answer in this thread. – Mohamed Emad Jul 26 '23 at 13:51
4

I face the same issue, and as instructed in most of the answers I have updated my dependency as follows.

dependencies {
  /* Use either Kotlin or Java dependency as per your project */

  // ...
  /* Android Arch Component Work Manager for Kotlin */
  implementation 'androidx.work:work-runtime-ktx:2.7.0' 


  //...
  /* Android Arch Component Work Manager for Java */
  def work_version = "2.7.1"
  implementation "androidx.work:work-runtime:$work_version"
  // optional - Test helpers
  androidTestImplementation "androidx.work:work-testing:$work_version"
}

But adding only the above didn’t sort out my issue and the app still got crashed.

So I switched off the Android Lint: Missing Pending Intent Mutability, Find the steps below to switch off.

Switching off Pending Intent Mutability

Go to the Search option and search "PendingIntent" and you will receive a window as shown below.

Android Studio Search window

Switch of the Android Lint: Missing Pending Intent Mutability and you are good to go.

KZoNE
  • 1,249
  • 1
  • 16
  • 27
1

I was facing the same issue on Android 12 for Url_Launcher. after adding this to build.gradle file, I solved my problem.

dependencies {
  // ...
  implementation 'androidx.work:work-runtime-ktx:2.7.0'
}
hafeezrana
  • 21
  • 5
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,255
  • 1
  • 17
  • 30
0

If the Chuck library produces your problem, 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
  • 704
  • 7
  • 10
0

A few things, make sure you are using JDK 11, second delete that .gradle folder inside the android folder, then follow along on this wonderful answer SO answer.

Try after doing gradle clean

Prajval Singh
  • 501
  • 3
  • 9