Skip to content

Crash when Android-specific classes are used as top-level val #27

@mxalbert1996

Description

@mxalbert1996

We are trying to use ComposablePreviewScanner with Paparazzi.
We have some components that use Android-specific classes in it, and the scanner crashes when scanning these classes.

Example 1

private val SomePath = android.graphics.Path().apply { ... }

The scanner crashes with

java.lang.UnsatisfiedLinkError: 'long android.graphics.Path.nInit()'
	at android.graphics.Path.nInit(Native Method)
	at android.graphics.Path.<init>(Path.java:51)
	at mypackage.SomeComponentKt.<clinit>

Example 2

private val Typeface = android.graphics.Typeface.create("sans-serif-medium", Typeface.NORMAL)

The scanner crashes with

java.lang.NullPointerException: Cannot invoke "java.util.Map.get(Object)" because "android.graphics.Typeface.sSystemFontMap" is null
	at android.graphics.Typeface.getSystemDefaultTypeface(Typeface.java:1216)
	at android.graphics.Typeface.create_Original(Typeface.java:899)
	at android.graphics.Typeface_Delegate.create(Typeface_Delegate.java:109)
	at android.graphics.Typeface.create(Typeface.java:899)
	at mypackage.SomeComponentKt.<clinit>

After Paparazzi is initialized (when using Paparazzi as the test rule), the crash doesn't happen, but the preview list need to be generated before the test rule kicks in.

We can use something like lazy as a workaround, but I'm wondering if there's some other way that doesn't require code change.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions