Skip to content

Fix NoSuchMethodError: WindowMetrics.getDensity() crash on launch - #3053

Merged
panasetskaya merged 1 commit into
developfrom
fix/windowmetrics-getdensity-crash
Aug 13, 2026
Merged

Fix NoSuchMethodError: WindowMetrics.getDensity() crash on launch#3053
panasetskaya merged 1 commit into
developfrom
fix/windowmetrics-getdensity-crash

Conversation

@StylianosGakis

Copy link
Copy Markdown
Member

Problem

The app crashes on launch on some Android 14 devices, before the first frame:

Fatal Exception: java.lang.NoSuchMethodError: No virtual method getDensity()F
in class Landroid/view/WindowMetrics;
  at androidx.window.layout.util.DensityCompatHelperApi34Impl.density(DensityCompatHelper.kt:64)
  at androidx.window.layout.WindowMetricsCalculatorCompat.computeCurrentWindowMetrics(...)
  at androidx.compose.material3.windowsizeclass.calculateWindowSizeClass(AndroidWindowSizeClass_android.kt:44)
  at com.hedvig.android.app.MainActivity.onCreate$lambda$4$0(MainActivity.java:253)

MainActivity derived the window size class with material3's calculateWindowSizeClass(activity). That routes through androidx.window's WindowMetricsCalculator, which calls WindowMetrics.getDensity(). That method is not present on some Android 14 platform builds, so the call links to nothing at runtime and throws NoSuchMethodError during initial composition. The library gates the call at API 34 but relies on a method only present in a later Android 14 revision.

androidx.window:window:1.5.0 is pulled in transitively via material3-window-size-class:1.5.0-alpha17 (from the jetbrains-material3 = 1.11.0-alpha07 stack).

Fix

Compute the WindowSizeClass from Configuration (screenWidthDp / screenHeightDp) via WindowSizeClass.calculateFromSize, which reads Resources.getConfiguration() and never touches androidx.window.

A guard comment at the call site documents why we avoid both calculateWindowSizeClass(activity) and LocalWindowInfo.containerSize (the latter routes through the same androidx.window density path in Compose UI 1.11.4).

Verification

Built the R8-minified staging APK and inspected the shrunk dex + mapping.txt:

  • material3's AndroidWindowSizeClass_androidKt (the crashing path) is stripped from the shipped app: the reported crash cannot recur.
  • The only residual WindowMetrics.getDensity() reference is Compose UI's lazy LocalWindowInfo.containerSize path. It is computed only on read, and no app code reads containerSize (custom navigation, no material3 adaptive APIs), so it never executes.

Scope note

This removes the live trigger of the buggy androidx.window code without pinning the dependency. If the app later adopts Compose adaptive-layout APIs that read containerSize, the underlying androidx.window bug would resurface and pinning androidx.window would then be warranted.

Firebase Crashlytics issue: 6a11e58b371ac16c7a6781fc95171530
Affected version: 14.4.2 (1786360451)

MainActivity derived the window size class with material3's
calculateWindowSizeClass(activity), which routes through androidx.window's
WindowMetricsCalculator and calls WindowMetrics.getDensity(). That method is
absent on some Android 14 builds, so the app crashed on launch with
NoSuchMethodError before the first frame.

Compute the WindowSizeClass from Configuration (screenWidthDp / screenHeightDp)
via WindowSizeClass.calculateFromSize, which never touches androidx.window.

Verified on the R8-minified staging build: the material3
AndroidWindowSizeClass path is stripped from the shipped dex. The only residual
getDensity reference is Compose UI's lazy LocalWindowInfo.containerSize path,
which no app code reads.

Firebase Crashlytics issue: 6a11e58b371ac16c7a6781fc95171530
Affected version: 14.4.2 (1786360451)
@StylianosGakis
StylianosGakis requested a review from a team as a code owner August 13, 2026 12:15
@panasetskaya
panasetskaya merged commit 8d220a4 into develop Aug 13, 2026
4 checks passed
@panasetskaya
panasetskaya deleted the fix/windowmetrics-getdensity-crash branch August 13, 2026 16:21
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

Successfully merging this pull request may close these issues.

2 participants