airbnb / paris

Define and apply styles to Android views programmatically

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TextView paris.extensions.style works on Android 12 but crashed on Android 9 with customized style.

donlingliang opened this issue · comments

Hi Paris developers,

We had an XmlPullParserException crash in Android 9 (API level 28) when using customized style.

The original code snippet below are the code that crashed.

CustomTextView constructor(context: Context): AppCompatTextView() {
    @ModelProp
    @JvmOverloads
    fun setTextStyle(style: TextStyle = TextStyle.BODY) = style(R.style.custom)
}

style resources:

<style name="CustomTextView.custom">
        <item name="android:textSize">12sp</item>
        <item name="android:fontFamily">@font/medium</item>
        <item name="fontFamily">@font/medium</item>
        <item name="android:background">@color/selector_clickable_text</item>
        <item name="android:clickable">true</item>
        <item name="android:focusable">true</item>
</style>

color:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/pressed"/>
    <item android:state_selected="true" android:color="@color/pressed"/>
    <item android:state_activated="true" android:color="@color/pressed"/>
    <item android:color="@color/transparent"/>
</selector>

Based on the stacktrace:

LOCATION:
TypedArrayTypedArrayWrapper.java line 45 in com.airbnb.paris.typed_array_wrappers.TypedArrayTypedArrayWrapper.getDrawable()
EXCEPTION:
org.xmlpull.v1.XmlPullParserException
MESSAGE:
Binary XML file line #2: <item> tag requires a 'drawable' attribute or child tag defining a drawable

I think the correct way is change the color resource from android:color with android:drawable like below.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/pressed"/>
    <item android:state_selected="true" android:drawable="@color/pressed"/>
    <item android:drawable="@color/transparent"/>
</selector>

But I don't know why the original code works on Android 12 and no crash, when crashing on Android 9. Try to look into the source code TextViewStyleApplier and etc, don't find anything related to VERSION.SDK_INT and wrong usage on android:color that only cause the Android 9 crash.

Appreciated that anyone has some insights or ideas.