Home > front end >  Main Activity fails to start up due to PendingIntent Flag error
Main Activity fails to start up due to PendingIntent Flag error

Time:12-07

I'm developing a system app, and when I attempt to start it up from the home screen in Android 31, it fails and an java.lang.IllegalArgumentException is thrown. Here's what the stack trace looks like:

java.lang.IllegalArgumentException: tech.[brand].[name]: 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 androidx.work.impl.utils.ForceStopRunnable.getPendingIntent(ForceStopRunnable.java:285)
        at androidx.work.impl.utils.ForceStopRunnable.isForceStopped(ForceStopRunnable.java:158)
        at androidx.work.impl.utils.ForceStopRunnable.forceStopRunnable(ForceStopRunnable.java:185)
        at androidx.work.impl.utils.ForceStopRunnable.run(ForceStopRunnable.java:103)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:91)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:920)

The stack doesn't reach any of my source code and far as I know I'm not calling any intent in my code so far, pending or otherwise. Does anyone know what's going on?

CodePudding user response:

PendingIntent is a flag in API 21 .So either you can set it like this:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
  PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
} 
else {
 PendingIntent.FLAG_UPDATE_CURRENT 
}

OR

According to the docs here , one should choose FLAG_IMMUTABLE. Use FLAG_MUTABLE in case if some functionality depends on the PendingIntent

val updatedPendingIntent = PendingIntent.getActivity(
   applicationContext,
   NOTIFICATION_REQUEST_CODE,
   updatedIntent,
   PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
)

CodePudding user response:

The androidx.work in the stack trace is WorkManager. As per the WorkManager 2.7.0-alpha02:

Make PendingIntent mutability explicit, to fix a crash when targeting Android 12.

Therefore you should upgrade to the latest version, WorkManager 2.7.1 by adding an explicit dependency on that latest version to the dependencies block of your build.gradle file:

implementation "androidx.work:work-runtime:2.7.1"
  • Related