Android 12 (API Level 31) Issue: App crashes due to missing flag on PendingIntent


Audience
Tags

I have an app that uses the DS457 to scan QR codes. This has all been working perfectly but the Google Play store now requires that apps target an API level of 31. I've updated our app to target this, but I'm now getting issues with the scanner. When the scanner is plugged in I get the following error:

11-22 11:51:37.468 24725 24725 E AndroidRuntime: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.hardware.usb.action.USB_DEVICE_ATTACHED flg=0x11000010 (has extras) } in com.zebra.scannercontrol.SDKHandler$1@8038304
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1689)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args$$ExternalSyntheticLambda0.run(Unknown Source:2)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:938)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.os.Looper.loopOnce(Looper.java:201)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.os.Looper.loop(Looper.java:288)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:7842)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: Caused by: java.lang.IllegalArgumentException: id.reath.spoke.debug: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
11-22 11:51:37.468 24725 24725 E AndroidRuntime: 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.
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.app.PendingIntent.checkFlags(PendingIntent.java:375)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:645)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.app.PendingIntent.getBroadcast(PendingIntent.java:632)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at com.zebra.scannercontrol.USBManager.getAvailableScannersList(USBManager.java:81)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at com.zebra.scannercontrol.SDKHandler.handleUSBDeviceAttach(SDKHandler.java:1684)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at com.zebra.scannercontrol.SDKHandler.access$800(SDKHandler.java:52)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at com.zebra.scannercontrol.SDKHandler$1.onReceive(SDKHandler.java:1643)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1679)
11-22 11:51:37.468 24725 24725 E AndroidRuntime: ... 9 more

I've tried upgrading to use the v2.6.10 version of the barcode scanner library, but this issue still persists. Has the library been updated to support this? As part of this upgrade, Google have said "Pending intent mutability: You must specify the mutability of each PendingIntent object that your app creates." (Ref: https://developer.android.com/google/play/requirements/target-sdk#pre12). If this has been updated, do you have any advice on what to look at to get this to stop crashing our app?

Thanks.

Submitted by kimberley@reath.id on November 22, 2022 Permalink

Thanks for getting back to me! We don't actually call PendingIntent in our code so I'm not sure what I need to change. I think this might be called within the Zebra Scanner Control library in the USBManager based on the stack trace that we are getting back.