Search code examples
androiddata-binding

Android Databinding "Execution failed for task ':app:dataBindingGenBaseClassesDebug"


I am new to android databinding and trying to learn from android documentation. As soon as I added data tag by specifying the model, I am getting the following error:

Any clue?

org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:dataBindingGenBaseClassesDebug'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:103)

Caused by: java.lang.IllegalArgumentException: couldn't make a guess for com.example.databindinglibrarysample.datamodel

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>
        <variable name="datamodel" type="com.example.databindinglibrarysample.datamodel" />
    </data>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{datamodel.firstname}"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>

</layout>

Model class:

package com.example.databindinglibrarysample;

public class datamodel extends BaseObservable {
    private String firstname;


    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }   
}

Solution

  • You probably haven't set the datamodel when you use DataBindingUtil when you bind the activity. For example

    MainActivityBinding binding= DataBindingUtil
                    .setContentView(this, R.layout.main_activity);
    binding.setDatamodel(new datamodel());