From 3f9cc72309967585d1145e34da9d75a383379049 Mon Sep 17 00:00:00 2001 From: gooddev97 <135578805+gooddev97@users.noreply.github.com> Date: Mon, 24 Aug 2026 06:27:00 -0700 Subject: [PATCH] Prevent DeviceInfo crash when WindowMetrics API is missing (#58077) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Fixes https://github.com/react/react-native/issues/57785. Some modified Android runtimes report API 34 or later while shipping a `WindowMetrics` implementation without `getDensity()`. `WindowMetricsCalculator` then throws `NoSuchMethodError` while `NativeDeviceInfo` exports its constants, crashing the app before the JavaScript runtime is ready. This change catches only that missing-method failure around the window metrics lookup, reports it as a soft exception, and returns the resource display metrics that were already copied as the fallback. Normal Android runtimes continue to use the current window bounds path unchanged. The regression test injects a `WindowMetricsCalculator` that throws the same `NoSuchMethodError` and verifies that resource width, height, density, and density DPI are returned. Before the production change, this test failed at `DeviceInfoModule.kt:46` with the uncaught error. ## Changelog: [ANDROID] [FIXED] - Prevent a startup crash when the WindowMetrics API is missing on an inconsistent Android runtime Pull Request resolved: https://github.com/react/react-native/pull/58077 Test Plan: - `JAVA_HOME=$(/usr/libexec/java_home -v 17) ./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests com.facebook.react.modules.deviceinfo.DeviceInfoModuleTest -Preact.internal.useHermesStable=true --no-daemon` — BUILD SUCCESSFUL (8 tests) - `JAVA_HOME=$(/usr/libexec/java_home -v 17) ./gradlew :packages:react-native:ReactAndroid:ktfmtCheck -Preact.internal.useHermesStable=true --no-daemon --rerun-tasks` — BUILD SUCCESSFUL - `JAVA_HOME=$(/usr/libexec/java_home -v 17) ./gradlew :packages:react-native:ReactAndroid:lintDebug -Preact.internal.useHermesStable=true --no-daemon` — BUILD SUCCESSFUL Reviewed By: javache Differential Revision: D117178531 Pulled By: cipolleschi fbshipit-source-id: a3fe17d0d4af3d1536735617c62b472bd44c94e3 --- .../modules/deviceinfo/DeviceInfoModule.kt | 14 ++++++++++++- .../deviceinfo/DeviceInfoModuleTest.kt | 20 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.kt index 5f5f5a4308e..2d20ced6fb3 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/deviceinfo/DeviceInfoModule.kt @@ -43,7 +43,19 @@ internal class DeviceInfoModule(reactContext: ReactApplicationContext) : windowDisplayMetrics.setTo(reactApplicationContext.resources.displayMetrics) val activity = reactApplicationContext.currentActivity ?: return windowDisplayMetrics - val bounds = WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(activity).bounds + val bounds = + try { + WindowMetricsCalculator.getOrCreate().computeCurrentWindowMetrics(activity).bounds + } catch (error: NoSuchMethodError) { + ReactSoftExceptionLogger.logSoftException( + NAME, + ReactNoCrashSoftException( + "WindowMetrics API is unavailable; falling back to resource display metrics.", + error, + ), + ) + return windowDisplayMetrics + } if (isEdgeToEdgeFeatureFlagOn) { windowDisplayMetrics.widthPixels = bounds.width() diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/deviceinfo/DeviceInfoModuleTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/deviceinfo/DeviceInfoModuleTest.kt index a0d5b7de8ac..a609d5b5d36 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/deviceinfo/DeviceInfoModuleTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/modules/deviceinfo/DeviceInfoModuleTest.kt @@ -208,6 +208,26 @@ class DeviceInfoModuleTest : TestCase() { } } + @Test + fun getWindowDisplayMetrics_fallsBackWhenWindowMetricsApiIsMissing() { + val activity = mock() + doReturn(activity).whenever(reactContext).currentActivity + + val calculator = mock() + whenever(calculator.computeCurrentWindowMetrics(activity)) + .thenThrow(NoSuchMethodError("WindowMetrics.getDensity()")) + + withWindowMetricsCalculator(calculator) { + val resourceMetrics = reactContext.resources.displayMetrics + val metrics = deviceInfoModule.getWindowDisplayMetrics() + + assertThat(metrics.widthPixels).isEqualTo(resourceMetrics.widthPixels) + assertThat(metrics.heightPixels).isEqualTo(resourceMetrics.heightPixels) + assertThat(metrics.density).isEqualTo(resourceMetrics.density) + assertThat(metrics.densityDpi).isEqualTo(resourceMetrics.densityDpi) + } + } + private fun givenDisplayMetricsHolderContains(fakeDisplayMetrics: WritableMap?) { doReturn(fakeDisplayMetrics).whenever(deviceInfoModule).getDisplayMetricsWritableMap() }