Can't resolve Android databinding class

Viewed 86239
29 Answers

Make sure your activity_main.xml file is enclosed with layout tags like so:

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

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

I've cleaned the project , Rebuild was done...but of no use. Then invalidated caches and restarted the project that too didn't helped me.

Then I changed my xml file name - that worked fantastically fine.

So, I would like to share you one thing, please change your xml file name.

For eg: If your xml file is: activity_main.xml and you can't get ActivityMainBinding in its Java class..........then change xml name to main_activity.xml and then use MainActivityBinding in its java class as 'private MainActivityBinding binding;'

This will do most probably.

I had the same problem after changing the package name of my source, after I tried a lot of things (including mentioned here), I solved the problem by importing the databinding class manually:

import com.domain.my.databinding.MyActivityBinding;

In my case DELETING the the app build folder and then rebuilding the project solved my problem.

Even following steps did not work.

          1) Rebuild the project after adding the dataBinding  in gradle.

          2) Rebuild after adding layout tag in xml.

          3) cleaning the project

          4) rebuilding the project

          5) Restarted the Android Studio

After enabling the dataBinding in app gradle file please rebuild. Then we can find Binding class generated.

          If the layout name is fragment_home, The Binding class name will be FragmentHomeBinding.

Make sure to add below lines in build.gradle file

dataBinding { enabled true }

Binding class will be generated based on your layout file name. By default, the name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it

for example, If your xml name is activity_main.xml then DataBinding class name will be ActivityMainBinding

then import the package:

import com.companyname.applicationname.databinding.ActivityMainBinding;

then you can use it

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
User user = new User("Test", "User");
binding.setUser(user);

Restart Android studio or try to rename the XML file to another name and check if binding works once it works.

  1. confirm add below code in android {} in build.gradle (:app)
    buildFeatures {
       viewBinding true
    }
  1. Binding's name is the corresponding .xml file name + "Binding", to check the .xml file, hold command and click the Activity name, then you jump to that .xml file OR you may get a dropdown list. No matter which, you can get the Binding. enter image description here

3. if still doesn't work then restart Android Studio

In my case: Solve problem without renaming XML file.

I checked all case and I did everything like Invalidate Caches/Restart Android Studio, clean Project, Rebuild Project but But the error is not resolved.

Solution - Just I change some code and revert that code again in activity XML file or change code in XML file.

Note - This is very late answer but may be help others who don't want change XML file name. :)

I was facing the same issue,

If your XML name is activity_main.xml than DataBinding class name will be ActivityMainBinding

If it is correct check if you add below code in your xml file,

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable
        name="xyz"
        type="com.example.yourapp.classname" />
   />

</data>

if either method is not the issue, try

  • clean project
  • Rebuild project

In my own experience, whenever you are including a layout, make sure the root view group has a layout id as shown below. Note the child_layout id in the Child Layout file.

ParentLayout

        <include
            android:id="@+id/child_layout"
            layout="@layout/child_layout"/>

ChildLayout

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:id="@+id/child_layout"
    android:layout_height="match_parent"
    android:background="#FFFFFe">

This line inside module/build.gradle solved the issue for me:

buildFeatures {
    viewBinding true
}

I've tried the following solutions which didn't work for me: 1) Invalidate cache and restart. 2) Restart Android Studio. 3) Rebuild project.

What DID fix the problem is: 1. Removing the "dataBinding" tag in the module gradle.build 2. Sync project 3 Return the "databinding" tag 4. Sync project again.

Try restarting Android Studio, or manually searching for the ActivityMainBinding class and adding your import.

Put your data tag in your last included xml. Here is an example:

MainActivity.java
public class MainActivity extends AppCompatActivity {

    public Item item;
    ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        item = new Item();
        item.setChecked(true);
        item.setName("a");
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.included.secondIncluded.setModel(item);


Item.java
public class Item extends BaseObservable {
    private String name;
    private Boolean checked;
    @Bindable
    public String getName() {
        return this.name;
    }
    @Bindable
    public Boolean getChecked() {
        return this.checked;
    }
    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);
    }
    public void setChecked(Boolean checked) {
        this.checked = checked;
        notifyPropertyChanged(BR.checked);
    }
}


activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="abc"
            android:textSize="20sp" />
        <include
            android:id="@+id/included"
            layout="@layout/included_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>


included_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/tv_title2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="123"
            android:textSize="20sp" />
        <include
            android:id="@+id/second_included"
            layout="@layout/second_included_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</layout>

second_included_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="model"
            type="com.example.simone.twowaydatabinding.Item" />
    </data>

    <LinearLayout
        android:id="@+id/linear_layout_included"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_title1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="xyz"
            android:textSize="20sp" />
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={model.name}"
            android:textSize="20sp" />
        <Switch
            android:id="@+id/switch_test"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="@={model.checked}" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="change"
            android:onClick="button_onClick"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/world"/>
    </LinearLayout>
</layout>       

I recently installed android studio 3.5.3 and this issue rised so what i did is.

1) Clean Project
2) Update Gradle (as notification was coming) 
3) Rebuild project

hope this helps.

try to review your xml, if has a value which was setted wrong. This is probably something about wrong databinding settings

Please put this code in build.gradle(app level), if not declared.

android {
    dataBinding.enabled true
    viewBinding {
        enabled = true
    }
}

// Android Studio 4.0

android {
    dataBinding.enabled true
    buildFeatures {
        viewBinding = true
    }
}

I also faced same issue. But after updating android to androidx solved like

try to change android.databinding.... to androidx.databinding...

Enclosed with layout tags

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

   <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

Hi the accepted answer is outdated, the new answer is include this in the build.gradle(:app)

inside the android block

buildFeatures { viewBinding true }

works 100%

FYI, I am using Android Studio Bumblebee | 2021.1.1

I tried all basic steps such as

  • Clean & build project
  • Invalidate caches and restart

All above not working. Then I found it by myself after going through the Gradle file.

Add below code into module/build.gradle file inside android tag.

kotlinOptions {
    jvmTarget = '1.8'
}
Related