CameraManager.getCameraCharacteristics() crash in background state with "IllegalArgumentException: Unable to find task ID" on Android 16
The following crashes occur only on Android 16 devices when the app is in the background state (100% based on Firebase Crashlytics, check the attached screenshot).
My application is a video recording app that runs with a foreground service, which allows it to continue working (using camera) while the app is in the background.
The crashes are reported by Firebase Crashlytics, so I have not been able to reproduce the issue yet or provide a minimal sample project.
The app currently has about 600,000 installed users. According to Crashlytics, the issue has affected 109 users (319 events) so far. So far, it happens rarely, and I can't reproduce it on the same devices.
Stack trace:
Fatal Exception: java.lang.IllegalArgumentException: Unable to find task ID 5429
at android.os.Parcel.createExceptionOrNull(Parcel.java:3377)
at android.os.Parcel.createException(Parcel.java:3357)
at android.os.Parcel.readException(Parcel.java:3340)
at android.os.Parcel.readException(Parcel.java:3282)
at android.app.IAppTask$Stub$Proxy.getTaskInfo(IAppTask.java:249)
at android.app.ActivityManager$AppTask.getTaskInfo(ActivityManager.java:6376)
at android.hardware.camera2.CameraManager.getRotationOverride(CameraManager.java:1674)
at android.hardware.camera2.CameraManager.getRotationOverride(CameraManager.java:1653)
at android.hardware.camera2.CameraManager.getCameraCharacteristics(CameraManager.java:714)
at com.MY_APP_HIDDEN.utilities.CameraUtil.getCameraCharacteristics(CameraUtil.kt:296)
Similar with a different task ID:
Unable to find task ID 147
Unable to find task ID 13649
Unable to find task ID 3420
Unable to find task ID ANY_NUMBER
The crashes occur on the following devices (mostly Pixel devices, some newer Samsung models, and one Sony Xperia device):
Galaxy S10e (Android 16)
Galaxy S23 Ultra (Android 16)
Galaxy S25 Ultra (Android 16)
Galaxy S26 (Android 16)
Galaxy S26 Ultra (Android 16)
Pixel 10 (Android 16)
Pixel 10 Pro (Android 16)
Pixel 10 Pro Fold (Android 16)
Pixel 10 Pro XL (Android 16)
Pixel 6 (Android 16)
Pixel 6 Pro (Android 16)
Pixel 6a (Android 16)
Pixel 7 (Android 16)
Pixel 7 Pro (Android 16)
Pixel 7a (Android 16)
Pixel 8 (Android 16)
Pixel 8 Pro (Android 16)
Pixel 8a (Android 16)
Pixel 9 (Android 16)
Pixel 9 Pro (Android 16)
Pixel 9 Pro Fold (Android 16)
Pixel 9 Pro XL (Android 16)
Pixel 9a (Android 16)
Pixel Fold (Android 16)
Xperia 1 V (Android 16)
It is unclear why CameraManager.getCameraCharacteristics() internally calls android.app.ActivityManager$AppTask.getTaskInfo
From a Android developer perspective, CameraManager.getCameraCharacteristics() should be safe to call at any time and should not depend on the application task state.
I am having a bit of difficulty tracking down source code that matches the stack trace, but in the now-current main, that getTaskInfo() call is in here:
@FlaggedApi(com.android.window.flags.Flags.FLAG_ENABLE_CAMERA_COMPAT_FOR_DESKTOP_WINDOWING) @TestApi public static int getRotationOverrideInternal(@Nullable Context context, @Nullable PackageManager packageManager, @Nullable String packageName) { if (!CameraManagerGlobal.sLandscapeToPortrait) { return ICameraService.ROTATION_OVERRIDE_NONE; } if (context != null) { final ActivityManager activityManager = context.getSystemService(ActivityManager.class); if (activityManager != null) { for (ActivityManager.AppTask appTask : activityManager.getAppTasks()) { final TaskInfo taskInfo = appTask.getTaskInfo(); final int freeformCameraCompatMode = taskInfo.appCompatTaskInfo .cameraCompatTaskInfo.freeformCameraCompatMode; if (freeformCameraCompatMode != 0 && taskInfo.topActivity != null && taskInfo.topActivity.getPackageName().equals(packageName)) { // WindowManager has requested rotation override. return getRotationOverrideForCompatFreeform(freeformCameraCompatMode); } } } } if (packageManager != null && packageName != null) { try { return packageManager.getProperty( PackageManager.PROPERTY_COMPAT_OVERRIDE_LANDSCAPE_TO_PORTRAIT, packageName).getBoolean() ? ICameraService.ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT : ICameraService.ROTATION_OVERRIDE_NONE; } catch (PackageManager.NameNotFoundException e) { // No such property } } return CompatChanges.isChangeEnabled(OVERRIDE_CAMERA_LANDSCAPE_TO_PORTRAIT) ? ICameraService.ROTATION_OVERRIDE_OVERRIDE_TO_PORTRAIT : ICameraService.ROTATION_OVERRIDE_NONE; }
It appears as though they are trying to determine if the camera output needs to be rotated in support of freeform multi-window support. I can see why that might be tied to tasks. But getTaskInfo() is a fragile call; IMHO they should be gracefully degrading if they cannot get the task info.
I see that you(?) filed a bug report, and with luck they will address this. In the meantime, you might need to add your own exception handling logic around getCameraCharacteristics() and do something (e.g., use the last-known values that you cache somewhere).
CommonsWare