Android Databinding – Variable is already defined

Recently I added a listener variable in the layout XML of one of my files. It looked like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="paymentActionListener"
            type="com.tideaccount.android.tabs.accounts.listeners.QueuedPaymentActionListener" />
    </data>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:background="@drawable/white_rounded"
        android:orientation="vertical"
        android:padding="15dp">
...

And I got the following exception from the DataBinding Variable paymentActionListe already defined. Then I found THIS issue. It was mentioned in it that the binding variables have a limit of 20 characters. I opened another generated DataBinding file and saw that there were 2 variables connected with each other: boolean paymentListObj and Listener paymentList. These same variables in the generated file of my XML had the exact same name so the truncation of them was not right.

The solution was to rename the variable paymentActionListener to listener and everything was fine. Shorter variable names prevent the DataBinding library from generating wrong variable names. I hope this bug will be fixed soon.

You may also like...