Can't resolve Android databinding class

AndroidData BindingAndroid Databinding

Android Problem Overview


While using data binding , I am not able to get class MainActivityBinding as per Data Binding Guide

My layout name is activity_main.xml. I am also see Android - DataBinding - How and when the Binding classes will be generated? but it can't help me.

Android Solutions


Solution 1 - Android

DataBinding class will be generated based on your xml file name. It is clearly mentioned in doc you are following.

> By default, a Binding class will be generated based on the name of the layout file, converting it to Pascal case and suffixing “Binding” to it. The above layout file was main_activity.xml so the generate class was > MainActivityBinding

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

If your xml name is main_activity.xml than DataBinding class name will be MainActivityBinding.

Dont forget to clean and build project once

you can follow this tutorial for more about DataBinding

Solution 2 - Android

TRY Renaming the xml file to another name and check if binding works once it works rename it back to the one that was used.

That helped for Android Studio 3.1

Solution 3 - Android

Thanks to all for your answer.I found solution with ContentMainBinding class name for data binding. Lets me explain.

NOTE: While using layout with <include ... here is <include layout="@layout/content_main" having Data Binding functionality, the class name related to include layout name. Here is the ContentMainBinding

My layout file are as below:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.databindingdemo.app.MainActivity">
    ...
    <include layout="@layout/content_main" />
    ...
    </android.support.design.widget.CoordinatorLayout>

And content_main.xml is layout where I added my Data Binding layout code.

So instead of using MainActivityBinding it can resolved with ContentMainBinding

The code which work for me is below:

//Code for data binding
    ContentMainBinding contentMainBinding = DataBindingUtil.setContentView(this, R.layout.content_main);
    user = new User("Pranay", "Patel", "[email protected]", "9999999999");
    contentMainBinding.setUser(user);

DONE.

Solution 4 - Android

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>

Solution 5 - Android

If DataBinding class name is correct and you used to clean and rebuild project but it still show error.
Then you try to restart AndroidStudio

Solution 6 - Android

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.

Solution 7 - Android

In such cases, if neither rebuild nor invalidate caches work, then there must be an error in one of your xml files where you may have used @{xyz.abc} and there must be a problem with either xyz or abc in the xml.

Solution 8 - Android

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;

Solution 9 - Android

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.

Solution 10 - Android

Make sure to add below lines in build.gradle file

dataBinding { enabled true }

Solution 11 - Android

Check that you activity/fragment's xml and class have consistent names; for example, if you have TimePickerFragment than xml file name should be time_picker_fragment.

It occurred to me that I had the layout named fragment_time_picker; after I changed it the binding was generated.

Solution 12 - Android

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

Solution 13 - Android

Cant comment so i'll just add this in answer. I believe activity_main.xml will create ActivityMainBinding rather than MainActivityBinding as you mentioned. in some cases where studio can't find the binding class then just invalidate the caches and rebuild the project.

Solution 14 - Android

By default, a Binding class is generated based on the name of the layout file, starting it with upper-case, removing underscores ( _ ) and capitalizing the following letter and then suffixing “Binding”.

This class will be placed in a databinding package under the module package.

For example, the layout file contact_item.xml will generate ContactItemBinding.

If the module package is com.example.my.app, then it will be placed in com.example.my.app.databinding.

Binding classes may be renamed or placed in different packages by adjusting the class attribute of the data element. For example:

<data class="ContactItem">
    ...
</data>

This generates the binding class as ContactItem in the databinding package in the module package. If the class should be generated in a different package within the module package, it may be prefixed with “.”:

<data class=".ContactItem">
    ...
</data>

In this case, ContactItem is generated in the module package directly. Any package may be used if the full package is provided:

<data class="com.example.ContactItem">
    ...
</data>

Solution 15 - Android

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

Solution 16 - Android

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. :)

Solution 17 - Android

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

Solution 18 - Android

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

Solution 19 - Android

  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

Solution 20 - Android

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
  3. Sync project again.

Solution 21 - Android

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>		

Solution 22 - Android

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.

Solution 23 - Android

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

Solution 24 - Android

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
    }
}

Solution 25 - Android

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

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

Solution 26 - Android

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>

Solution 27 - Android

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%

Solution 28 - Android

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'
}

Solution 29 - Android

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

buildFeatures {
    viewBinding true
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpRaNaYView Question on Stackoverflow
Solution 1 - AndroidRockyView Answer on Stackoverflow
Solution 2 - AndroidIsmail IqbalView Answer on Stackoverflow
Solution 3 - AndroidpRaNaYView Answer on Stackoverflow
Solution 4 - AndroidAndrew LeeView Answer on Stackoverflow
Solution 5 - AndroidLinhView Answer on Stackoverflow
Solution 6 - AndroidMusthafaView Answer on Stackoverflow
Solution 7 - AndroidRohan TanejaView Answer on Stackoverflow
Solution 8 - Androidhiddeneyes02View Answer on Stackoverflow
Solution 9 - AndroidThriveniView Answer on Stackoverflow
Solution 10 - AndroidRevanth KovuriView Answer on Stackoverflow
Solution 11 - AndroidAAryzView Answer on Stackoverflow
Solution 12 - AndroidAlen LeeView Answer on Stackoverflow
Solution 13 - AndroidAnkitView Answer on Stackoverflow
Solution 14 - AndroidPriyank PatelView Answer on Stackoverflow
Solution 15 - AndroidBrahem MohamedView Answer on Stackoverflow
Solution 16 - AndroidVibhanshu ShuklaView Answer on Stackoverflow
Solution 17 - AndroidDayona JosephView Answer on Stackoverflow
Solution 18 - AndroidAndrew ChelixView Answer on Stackoverflow
Solution 19 - AndroidCN_CabbageView Answer on Stackoverflow
Solution 20 - AndroidNativView Answer on Stackoverflow
Solution 21 - Androidlive-loveView Answer on Stackoverflow
Solution 22 - AndroidUMAR-MOBITSOLUTIONSView Answer on Stackoverflow
Solution 23 - AndroidAury0nView Answer on Stackoverflow
Solution 24 - Androidapurv thakkarView Answer on Stackoverflow
Solution 25 - AndroidSujit AcharyaView Answer on Stackoverflow
Solution 26 - AndroidRavirajView Answer on Stackoverflow
Solution 27 - AndroidZaid ZakirView Answer on Stackoverflow
Solution 28 - AndroidParth PatelView Answer on Stackoverflow
Solution 29 - AndroidJonathan ApplebaumView Answer on Stackoverflow