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

12

73

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

 }
Pawpaw answered 28/1, 2022 at 12:59 Comment(1)
But my project didn't use PeningIntent, also cause the error!Harker
J
76

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">
Johann answered 3/2, 2022 at 15:47 Comment(6)
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.Twister
Yes I also found it on Samsung and HTC devices. I will post solutions once I solve itJohann
Please take a look at this comment github.com/facebook/react-native/issues/…Jabalpur
Thanks @HamzaHmem is this working for you ? did you try?Johann
@Johann 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.Jabalpur
@HamzaHmem thanks could you please tell me where exactly we need to change?Johann
P
26

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);
  }
Proboscidean answered 3/2, 2022 at 14:40 Comment(4)
Cool, but why should we check the version 23 and above when the error message is "Targeting S+ (version 31 and above)"Bandur
@Bandur 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) onwardsProboscidean
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.Corina
what is flutter kotlin version of code?Harker
K
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);
}
Krawczyk answered 7/6, 2022 at 4:4 Comment(3)
Worked for me, but i had to specify only FLAG_MUTABLE instead of bothWarrantable
Too much copy/paste. Use flags variable instead. and the pendingIntent creation will be one command instead of two.Innis
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);Innis
C
10

For Future reference

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

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

add | PendingIntent.FLAG_IMMUTABLE to line where you define PendingIntent

 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE);
Coppock answered 20/2, 2023 at 8:29 Comment(1)
worth noting that if you have issues with metro this fixes the problem by setting the flag. Only good answer in this thread.Dipteral
P
7

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)
Possum answered 8/4, 2022 at 9:22 Comment(0)
A
6

Fixed by adding in app/build.gradle

dependencies {
    // ...
    implementation 'androidx.work:work-runtime-ktx:2.7.0'
}
Absolute answered 12/1, 2023 at 14:19 Comment(0)
D
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.

Dugas answered 3/7, 2022 at 3:25 Comment(0)
C
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'
}
Cash answered 11/2, 2023 at 8:52 Comment(1)
This line of code not working for me. The error remains the same!Harker
F
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

Fein answered 21/4, 2022 at 6:31 Comment(0)
B
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.

Brittni answered 25/5, 2022 at 12:14 Comment(0)
S
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

Saguache answered 8/1, 2023 at 8:54 Comment(0)
G
0

According to this guide, everyone used com.google.androidbrowserhelper:androidbrowserhelper:2.2.0. The newer version (2.5.0) fixed the problem:

dependencies {
  // ...
  implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.5.0'
}
Gaither answered 6/4 at 13:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.