89

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.(Unknown Source:67) at com.google.android.gms.internal.gtm.zzap.zzc(Unknown Source:82) at com.google.android.gms.analytics.GoogleAnalytics.getInstance(Unknown Source:15) at di.internal.module.ApplicationModule.providesGoogleAnalyticsLogger$app_developmentDebug(ApplicationModule.kt:339) at di.internal.module.ApplicationModule_ProvidesGoogleAnalyticsLogger$app_developmentDebugFactory.providesGoogleAnalyticsLogger$app_developmentDebug(ApplicationModule_ProvidesGoogleAnalyticsLogger$app_developmentDebugFactory.java:47) at di.internal.module.ApplicationModule_ProvidesGoogleAnalyticsLogger$app_developmentDebugFactory.get(ApplicationModule_ProvidesGoogleAnalyticsLogger$app_developmentDebugFactory.java:36) at di.internal.module.ApplicationModule_ProvidesGoogleAnalyticsLogger$app_developmentDebugFactory.get(ApplicationModule_ProvidesGoogleAnalyticsLogger$app_developmentDebugFactory.java:11) at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) at di.internal.module.ApplicationModule_ProvidesMultiAnalyticsLogger$app_developmentDebugFactory.get(ApplicationModule_ProvidesMultiAnalyticsLogger$app_developmentDebugFactory.java:35) at di.internal.module.ApplicationModule_ProvidesMultiAnalyticsLogger$app_developmentDebugFactory.get(ApplicationModule_ProvidesMultiAnalyticsLogger$app_developmentDebugFactory.java:10) at dagger.internal.DoubleCheck.get(DoubleCheck.java:47) at di.internal.component.DaggerIProdApplicationComponent.injectChApplication(DaggerIProdApplicationComponent.java:941) 2021-07-02 11:18:17.611 22561-22561/com.chrobinson.navispherecarrier.dev E/AndroidRuntime: at di.internal.component.DaggerIProdApplicationComponent.inject(DaggerIProdApplicationComponent.java:876) at com.chrobinson.navispherecarrier.ChApplication.onCreate(ChApplication.kt:90) at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1211) at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6682)

Adam Gardner
  • 1,216
  • 1
  • 9
  • 21
  • `com.google.android.gms.analytics.GoogleAnalytics.getInstance` -- are you using Google Analytics? – CommonsWare Jul 02 '21 at 16:38
  • I have some legacy Google analytics events I have not switched over to Firebase. – Adam Gardner Jul 02 '21 at 20:20
  • Based on the stack trace, that is where your crash is coming from. By next year, when you will need to get `targetSdkVersion` up to `31`, you are going to need to upgrade this dependency (and perhaps others). – CommonsWare Jul 02 '21 at 20:30
  • I have `com.google.android.gms:play-services-analytics` set to 17.0.0 and thats all I see on the docs. Maybe its time to just full rip out Google Analytics and be fully on Firebase. – Adam Gardner Jul 02 '21 at 20:51
  • 1
    I have the same issue with API 31 (Android S works fine). I think there are some libraries that are not updated. I suspect WorkManager is one of these (maybe the method that checks the Android version is not updated). – iClaude Jul 17 '21 at 15:38

17 Answers17

75

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

jomin v george
  • 1,299
  • 11
  • 26
  • 2
    This should be the accepted answer! – PieterAelse Sep 28 '21 at 15:32
  • 2
    This is not a bug, the bug fix you are seeing there says that they made it explicit to fix a crash, so the current state is the correct state. The release you are referring to is just the alpha version of 2.7.0, the actual release was made after this alpha version. So what you are doing is actually a regression. You can instead just use `PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT` – Aegletes Oct 17 '21 at 02:17
  • @Aegletes But I have tried PendingIntent.FLAG_IMMUTABLE before including this alpha version and still i got that exception. – jomin v george Oct 17 '21 at 14:04
  • @jominvgeorge could you try exactly `PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT` or in there is a statement, I am not saying either one of them :) – Aegletes Oct 17 '21 at 15:52
  • 3
    I lost a week checking my code for possible errors with the flags and there was none. And now I add this line everything works as expected. Why Google makes our life so hard some times. Did they even check their releases or in order to meet deadlines they release unchecked code? Anyway, THANK YOU very much for this. You saved me a lot of headaches! – Nick Oct 21 '21 at 06:43
  • 1
    Adding the latest version solved my problem. ```def work_version = "2.7.0" // (Java only) implementation "androidx.work:work-runtime:$work_version"``` – Arshad Mehmood Oct 22 '21 at 12:57
  • Add it to app level build.gradle – Ataberk Nov 28 '21 at 21:10
  • We have this already in ours implementation "androidx.work:work-runtime-ktx:2.7.1" and it fails only on a android UI test. I wonder if the fix is not in for ktx libs – JPM Mar 01 '22 at 19:29
  • This version have worked perfectly – Mohammad Arman Apr 15 '22 at 11:06
  • The solution did not work for Android 12, Pixel 3 XL. – Vadym Vikulin May 16 '22 at 11:34
  • Also you can see this https://stackoverflow.com/a/72363910/12225347 – Codeplayon May 24 '22 at 13:37
  • Worked like magic! – Taslim Oseni Sep 22 '22 at 15:29
  • still have this issue. i'm not using PendingIntent. – chanrlc Mar 24 '23 at 01:28
  • This has worked for me Thank you so much. @jominvgeorge – Thilina Chamika Aug 27 '23 at 13:47
