Android 12 run: Fatal Exception Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE

I created Android freshly with cap add android. App is working on Android 11 without problems

When I try to run my app on Android 12, I get this error in Android Studio run console

.E/AndroidRuntime: FATAL EXCEPTION: pool-12-thread-1
    Process: com.restegourmet.rg_eat, PID: 32757
    java.lang.IllegalArgumentException: com.mydomain.myapp: 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:382)
        at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:673)
        at android.app.PendingIntent.getBroadcast(PendingIntent.java:660)
        at androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:174)
        at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:108)
        at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:86)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
        at java.lang.Thread.run(Thread.java:1012)

Im using @capacitor/android”: “4.4.0”,`

Any idea how to fix this?

There is some plugin that needs to be updated, would be helpful if you provide your plugin list.
npx cap ls should list all your capacitor plugins, or you can check the package.json

Thank you @julio-ionic for this quick answer. Here is my list:

[info] Found 13 Capacitor plugins for android:
       @capacitor-community/admob@4.0.0
       @capacitor-community/fcm@3.0.0
       @capacitor-community/firebase-analytics@1.0.1
       @capacitor-community/keep-awake@3.0.0
       @capacitor/app@4.1.0
       @capacitor/app-launcher@4.0.1
       @capacitor/browser@4.0.1
       @capacitor/device@4.0.1
       @capacitor/network@4.0.1
       @capacitor/push-notifications@4.1.1
       @capacitor/splash-screen@4.1.1
       @capacitor/status-bar@4.0.1
       capacitor-rate-app@3.0.0
[info] Found 1 Cordova plugin for android:
       cordova-plugin-purchase@11.0.0
[info] Found 14 Capacitor plugins for ios:
       @capacitor-community/admob@4.0.0
       @capacitor-community/fcm@3.0.0
       @capacitor-community/firebase-analytics@1.0.1
       @capacitor-community/keep-awake@3.0.0
       @capacitor/app@4.1.0
       @capacitor/app-launcher@4.0.1
       @capacitor/browser@4.0.1
       @capacitor/device@4.0.1
       @capacitor/network@4.0.1
       @capacitor/push-notifications@4.1.1
       @capacitor/splash-screen@4.1.1
       @capacitor/status-bar@4.0.1
       capacitor-plugin-app-tracking-transparency@2.0.3
       capacitor-rate-app@3.0.0
[info] Found 1 Cordova plugin for ios:
       cordova-plugin-purchase@11.0.0

The problem is with the androidx.work:work-runtime dependency version, but I can’t see any of those plugins using that dependency.

If you run

cd android
./gradlew androidDependencies

do you see androidx.work:work-runtime listed?

The fcm and firebase analytics are using old dependencies, so might be using that dependency

@julio-ionic, yes, androidx.work:work-runtime is listed

You can find the whole output here: Pastebin link

What can I do to get around this?

Looks like the problem is admob plugin, but it’s weird because version 4 uses implementation 'com.google.android.gms:play-services-ads:21.1.0', but your app is getting com.google.android.gms:play-services-ads:20.4.0 which is what causes the problem
admob/build.gradle at f59fc834ac9d9a64190fa8378344e366c7b00e1a · capacitor-community/admob · GitHub.

Try uninstalling it, reinstalling it and running npx cap sync

Ahhhh, argh: This is caused because I temporary changed version to 20.4.0 to try to get around this bug: "Duplicate class found" error if play-services-ads:21.2.0 is used - #2 by julio-ionic

Ok, so this one is closed, thank you @julio-ionic !!! But the other one is still not fixed. Any idea?

try forcing androidx.work:work-runtime to version 2.7.0 or 2.7.1, those versions support targeting SDK 31 or newer.

Thank you, this works. Is it possible to force those versions without changing the code of the @capacitor-community/AdMob Plugin?

You can force it like this for all modules, but not sure if admob plugin could break if the newer version is not compatible or introduced breaking changes

configurations.all {
  resolutionStrategy { 
    force 'androidx.work:work-runtime:2.7.1'
  }
}