Skip to content Skip to sidebar Skip to footer

Data Binding Broke After Upgrade To Gradle 2.3

After upgrading to Gradle 2.3. My project cannot compile. I'm having the log in the console incompatible types: ObservableInt cannot be converted to int Look at the generated file

Solution 1:

This looks like a bug. Please file it. There are many tests and we don't expect this kind of regression. It is important that we get your specific example so we can be sure it is caught.

You can ignore the warnings for now.

The first is caused because of this:

@BindingAdapter("app:visibility")

You should use this instead:

@BindingAdapter("visibility")

The second is because we support ObservableInt as a parameter. You typically don't want to accept ObservableInt, but rather int instead. I'd love to see use cases where ObservableInt is necessary. We may just remove that warning and support it always or we may pull the plug on supporting ObservableInt as a parameter if there are no valid uses.

----- edit -----

I tested this with a little application and I didn't have any issue without any BindingAdapter. Here is the layout:

<layout>
    <data>
        <variable name="model" type="com.example.gmount.testobservableint.MyModel"/>
    </data>
    <android.support.constraint.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:onClick="@{model::clicked}"
            tools:context="com.example.gmount.testobservableint.MainActivity">

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                android:visibility="@{model.visibility}"
        />

    </android.support.constraint.ConstraintLayout>
</layout>

Here is my model:

public class MyModel {
    public final ObservableInt visibility = new ObservableInt(View.VISIBLE);

    public void clicked(View view) {
        int oldVisibility = visibility.get();
        int newVisibility = oldVisibility == View.VISIBLE ? View.GONE : View.VISIBLE;
        visibility.set(newVisibility);
    }
}

Even when I used a BindingAdapter taking an ObservableInt, it worked. Here's my BindingAdapter:

@BindingAdapter("visiblity")
public static void setVisibility(View view, ObservableInt visibility) {
    view.setVisibility(visibility.get());
}

And I changed the View's binding to be:

    <TextView ...
            app:visibility="@{model.visibility}"
    />

Is there something different about your viewModel?


Solution 2:

You just have to add this to the bottom of your build.gradle dependencies:

apt 'com.android.databinding:compiler:2.3.0'

Solution 3:

See this: https://stackoverflow.com/a/42711830/376829 regarding GoMobile update to version "+eb90329 Mar 7 2017" and GoBind plugin revert to version "0.2.6" (although the current version is "0.2.8")


Solution 4:

 android:visibility="@{viewModel.contentVisibility}" 

remember this

dataBinding {
    enabled = true
}

re-download the library from the Support repository in the Android SDK manager.


Post a Comment for "Data Binding Broke After Upgrade To Gradle 2.3"