cashapp / paparazzi

Render your Android screens without a physical device or emulator
https://cashapp.github.io/paparazzi/
Apache License 2.0
2.25k stars 213 forks source link

`Resources$NotFoundException` on tests with `AlertDialog` on latest compileSdk version #1025

Closed jmartinesp closed 5 months ago

jmartinesp commented 1 year ago

Description Our tests with AlertDialog compose components are failing with stacktraces like this after settings both compileSdk and targetSdk to 34.

io.element.android.tests.uitests.ScreenshotTest > preview_tests[io.element.android.services.apperror.impl_null_DefaultGroup_AppErrorViewLightPreview_0_null,NEXUS_5,1.0,en] FAILED
    android.content.res.Resources$NotFoundException: Could not find dimen resource matching value 0x105024F (resolved name: resize_shadow_size) in current configuration.
        at app//android.content.res.Resources_Delegate.throwException(Resources_Delegate.java:1161)
        at app//android.content.res.Resources_Delegate.throwException(Resources_Delegate.java:1137)
        at app//android.content.res.Resources_Delegate.throwException(Resources_Delegate.java:1141)
        at app//android.content.res.Resources_Delegate.getDimensionPixelSize(Resources_Delegate.java:670)
        at app//android.content.res.Resources.getDimensionPixelSize(Resources.java:828)
        at app//com.android.internal.policy.DecorView.<init>(DecorView.java:320)
        at app//com.android.internal.policy.PhoneWindow.generateDecor(PhoneWindow.java:2385)
        at app//com.android.internal.policy.PhoneWindow.installDecor(PhoneWindow.java:2727)
        at app//com.android.internal.policy.PhoneWindow.getDecorView(PhoneWindow.java:2144)
        at app//androidx.compose.ui.window.DialogWrapper.<init>(AndroidDialog.android.kt:352)
        at app//androidx.compose.ui.window.AndroidDialog_androidKt.Dialog(AndroidDialog.android.kt:163)
        at app//androidx.compose.material3.AndroidAlertDialog_androidKt.AlertDialog(AndroidAlertDialog.android.kt:151)
        at app//io.element.android.libraries.designsystem.components.dialogs.ErrorDialogKt.ErrorDialog-10sF2-8(ErrorDialog.kt:48)

Steps to Reproduce Record screenshots of composables with AlertDialogs when using compileSdk and targetSdk set to 34.

Expected behavior The screenshots should be recorded.

Additional information:

gabrielittner commented 1 year ago

We have this as a workaround:

Paparazzi(
  environment = detectEnvironment().run {
    copy(compileSdkVersion = 33, platformDir = platformDir.replace("34", "33"))
  },
)

That allows us to use compile and target sdk 34 while keeping Paparazzi on 33 until it supports the newer platform version. I assume it needs a layoutlib update

ZacSweers commented 1 year ago

Note the above solution can require more work if your CI uses ephemeral nodes. You will need to ensure that SDK 33 is also actually installed

alashow commented 1 year ago

Note the above solution can require more work if your CI uses ephemeral nodes. You will need to ensure that SDK 33 is also actually installed

Can confirm. I couldn't use sdkmanager (sdkmanager --install "platforms;android-33") in our CI, so to fix this I added empty module having compileSdk = 33 (while everything else is compileSdk = 34), which made AGP to install both 33 & 34 in build time.

Details ```kotlin // dummy empty module with only build.gradle.kts (ex project/libs/compileSdk33-require/build.gradle.kts) plugins { alias(libs.plugins.android.library) } android { namespace = "common.compilesdk33_require" compileSdk = 33 defaultConfig { minSdk = 24 } } // settings.gradle include (":libs:compileSdk33-require") ``` Then just add `./gradlew :libs:compileSdk33-require:build` step to CI at beginning or before running tests (or just add `implementation(project(':libs:compileSdk33-require'))`)
dniHze commented 1 year ago

It looks like AS Hedgehog handles previews properly, whilst Giraffe Patch 1 is failing to render previews for such composables. I guess this would stick for a while until the next patch for Giraffe with SDK 34 support or Hedgehog is released.

ZacSweers commented 1 year ago

Wait giraffe breaks compose previews on sdk 34?

dniHze commented 1 year ago

Seems so, tested the same composable on Giraffe Patch 1 and Hedgehog Canary 16, and Giraffe renders 1x1px previews with compile SDK 34.

Upd: it renders regular composables, just fails on dialog and popups

peterdk commented 10 months ago

I had the same issue, and got it working with the workaround of setting environment to 33. However, now my tests randomly fail with Resources$NotFOundException while it succeedes when I run them for a 2nd time... So I am not sure why.

Tried with 1.3.2-SNAPSHOT.

Also, Hedgehog indeed breaks previews in AndroidStudio of my AlertDialogs.

pstambrecht commented 10 months ago

Snapshot 1.3.2-SNAPSHOT doesn't work for us. Is this fix targeting into 1.3.2?

dniHze commented 10 months ago

Snapshot 1.3.2-SNAPSHOT doesn't work for us. Is this fix targeting into 1.3.2?

👋 @pstambrecht

The bug is strictly dependent on the layoutlib. Paparazzi only updates to the most recent stable layoutlib. Until a stable build of layoutlib from AS Hedgehog that properly supports SDK 34 is released, I doubt something can be done without working around layoutlib internals. It looks like 🦔 has reached RC2 so the first release might be soon-ish 🤞.

jrodbx commented 7 months ago

Looks like this won't be possible until Iguana, tracked in https://github.com/cashapp/paparazzi/issues/1220.

Compare Hedgehog to Iguana noting this commit, in particular:

Screenshot 2024-01-22 at 8 26 54 AM
peterdk commented 6 months ago

Somehow it works now for me again. I use Buildtools version 34.0.0, and compileSdk 34. With latest Android SDK updates from check for updates dialog in Android studio. (Paparazzi 1.3.3)

However, I do have targetted Android 33. But that failed for me as well for Dialogs before. So FYI,

jrodbx commented 5 months ago

Closed by https://github.com/cashapp/paparazzi/pull/1314