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

We Are Going To Discuss About Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified. So lets Start this Java Article.

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

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

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">

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

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 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">

Original Author HaryanviDeveloper Of This Content

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

Original Author Saksham Pruthi Of This Content

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

Original Author Kevin ABRIOUX Of This Content

Solution 4

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

Original Author Dean Amilush Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment