Can't resolve Android databinding class
Asked Answered
D

29

64

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.

Devaluation answered 9/3, 2016 at 5:16 Comment(8)
What is your activity name??Dieback
@MD My actvity name is : MainActivity.javaDevaluation
Clean and Built project this will generate DataBinding class again as @RRR saidDieback
Already tried this things but it can't solve my problem!Devaluation
Anyone facing the initial databinding setup try this code2concept.blogspot.in/2016/07/…Norther
I noticed that if the xml layout file starts directly from a constraint layout it won't work. Wrap everything in <layout>, move xmlns:android="...", xmlns:app="...", and xmlns:tools="..." to it, and then clean and rebuild. The binding object should appear. Also as the others said, make sure your naming is correct.Fluoric
Possible duplicate of Data Binding class not generatedManilla
mobologicplus.com/… please check this tutorialContravene
D
20

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.

Devaluation answered 9/3, 2016 at 6:13 Comment(3)
You have fooled yourself, it will set your activity layout content_main.xml not activity_main.xml, What you have found is the default way to attach activity to layout.Manilla
Don't follow this answer, It will set layout content_main, If you want to inflate activity_main.xml then wrap it with <layout tag. and use ActivityMainBinding class.Manilla
Thanks @Khemraj for suggestion. It is an old answer. It may have an error. Can you please update answer if you can?Devaluation
C
108

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

Ceciliacecilio answered 9/3, 2016 at 5:16 Comment(6)
I am make sure with layout name as I already added in my question.Devaluation
yes but in question you have mentioned activity_main.xml and using MainActivityBinding , it should be ActivityMainBindingCeciliacecilio
Make sure you rebuild the project after adding xml components, so that this class is generated.Murderous
If you struggle with unresolve symbol, try wrap you layout in <layout> tag.Poteen
Whenever you work with binding follow steps 1: first do dataBinding {enabled = true } in gradle ..sync it ..2: use <layout > <data> </data></layout >tag if you have viewmodel then apply it in <variable > in your layout tag 3: then go to your activity and see ActivityMainBinding there .. if not again rebuild it and see it must be there ..ThanksQuicksand
Thanks a lot. Was suffering from last hour and trying to type MyActivityBinding is of ActivityMyBinding. Thanks again.Ptosis
L
27

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

Lynd answered 19/4, 2018 at 10:56 Comment(6)
It's better than restarting the IDERhodarhodamine
Interestingly AS was not even renaming xml file. As I changed from explorer, binding class was auto detected. A classic bug! Thanks for the pointer.Emie
awesome man..what a great answer. who could have thought renaming will fix it (hmmm...Android studio!!!..). i have been following it up by the books and was not working but this trick fixed it.Dissociable
This work for me, but i have lots of layous, there is not other solution? (Android 3.2)Mercy
@GilbertoIbarra can you try restarting android studioLynd
Bug still exists in Android Studio 3.2.1Stroh
D
20

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.

Devaluation answered 9/3, 2016 at 6:13 Comment(3)
You have fooled yourself, it will set your activity layout content_main.xml not activity_main.xml, What you have found is the default way to attach activity to layout.Manilla
Don't follow this answer, It will set layout content_main, If you want to inflate activity_main.xml then wrap it with <layout tag. and use ActivityMainBinding class.Manilla
Thanks @Khemraj for suggestion. It is an old answer. It may have an error. Can you please update answer if you can?Devaluation
I
18

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>
Indoeuropean answered 29/3, 2019 at 6:24 Comment(2)
Just having <layout> tag anywhere in the XML solves it.Macaroon
It saves my day actually I forget to do thatAdkison
O
9

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

Oisin answered 2/11, 2016 at 10:57 Comment(0)
H
8

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.

Hashish answered 16/5, 2018 at 4:28 Comment(0)
K
7

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.

Karlotte answered 5/9, 2017 at 13:32 Comment(0)
S
7

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;
Stonedeaf answered 17/8, 2018 at 8:2 Comment(0)
B
6

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.
Baroness answered 3/8, 2018 at 6:54 Comment(2)
I also tried to change bindings generation type from AS built-in to compiler and vice versa with restarting AS. Nothing happens. Yesterday everything worked fineRolon
I waste one hour to solve this, then I tried your solution I change name of xml still it doesn't work then I rebuild, clean restart and it works. thanksEstes
C
5

Make sure to add below lines in build.gradle file

dataBinding { enabled true }

Consol answered 4/8, 2019 at 17:33 Comment(0)
C
4

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.

Choker answered 19/4, 2017 at 17:3 Comment(0)
A
4

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);
Antecedents answered 6/5, 2018 at 11:43 Comment(0)
H
4
  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

Herson answered 18/9, 2021 at 9:58 Comment(0)
H
2

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.

Hans answered 9/3, 2016 at 5:25 Comment(0)
G
2

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>
Gab answered 2/4, 2016 at 19:9 Comment(0)
M
2

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

Millham answered 28/5, 2018 at 14:46 Comment(0)
C
1

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

Curhan answered 19/5, 2020 at 10:54 Comment(0)
U
1

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
Uxorial answered 18/11, 2020 at 12:9 Comment(0)
K
1

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">
Kilmarx answered 9/5, 2021 at 4:51 Comment(0)
M
1

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

buildFeatures {
    viewBinding true
}
Marplot answered 19/2, 2022 at 18:19 Comment(0)
K
0

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.

Konikow answered 9/11, 2017 at 9:8 Comment(0)
L
0

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>       
Legality answered 23/7, 2018 at 2:7 Comment(2)
why do you invoke setContentView() twice?Guimar
Not sure, I don't remember, this was a long time ago. I actually decided not to use Android data binding, I found it was inflexible.Legality
H
0

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.

Hullo answered 20/2, 2020 at 14:21 Comment(0)
V
0

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

Voltaic answered 17/6, 2020 at 18:49 Comment(0)
E
0

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
    }
}
Extensity answered 23/6, 2020 at 9:58 Comment(0)
K
0

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

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

Klenk answered 27/9, 2020 at 0:37 Comment(0)
S
0

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>
Sickler answered 4/1, 2021 at 11:48 Comment(0)
B
0

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%

Bangkok answered 1/5, 2021 at 4:35 Comment(0)
L
0

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'
}
Lenrow answered 1/2, 2022 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.