androidgradleandroid-databindinggenerated-code

Data binding stops working when using MutableLiveData variable inside layout [Gradle to 3.5.1]


I'm using DataBinding in my project. Use Java, not Kotlin, Android Studio version is 3.5.1(latest)

After upgrading project gradle version from 3.5.0 to 3.5.1 got error inside *BindingImpl class. I've figure out that the problem is with MutableLiveData inside one of my layout file

Tried to clean up project and restart Android Studio. But the only thing that works is to set back gradle version to 3.5.0

Here is my layout

<?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">

    <data>

        <import type="androidx.lifecycle.MutableLiveData" />

        <variable
            name="hintText"
            type="String" />

        <variable
            name="helperText"
            type="String" />

        <variable
            name="collection"
            type="Object" />

        <variable
            name="selection"
            type="MutableLiveData" /> // this one is breaks down in 3.5.1

        <variable
            name="enabled"
            type="Boolean" />

    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:setupSpinnerHint="@{true}">

        <kz.jgroup.android.umag.base.view.customs.ResizableItemsSpinner
            android:id="@+id/spinner"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:adapterWith="@{collection}"
            android:background="@drawable/spinner_outlined_box"
            android:enabled="@{enabled == null ? true : enabled}"
            android:paddingStart="12dp"
            android:paddingTop="26dp"
            android:paddingEnd="32dp"
            android:paddingBottom="17dp"
            android:selectedItem="@={selection}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:textSize="16sp"
            tools:listitem="@layout/item_spinner_inline" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:background="@color/white"
            android:paddingLeft="4dp"
            android:paddingRight="4dp"
            android:text="@{hintText}"
            android:textColor="@color/focusable_color"
            android:textSize="14sp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Подсказка" />


        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/helper_text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginTop="5dp"
            android:emptyToGone="@{helperText}"
            android:textColor="@color/focusable_color"
            android:textSize="12sp"
            app:layout_constraintStart_toStartOf="@+id/hint"
            app:layout_constraintTop_toBottomOf="@+id/spinner"
            tools:text="Вспомогательное сообщение" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</layout>

And here is where I get error and what DataBinding generates

With version 3.5.1

        boolean enabledJavaLangObjectNull = false;
        ? selectionGetValue = null; //Illegal start of expression
        java.lang.Object collection = mCollection;
        boolean enabledJavaLangObjectNullBooleanTrueEnabled = false;
        java.lang.String helperText = mHelperText;
        androidx.lifecycle.MutableLiveData<?> selection = mSelection;
        java.lang.String hintText = mHintText;
        java.lang.Boolean enabled = mEnabled;

In 3.5.0 version generated code looks:

        boolean enabledJavaLangObjectNull = false;
        java.lang.Object selectionGetValue = null;
        java.lang.Object collection = mCollection;
        boolean enabledJavaLangObjectNullBooleanTrueEnabled = false;
        java.lang.String helperText = mHelperText;
        androidx.lifecycle.MutableLiveData selection = mSelection;
        java.lang.String hintText = mHintText;
        java.lang.Boolean enabled = mEnabled;

I expected that with upgrading from stable 3.5.0 to stable 3.5.1 everything will be ok, but as you see it's not((


Solution

  • Fixed with 3.5.2:

    https://androidstudio.googleblog.com/2019/11/android-studio-352-available.html

    and the issue itself:

    https://issuetracker.google.com/issues/141633235