pRaNaY
pRaNaY

Reputation: 25310

Can't resolve Android databinding class

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.

Upvotes: 66

Views: 97615

Answers (29)

Jonathan Applebaum
Jonathan Applebaum

Reputation: 5986

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

buildFeatures {
    viewBinding true
}

Upvotes: 1

Parth Patel
Parth Patel

Reputation: 891

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

Upvotes: 0

CN_Cabbage
CN_Cabbage

Reputation: 445

  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

Upvotes: 5

Andrew Chelix
Andrew Chelix

Reputation: 1212

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

Upvotes: 1

Zaid Zakir
Zaid Zakir

Reputation: 563

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%

Upvotes: 0

Raviraj
Raviraj

Reputation: 926

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>

Upvotes: 0

Dayona Joseph
Dayona Joseph

Reputation: 31

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

Upvotes: 1

Sujit Acharya
Sujit Acharya

Reputation: 31

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

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

Upvotes: 0

Apurv Thakkar
Apurv Thakkar

Reputation: 10228

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

Upvotes: 0

Aury0n
Aury0n

Reputation: 217

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

Upvotes: 0

Vibhanshu Shukla
Vibhanshu Shukla

Reputation: 309

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

Upvotes: 1

UMAR-MOBITSOLUTIONS
UMAR-MOBITSOLUTIONS

Reputation: 78004

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.

Upvotes: 0

Revanth Kovuri
Revanth Kovuri

Reputation: 161

Make sure to add below lines in build.gradle file

dataBinding { enabled true }

Upvotes: 5

Andrew Lee
Andrew Lee

Reputation: 466

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>

Upvotes: 18

dzboot02
dzboot02

Reputation: 2979

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;

Upvotes: 8

Thriveni
Thriveni

Reputation: 771

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.

Upvotes: 6

live-love
live-love

Reputation: 52494

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>       

Upvotes: 0

Brahem Mohamed
Brahem Mohamed

Reputation: 392

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

Upvotes: 2

Musthafa
Musthafa

Reputation: 261

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.

Upvotes: 8

Alen Lee
Alen Lee

Reputation: 2509

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

Upvotes: 4

Ismail Iqbal
Ismail Iqbal

Reputation: 2580

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

Upvotes: 27

Nativ
Nativ

Reputation: 3150

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.

Upvotes: 0

Rohan Taneja
Rohan Taneja

Reputation: 10727

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.

Upvotes: 7

AAryz
AAryz

Reputation: 160

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.

Upvotes: 4

Linh
Linh

Reputation: 61009

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

Upvotes: 9

Priyank Patel
Priyank Patel

Reputation: 12362

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>

Upvotes: 2

pRaNaY
pRaNaY

Reputation: 25310

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.

Upvotes: 20

Ankit
Ankit

Reputation: 429

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.

Upvotes: 2

Rocky
Rocky

Reputation: 268

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

Upvotes: 109

Related Questions