Ken Zira
Ken Zira

Reputation: 1176

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

android.databinding.ObservableInt viewModelLoadingVisibility;
this.vLoading.getRoot().setVisibility(viewModelLoadingVisibility);

In xml file

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvProducts"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:visibility="@{viewModel.contentVisibility}"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>

I tried in method in my binding class

@BindingAdapter("app:visibility")
public static void setViewVisible(View view, ObservableInt visible) {
        int visibility = visible.get();
        view.setVisibility(visibility);
}

and got log

warning: Application namespace for attribute app:visibility will be ignored.

public static void setViewVisible(View view, ObservableInt visible) {

warning: Use of ObservableField and primitive cousins directly as method parameters is deprecated and support will be removed soon. Use the contents as parameters instead in method public static void setViewVisible(android.view.View,android.databinding.ObservableInt)

public static void setViewVisible(View view, ObservableInt visible) {

Anyone encounters this?

Upvotes: 3

Views: 2172

Answers (4)

David Manpearl
David Manpearl

Reputation: 12656

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

Upvotes: 1

George Mount
George Mount

Reputation: 20926

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?

Upvotes: 2

Christoph Mayr
Christoph Mayr

Reputation: 347

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

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

Upvotes: 1

Remario
Remario

Reputation: 3863

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

remember this

dataBinding {
    enabled = true
}

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

Upvotes: 0

Related Questions