4

I am using databinding in my app and it is working perfectly. But when I enable minifyEnabled true and shrinkResources true than it show me some errors.

Execution failed for task':app:transformClassesAndResourcesWithR8ForDebug'.
Caused by: com.android.tools.r8.utils.AbortException: Error: D:\TUK_Sep_Conclave\TUK_Septmber\app\build\intermediates\proguard-rules\debug\aapt_rules.txt, offset: 48276, line: 457, column: 33, Expected field or method name at D:\TUK_Sep_Conclave\TUK_Septmber\app\build\intermediates\proguard-rules\debug\aapt_rules.txt:457:33

-keepclassmembers class * { *** @{(view)->listener.onClick(view)}(android.view.View); }
                            ^
at com.android.tools.r8.utils.Reporter.failIfPendingErrors(Reporter.java:101)

Suppressed: com.android.tools.r8.shaking.ProguardRuleParserException: Expected field or method name at D:\TUK_Sep_Conclave\TUK_Septmber\app\build\intermediates\proguard-rules\debug\aapt_rules.txt:457:33
-keepclassmembers class * { *** @{(view)->listener.onClick(view)}(android.view.View); }

and my layout.xml is

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.ui.user.activity.ContactUsActivity"
    tools:ignore="HardcodedText">

    <data>

        <variable
            name="listener"
            type="com.example.ui.user.activity.ContactUsActivity" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/action_call_one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/selectableItemBackground"
            android:clickable="true"
            android:focusable="true"
            android:gravity="center"
            android:onClick="@{(view)->listener.onClick(view)}"
            android:padding="3dip"
            android:text="+91-1234567890"
            android:textColor="@android:color/white"
            android:textSize="12sp" />

    </LinearLayout>

</layout>

Now my question is how to write progaurd rules for android databinding and click listener. Any help will be appreciated, Thanks.

Sumit Shukla
  • 4,116
  • 5
  • 38
  • 57
Jitendra Singh
  • 200
  • 4
  • 17

1 Answers1

3

It's a very old question but answering for others who will face this issue.

Kindly check these steps to resolve the issue.

Step # 1

//Don't forget to add this rule in proguard-rules.pro

-keepclassmembers class * extends android.app.Activity {
     public void *(android.view.View);
 }

Step # 2

Kindly double check layout files which call

android:onClick="@{(view)->listener.onClick(view)}"

Check on top its must include variable

<variable
    name="listener"
    type="com.example.ui.user.activity.ContactUsActivity" />

If a variable is missing in any layout file above error will come.

Step # 3

If your issue is not resolved yet then you have to change

android:onClick="@{(view)->listener.onClick(view)}"

To

android:onClick="@{listener::onClick}"
  • Even after following your suggestions, getting the same issue . Expected field or method name at aapt_rules.txt -keepclassmembers class * { *** @{listener::onClick}(android.view.View); } – Deepak Rattan Jan 30 '23 at 07:58