40

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'
  • 1
    It's worth to mention what is missing in the documentation: you will also need `FLAG_MUTABLE` when using `setPendingIntentTemplate` on a `RemoveViews` (used e.g. to detect click events on widget list items). – Derek K Mar 05 '22 at 14:11
  • Also change the pndingIntent in service class like this https://stackoverflow.com/a/72363910/12225347 – Codeplayon May 24 '22 at 13:38
  • Thank's for the answer, work pretty good. A tip, you can use the latest version of the library, in my case I used implementation 'androidx.work:work-runtime-ktx:2.7.1' – Desilio Neto May 28 '22 at 09:28
25

Add your pending intent like this:

 PendingIntent.getActivity(getApplicationContext(), 0, intent,PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT)
17

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"
Nick
  • 1,393
  • 1
  • 14
  • 22
4

There are two steps

1- Add androidx.work which is shown in the Error message

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

2- Fix PendingIntent by Chang or add FLAG_IMMUTABLE

PendingIntent pendingIntent =
                PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);
    
Mori
  • 2,653
  • 18
  • 24
2

I experience a SIMILAR PROBLEM on android 12.

PendingIntent.java – line 375 android.app.PendingIntent.checkFlags enter image description here

It was solved by updating Onesignal Dependency.

    implementation 'com.onesignal:OneSignal:4.6.3'
Rohaitas Tanoli
  • 624
  • 4
  • 16
2

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.

Nihas Nizar
  • 619
  • 8
  • 15
2

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

2

If you're using the Google AdMob SDK (version 20.4.0), they recommend adding this constraint in the AdMob official release notes.

Link: https://developers.google.com/admob/android/rel-notes?hl=en

dependencies {
  implementation 'com.google.android.gms:play-services-ads:20.4.0'

  // For apps targeting Android 12, add WorkManager dependency.
  constraints {
    implementation('androidx.work:work-runtime:2.7.0') {
      because '''androidx.work:work-runtime:2.1.0 pulled from
      play-services-ads has a bug using PendingIntent without
      FLAG_IMMUTABLE or FLAG_MUTABLE and will fail in Apps
      targeting S+.'''
    }
  }
}
suspicioususer
  • 161
  • 1
  • 5
1

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.

Adam Gardner
  • 1,216
  • 1
  • 9
  • 21
  • No need to completely remove Google Analytics, just update to its latest version. I have both working at the same time. Experienced this issue while upgrading target and compile SDK to 33 My working project dependencies (sorry for the format, idea is there) `implementation platform("com.google.firebase:firebase-bom:30.3.1") implementation "com.google.firebase:firebase-analytics implementation "com.google.android.gms:play-services-analytics:18.0.1 ` – amartin1911 Aug 31 '22 at 18:02
1

If you happen to use Google Analytics and already tried changing flags, just update the aforementioned lib to its latest version.

implementation "com.google.android.gms:play-services-analytics:18.0.1"

Furthermore, if you're also using Firebase Analytics, both could live at the same time:

implementation platform("com.google.firebase:firebase-bom:30.3.1")
implementation "com.google.firebase:firebase-analytics"
implementation "com.google.android.gms:play-services-analytics:18.0.1"
amartin1911
  • 512
  • 5
  • 11
1

u Need this,

dependencies {
    implementation 'androidx.work:work-runtime:2.7.1'
    implementation 'com.google.firebase:firebase-messaging:21.0.1'
    implementation('com.google.firebase:firebase-iid:21.1.0')
    implementation platform('com.google.firebase:firebase-bom:31.1.0')
}
0

Upgrading Google Play Services and Firebase libraries to the latest version solved this problem for me.

MartijndeM
  • 146
  • 1
  • 9
0

Updating the WorkManager library isn't probably gonna be enough. There might also be other libraries that are using PendingIntent without the mutability flag, especially when you are working on a huge project. To find out this, use android studio's code inspection tool that will list down all the places where the Pending intent is used without a mutability flag.

Inspection results -> Android -> Lint -> Security

Harshal Pudale
  • 178
  • 1
  • 2
  • 7
0

I have resolved my issue by doing this

PendingIntent pendingIntent = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
        pendingIntent = PendingIntent.getActivity
               (this, 0, notificationIntent, PendingIntent.FLAG_MUTABLE);
    }
    else
    {
         pendingIntent = PendingIntent.getActivity
                (this, 0, notificationIntent,0);
    }
0

In my specific case non of the mentioned answers worked, this was because I was already setted most of the dependencies they mention.

Something that work in my specific case was to change the version of the com.google.android.gms:play-services-analytics dependency, I had setted 15.0.1, and this was causing the issue. After I update to

implementation "com.google.android.gms:play-services-analytics:18.0.1"

Solve my issue.

Cristian Zumelzu
  • 842
  • 10
  • 15
0

I resolved mine by using firebasebom dependency, which basically used the latest version of every firebase SDK i have installed especially (firebasemessaging)

dependencies {
 // Import the BoM for the Firebase platform
 implementation platform('com.google.firebase:firebase-bom:31.2.3')

 // Declare the dependencies for the desired Firebase products without 
 specifying versions
 // For example, declare the dependencies for Firebase Authentication 
 and Cloud Firestore
 implementation 'com.google.firebase:firebase-auth'
 implementation 'com.google.firebase:firebase-firestore'
}

and also updated

val pendingIntent = PendingIntent.getActivity(this,
        0,
        resultIntent,
        PendingIntent.FLAG_IMMUTABLE
    )

 val pendingIntent = PendingIntent.getActivity(this,
        0,
        resultIntent,
        PendingIntent.FLAG_IMMUTABLE
    )