DataBinding not working in module
Asked Answered
T

2

10

I'm using data binding within my app. I have an application module and the data binding is working successfully.

However, I also have a 'home-module' I'm trying to use the same techniques, but the data binding is erroring in the xml with the following:

Error:(63, 34) No resource type specified (at 'onClick' with value '@{viewModel::onButtonClicked}').

I've found this bug report which suggests this was an issue but it's been fixed.

I can't see any issues with the code, I think the problem is because it's in the library module.

Is there anything I can do to get this working?

home-module

activity_home.xml

<?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>
        <variable name="viewModel" type="com.flowmellow.projectx.home.ui.viewmodel.HomeViewModel"/>
    </data>

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/home_screen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context="com.flowmellow.projectx.home.ui.viewmodel.HomeActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.AppBarLayout>

        <android.support.constraint.ConstraintLayout
            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"
            android:id="@+id/content_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            tools:context="com.flowmellow.projectx.home.ui.viewmodel.HomeActivity">

            <TextView
                android:text="@string/activity_home_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/activity_home_title"
                android:textAppearance="@style/TextAppearance.AppCompat.Large"
                android:layout_marginStart="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginLeft="16dp"
                android:layout_marginEnd="16dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginRight="16dp"
                app:layout_constraintBottom_toTopOf="@+id/activity_home_button"
                android:layout_marginBottom="64dp" />

            <Button
                android:text="@string/activity_home_button"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/activity_home_button"
                style="@style/Widget.AppCompat.Button.Colored"
                android:onClick="@{viewModel::onButtonClicked}"/>

        </android.support.constraint.ConstraintLayout>

    </android.support.design.widget.CoordinatorLayout>
</layout>

HomeActivity

public class HomeActivity extends AppCompatActivity {

    private HomeViewModel mHomeViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
        final CustomSensorManager customSensorManager = new CustomSensorManager(sensorManager);
        mHomeViewModel = new HomeViewModel(customSensorManager);

        setBindingContentView(R.layout.activity_home);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

    private void setBindingContentView(@LayoutRes int layoutResID) {
        ActivityHomeBinding binding = DataBindingUtil.setContentView(this, layoutResID);
        binding.setViewModel(mHomeViewModel);
    }
}

HomeViewModel

public class HomeViewModel {

    private CustomSensorManager mSensorManager;

    public HomeViewModel(@NonNull CustomSensorManager sensorManager) {
        mSensorManager = sensorManager;
    }

    public void onButtonClicked(View view) {
        mSensorManager.scan(true);
    }
}

build.gradle

apply plugin: 'com.android.library'
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')

    compile 'com.android.support:appcompat-v7:25.0.0'
    compile 'com.android.support:design:25.0.0'
    compile 'com.android.support.constraint:constraint-layout:+'
}

Update:

Could be a bug. link

My problem:

enter image description here

The engine module has no UI, so the dependency was missed, makes a lot of sense now.

Thinnish answered 20/1, 2017 at 13:43 Comment(5)
Do you have data binding enabled in both the library and the application modules?Romola
Yes, enabled in the application moduleThinnish
I just tested this and it works. I suspect that you don't have data binding enabled in the library module also.Romola
100% I did have DataBinding enabled in both application and the library module. Thank for testing. But If it's working for you I must have done something wrong... I'll just have to try again. Thanks for spending the time.Thinnish
FYI The build.gradle from the library module was copied into the question.Thinnish
S
22

Say you have a multilevel module hierarchy like this in your app:

Multilevel module hierarchy in Android

In order for databinding to work module A, you have to make sure you enable databinding in the whole hierarchy from the root module into module A.

That means you have to add:

dataBinding {
    enabled = true
}

To the modules:

  • app module
  • module A
  • module B

It is important to remember that you do have to also enable databinding in module B, so all paths are covered.

Sherrer answered 25/1, 2017 at 11:40 Comment(3)
Yes! This was my problem, I had another module dependant on home-module. Working fine now thank you.Thinnish
Is this still required? I face a problem where I have an android module whose 3rd child dependency uses databinding. When I add databinding to app gradle as well, I get R8: Program type already present exception which does not happen if I remove it from the app module.Jablon
Is this requirement mentioned in android official doc?Whirlpool
E
0

I faced this issue same and got some information to solved it.

https://developer.android.com/topic/libraries/data-binding/start#build_environment

enter image description here

Eldrid answered 21/8, 2023 at 7:47 Comment(1)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewKikuyu

© 2022 - 2024 — McMap. All rights reserved.