Targeting S+ (version 10000 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent
AndroidAndroid StudioAndroid Problem Overview
Trying to update my application to Android S and running into some issues as the Title/error says. I get the error
> Targeting S+ (version 10000 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.
I only have 1 PendingIntent within my code for notifications and I added the Flag
PendingIntent.getActivity(
mContext,
0 /* Request code */,
intentOptional.get(),
PendingIntent.FLAG_IMMUTABLE
)
Reading Google's documentation this should be all I need to for this security update within Android S. I did find a couple month old post on here that asked something similar and someone said to add WorkManager https://stackoverflow.com/a/67181567/4219444 into project even if you do not use it. So I added
def work_version = "2.7.0-alpha04"
implementation "androidx.work:work-runtime-ktx:$work_version"
This didnt help at all as I still receive the error. Does anyone know if this is common issue with Android S upgrades or does it check libraries also? Stuck as the app just keeps crashing and not sure what to do.
I have created a application with none of my libraries and used the same PendingIntent and was able to run a basic hello world application with the pending intent. The full error I receive from the project I am trying to get to compile is:
> Targeting S+ (version 10000 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.gms.internal.gtm.zzbv.zzfe(Unknown Source:52)
at com.google.android.gms.internal.gtm.zzbv.cancel(Unknown Source:54)
at com.google.android.gms.internal.gtm.zzbv.zzaw(Unknown Source:4)
at com.google.android.gms.internal.gtm.zzan.zzag(Unknown Source:7)
at com.google.android.gms.internal.gtm.zzap.
Android Solutions
Solution 1 - Android
It is solved by adding
implementation 'androidx.work:work-runtime:2.7.0-alpha05'
in my gradle. (You can add the latest release version of this library)
Seems like it is a bug in workmanager. Check the bug fix in Version 2.7.0-alpha02
https://developer.android.com/jetpack/androidx/releases/work#2.7.0-alpha02
Solution 2 - Android
According to documentation, "It is strongly recommended to use FLAG_IMMUTABLE when creating a PendingIntent. FLAG_MUTABLE should only be used when some functionality relies on modifying the underlying intent, e.g. any PendingIntent that needs to be used with inline reply or bubbles."
So use this PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
Instead of just PendingIntent.FLAG_UPDATE_CURRENT
e.g PendingIntent.getActivity(context, yourRequestID, yourIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
Because "FLAG_UPDATE_CURRENT still works even if FLAG_IMMUTABLE is set"
If the error still remains and your targetSdkVersion = 31 then the error must caused because one of your dependencies is internally using WorkManager or your are directly using old version of WorkManager.
To solve simply add this dependency
implementation 'androidx.work:work-runtime-ktx:2.7.0'
Solution 3 - Android
Well after reading the answers here I found out this in Android's website:
> Note: WorkManager Version 2.7.0 is required for apps targeting Android > 12 (S).
https://developer.android.com/jetpack/androidx/releases/work
So adding this dependency will make it work.
def work_version = "2.7.0"
// (Java only)
implementation "androidx.work:work-runtime:$work_version"
// Kotlin + coroutines
implementation "androidx.work:work-runtime-ktx:$work_version"
Solution 4 - Android
Add your pending intent like this:
PendingIntent.getActivity(getApplicationContext(), 0, intent,PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT)
Solution 5 - Android
So to solve this issue with the Android S update, I was able to spend some time and removing Google Analytics completely and replacing it with Firebase Analytics and Crashlytics.
I am not fully sure if this is because Google hasn't/wont update Google Analytics for Android S or what.
Solution 6 - Android
I updated my work-runtime-ktx
version to 2.7.1
After the above change i got into another error
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type: ErrorScope{Error scope for class <ERROR CLASS> with arguments: org.jetbrains.kotlin.types.IndexedParametersSubstitution@14ac19e7}
Look how i solved the above error by updating kotlin-gradle-plugin
version here.
Solution 7 - Android
I experience a SIMILAR PROBLEM on android 12. > PendingIntent.java – line 375 android.app.PendingIntent.checkFlags
It was solved by updating Onesignal Dependency.
implementation 'com.onesignal:OneSignal:4.6.3'
Solution 8 - Android
Upgrading Google Play Services and Firebase libraries to the latest version solved this problem for me.
Solution 9 - Android
In Android Studio go to:
File -> Project Structure -> Dependencies -> app,
check there if any underlined dependencies with suggestions, if so, let the studio implement that correctly.
THIS and implementation 'androidx.work:work-runtime-ktx:2.7.1' Solved the problem to me.
p.s. You can not see that kind suggestions in the gradle