Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. #287

Open
vinod-takhar opened this issue Dec 21, 2023 · 2 comments

Comments

@vinod-takhar
Copy link

FATAL EXCEPTION: main
Process: com.hornblower.statuecruises, PID: 16525
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hornblower.statuecruises/com.hornblower.activity.ArActivityKt}: java.lang.IllegalArgumentException: com.hornblower.statuecruises: 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.ActivityThread.performLaunchActivity(ActivityThread.java:3802)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3942)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2448)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:205)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8194)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
Caused by: java.lang.IllegalArgumentException: com.hornblower.statuecruises: 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.checkPendingIntent(PendingIntent.java:450)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:753)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:740)
at com.mapbox.android.telemetry.AlarmSchedulerFlusher.register(AlarmSchedulerFlusher.java:31)
at com.mapbox.android.telemetry.MapboxTelemetry.startAlarm(MapboxTelemetry.java:394)
at com.mapbox.android.telemetry.MapboxTelemetry.startTelemetry(MapboxTelemetry.java:388)
at com.mapbox.android.telemetry.MapboxTelemetry.enable(MapboxTelemetry.java:132)
at com.mapbox.services.android.navigation.v5.navigation.NavigationMetricsWrapper.init(NavigationMetricsWrapper.java:30)
at com.mapbox.services.android.navigation.v5.navigation.NavigationTelemetry.initialize(NavigationTelemetry.java:124)
at com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation.initializeTelemetry(MapboxNavigation.java:921)
at com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation.initialize(MapboxNavigation.java:891)
at com.mapbox.services.android.navigation.v5.navigation.MapboxNavigation.(MapboxNavigation.java:121)
at com.hornblower.activity.ArActivityKt.startNavigation(ArActivity.kt:155)
at com.hornblower.activity.ArActivityKt.onPermissionsGranted(ArActivity.kt:92)
at com.hornblower.activity.ArBaseActivity.onCreate(ArBaseActivity.java:64)
at android.app.Activity.performCreate(Activity.java:8621)
at android.app.Activity.performCreate(Activity.java:8599)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1456)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3784)
... 12 more

@reggirepret
Copy link

hi, any update on this?

@RamenChan
Copy link

Hi @vinod-takhar ,

To resolve the IllegalArgumentException related to PendingIntent when targeting Android S+ (API level 31 and above), you need to specify either FLAG_IMMUTABLE or FLAG_MUTABLE when creating a PendingIntent. This change is required due to the updated security requirements in Android 12 (API level 31) and above. Here's a detailed explanation and steps to fix the issue:

Explanation
When your application targets Android S (API level 31) or higher, the system enforces that you specify the mutability of PendingIntent objects. This is to ensure that the PendingIntent cannot be altered by malicious actors. The FLAG_IMMUTABLE flag makes the PendingIntent immutable, meaning it cannot be modified once created. The FLAG_MUTABLE flag allows the PendingIntent to be modified.

Steps to Fix
Identify the PendingIntent Creation: Locate where the PendingIntent is being created in your code. In this case, the stack trace points to AlarmSchedulerFlusher.register and other related methods in the Mapbox library.

Modify the PendingIntent Creation: Add either FLAG_IMMUTABLE or FLAG_MUTABLE to the PendingIntent creation. It's generally recommended to use FLAG_IMMUTABLE unless you specifically need the PendingIntent to be mutable.

Here's how you can modify the code:

Example Modification
Before:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, 0);

After:

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_IMMUTABLE);

Detailed Steps
Locate PendingIntent Creation in Mapbox Library:

If you are using the Mapbox library and don't have direct control over the source code, check if there's an updated version of the library that addresses this issue.
If not, you might need to fork the library or contribute a patch.
Update Your Code:

Find all instances where PendingIntent is created in your codebase.
Update the creation of PendingIntent to include the appropriate flag.
Check for Updates:

Ensure that all dependencies are up-to-date, as library maintainers may have already addressed this issue in newer releases.

Example in Context
ArActivity.kt:

// Locate where the PendingIntent is created and modify it
val pendingIntent: PendingIntent = PendingIntent.getBroadcast(
this,
requestCode,
intent,
PendingIntent.FLAG_IMMUTABLE
)
Stack Trace Analysis
From the stack trace provided, the relevant part is
Caused by: java.lang.IllegalArgumentException: com.hornblower.statuecruises: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.

This indicates that the PendingIntent creation in the Mapbox library (or possibly your code that interacts with it) does not specify the required flag.

Additional Notes
Always prefer FLAG_IMMUTABLE unless you have a specific reason to use FLAG_MUTABLE.
Review the official Android documentation on PendingIntent for more details.
By following these steps and updating the PendingIntent creation in your code, you should be able to resolve the IllegalArgumentException and ensure compatibility with Android S+ (API level 31 and above).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants