Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot invoke "com.android.layoutlib.bridge.android.BridgeContext.isAppCompatTheme()" because "this.mContext" is null #630

Closed
elihart opened this issue Nov 11, 2022 · 13 comments
Labels
bug Something isn't working

Comments

@elihart
Copy link

elihart commented Nov 11, 2022

Description
We are seeing on CI that paparazzi occasionally fails with the error:

NullPointerException: Cannot invoke "com.android.layoutlib.bridge.android.BridgeContext.isAppCompatTheme()" because "this.mContext" is null

Steps to Reproduce
We are using Paparazzi with compose. The test class is in android unit test sources with junit 4.

class OurTestClass {
    @get:Rule
    val paparazzi = Paparazzi(
        deviceConfig = PIXEL_5.copy(
            softButtons = false,
            locale = "en-rUS"
        ),
        theme = "BaseUiPrimitivesTheme.TabNav",
        renderingMode = SessionParams.RenderingMode.NORMAL,
    )

 @Test
 fun ourTestFunction() {
   paparazzi.snapshot {
     // Compose UI
   }
 }
}

Sorry, I don't have any better repro steps and can't tell why it only sometimes happens.

Expected behavior
Test should run consistently without crashing

Additional information:

  • Paparazzi Version: 1.1.0
  • OS: Linux
  • Compile SDK: 33
  • Gradle Version: 7.5.1
  • Android Gradle Plugin Version: 7.3
@elihart elihart added the bug Something isn't working label Nov 11, 2022
@jrodbx
Copy link
Collaborator

jrodbx commented Nov 29, 2022

By chance, does your test contain other Junit Rules outside of a RuleChain?

@elihart
Copy link
Author

elihart commented Nov 29, 2022

No, our test class only has the single Paparazzi rule

@vinaygaba
Copy link

vinaygaba commented Feb 22, 2023

@jrodbx I'm seeing the same error as part of the work I'm doing around Showkase + Paparazzi integration. I'm now integrating that artifact to airbnb's app and seeing errors of this nature in about 33% of the tests. The test itself uses TestParameterInjector for creating a parameterized Paparazzi test

Error

java.lang.NullPointerException: Cannot invoke "com.android.layoutlib.bridge.android.BridgeContext.setBridgeInflater(android.view.BridgeInflater)" because "context" is null	

at com.android.layoutlib.bridge.impl.RenderSessionImpl.init(RenderSessionImpl.java:205) |  
-- | --
  |   | at app.cash.paparazzi.Paparazzi.prepare(Paparazzi.kt:168) |  
  |   | at app.cash.paparazzi.Paparazzi$apply$statement$1.evaluate(Paparazzi.kt:116) |  
  |   | at com.google.testing.junit.testparameterinjector.PluggableTestRunner$ContextMethodRule$1.evaluate(PluggableTestRunner.java:420) |  
  |   | at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) |  
  |   | at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) |  
  |   | at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) |  
  |   | at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) |  
  |   | at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) |  
  |   | at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) |  
  |   | at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) |  
  |   | at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) |  
  |   | at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) |  
  |   | at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) |  
  |   | at org.junit.runners.ParentRunner.run(ParentRunner.java:413)

Sample Test

https://github.com/airbnb/Showkase/blob/d26dca339e4ed361acbb9f1fa2cbea2a007a7d56/showkase-processor-testing/src/test/resources/ShowkaseProcessorTest/class_with_@ScreenshotTest_generates_paparazzi_screenshot_test_for_all_UI_elements/output/MyScreenshotTest_PaparazziShowkaseTest.kt

@vinaygaba
Copy link

I spent way too long on this, but was eventually able to figure out the line that's causing this issue. Removing this from the setup gets rid of this error. Sounds like a bug somewhere in Paparazzi

Paparazzi(
    maxPercentDifference = 0.0,
    showSystemUi = false,
-  renderingMode = SessionParams.RenderingMode.SHRINK
)

@vinaygaba
Copy link

One more update: I was able to reduce the odds of the error happening with my previous comment, however it still happens often enough that I cannot possibly use it without causing disruption. Any chance I could get some advice?

@vinaygaba
Copy link

vinaygaba commented Apr 18, 2023

Just to close the loop on this issue, this happens when a component uses Lottie Compose in its implementation. Thanks to this tip on a separate issue.

To get around this, add this to your test class.

@Before
fun setup() {
    LottieTask.EXECUTOR = Executor(Runnable::run)
}

@jrodbx
Copy link
Collaborator

jrodbx commented Jun 13, 2023

Closing as fixed

@matejdro
Copy link
Contributor

I wouldn't really count this as fixed, as this is only a workaround.

Error message is still super confusing and unhelpful.

@vassilisimon
Copy link

Yes, it's not fixed.

@jrodbx
Copy link
Collaborator

jrodbx commented Jul 9, 2023

@matejdro can you file a new issue with a repro project?

It's not a workaround since the issue is ultimately not a Paparazzi issue but rather a Lottie issue, due to how it exposes its threading implementation.

@matejdro
Copy link
Contributor

It only happens on our CI system, I cannot reproduce it on my machine to make a reliable repro project.

Maybe "Lottie" section could be added to README to explain this? I am prepared to offer a Pull Request for that.

@jamesrapadmi
Copy link

jamesrapadmi commented Sep 21, 2023

I'm seeing this issue on all my paparazzi tests, even without SessionParams.RenderingMode.SHRINK and with the following setup block.

@Before
fun setup() {
    LottieTask.EXECUTOR = Executor(Runnable::run)
}

Is there anything else I can do to avoid this?

java.lang.NullPointerException: Cannot invoke "com.android.layoutlib.bridge.android.BridgeContext.setBridgeInflater(android.view.BridgeInflater)" because "context" is null
	at com.android.layoutlib.bridge.impl.RenderSessionImpl.init(RenderSessionImpl.java:198)
	at app.cash.paparazzi.Paparazzi.prepare(Paparazzi.kt:175)
	at app.cash.paparazzi.Paparazzi$apply$statement$1.evaluate(Paparazzi.kt:124)
...

For reference, I'm on:

  • Paparazzi Version: 1.3.1
  • OS: macOS
  • Compile SDK: 34
  • Gradle Version: 8.3
  • Android Gradle Plugin Version: 8.1.1

@mrea1
Copy link

mrea1 commented Jun 20, 2024

I'm seeing this on a project, but not using Lottie

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants