Data Binding class not generated

88,344

Solution 1

I did not get any satisfying answers. So here are the tips which are summary of my data binding knowledge.

Tips For Solving DataBinding Issues

Update

To get more accurate errors and suggestions, I strongly recommend to update Android Studio and Gradle plugin version to the latest. Because I am not facing many issues after AS 3.2 version.

See Latest Android Studio, and Latest Gradle Plugin.

Orignal Solution

After reading this answer, you will not get stuck in data binding auto generation issues for both Classes and Data Variables.

Check these points one by one. Any of these can make your work done. Point 3 to last are really important, so don't miss them.

1. Check if data-binding enabled

You should have data binding enabled in build.gradle. If not then add this and Sync.

android {
    ...
    buildFeatures {
        dataBinding true
    }
}

2. Check layout is converted in binding layout

Now if you want data binding class to be generated then you should wrap xml layout with data binding (<layout tag). Something like this.

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

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

Along with this check whether the binding variable names are correct as in the view model class

3. Auto-generated Binding class name?

Your data binding class should be generated after creating binding layout.

If your layout name is in snake case activity_main.xml then data binding class will be generated in camel case like ActivityMainBinding.

4. Can't See Import Suggestion?

Sometimes when you type ActivityMai..., then it does not show suggestion, in that case import manually.

import <yourpackage>databinding.ActivityMainBinding;

5. Read Build Fail Logcat

Your binding class and new variables in layout will not be generated if your build fails. So first Make project by Ctrl + F9 (Build > Make project).

  • If a build fails then see what is an error, usually we have errors in layout fields. Error logs will point out the error line number with the issue.
  • Binding may fail cause some stupid error, like syntax error or missing import. In that case, you will get logcat full of errors of binding classes. But you should read complete logcat to find appropriate issue.

6. Close and open project from recent

I always do this because it takes much less time than Rebuild / Make project.

  • Close project from File > Close Project
  • Open again from recent

Note that I prefer Close and Open from Recent because it takes much less time than Rebuild / Restart IDE.

7. Rebuild Project

If still your class is not generated. (Some time when we paste layout file, then it happens). Then Rebuild Project from Build> Rebuild (Not Build or Make project). It will generate your data binding class. (Rebuild does Magic for me.)

8. Have latest Android Studio

After updating AS to Android Studio 3.2, I felt many bugs fix in data binding auto generation. So you should also have the latest AS.

#Solution for <variables

<data>
    <variable
        name="item"
        type="com.package.Model"/>
</data>

Usually, when we put a variable in layout, it creates a getter and setter of it. And we can use binding.setItem(item); and binding.getItem();, but if you can't see those methods then read the below information.

1. Close and open project from recent

If you have created a data variable - <variable in your layout and it does not show up its setter and getter in data binding class, then Close and Open from Recent your project.

2. Clean project after changing the type

If you changed the type of some <variable in your layout and getter setter type is not changing then Clean project (Build> Clean Project)

Final words

Finally if still your binding class is not generated, then we have our most powerful weapon. - Restart Android Studio

  • First, try just restart, this always generates variables of my binding layout after restart.
  • If it does not work then Invalidate Cache & Restart.

This is all that i do to solve my data binding errors. If you get any further issues, you can comment here.

Solution 2

DataBinding class generated automatically.

if your xml name is activity_test then the Binding class will be ActivityTestBinding.

but,

dataBinding {
        enabled = true
    }

layout should have layout as root

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

Solution 3

I had the same issue. After reading over the android sdk docs, there is only the expected file name to be created but nothing about what to do if it does not happen. I noticed after some more research that after removing the namespace to the layout element like below (using your example), it worked for me.

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

Solution 4

In my case, the Binding class was generated and in place ( but I thought it wasn't) ... but it does not automatically add import of said class to the activity/fragment import section... so... OPTION + ENTER :)

Solution 5

if you do base job, for enable databainding in your project, like use enable in gradle and use layout tag in xml, when you change xml code and did not generat new databinding class for those xml you can use a fast way for generation only data binding class in gradle->other->databindinggenbaseclassesDebug it fast more than bulid whole project. its generate only databinding class. enter image description here

Share:
88,344
Ravi
Author by

Ravi

Follow me on instagram for more programming posts: https://www.instagram.com/ravi.rupareliya/

Updated on January 07, 2022

Comments

  • Ravi
    Ravi over 2 years

    I am using Data Binding in my project, when using <layout> and <data> in my xml binding class is not generated.

    For example i have activity_main.xml

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

    Now if i am writing ActivityMainBinding in my activity/fragment it shows error that class is not available. But after including <variable> in my xml file, it is able to generate ActivityMainBinding class.

    Android Studio : 2.1.3
    Classpath : com.android.tools.build:gradle:2.1.3
    minSdkVersion 16
    targetSdkVersion 24
    buildToolsVersion 24.0.0

  • Ravi
    Ravi almost 8 years
    for gradle 1.5.0 and above, it is not needed to use this classpath.
  • Ravi
    Ravi almost 8 years
    I have clearly mentioned in my question that after adding <variable> in xml it is generating Binding class, so there is no case of not adding dataBinding in gradle file, if i have not added than it should not generate binding class.
  • Rob
    Rob about 6 years
    Bingo, worked for me, though I had to type out the import manually, intellisense wasn't offering the option for whatever reason.
  • Alexey Markov
    Alexey Markov about 6 years
    I am searching soultion for hours, make gradle magic a lot, but I use ConstraintLayout as a root. Thank you!
  • Vicky Kapadia
    Vicky Kapadia about 6 years
    This should be marked as the correct answer. Rest all are just workarounds. No need of moving namespaces or even adding data element
  • PravyNandas
    PravyNandas almost 6 years
    If the problem continues... (8) Invalidate & restart.... if not resolved, (9) change dataBinding { enabled = false } ... build, and then dataBinding { enabled = true } build again....
  • samus
    samus almost 6 years
  • Ivan Kaloyanov
    Ivan Kaloyanov almost 6 years
    In my case, I had to import the classpath manually for some reason the IDE does not show this as a solution. Tnx :)
  • M.Pomeroy
    M.Pomeroy almost 6 years
    Why would dataBinding require internet permission, in a general case?
  • Juliano
    Juliano over 5 years
    Saved my day. I was missing the <layout> wrapping in my layout file. Thank you!
  • Rohan Lodhi
    Rohan Lodhi over 5 years
    @M.Pomeroy yes it is not required, not edited the answer thanks
  • Nizamudeen Sherif
    Nizamudeen Sherif about 5 years
    Before Synching and restarting. Please try to 'Make'. It will workout.
  • Tim John
    Tim John almost 5 years
    You could also add that the "import android.R" must be removed, if its part of the import list. This solved my problem with Unresolved reference to the activity xml file
  • dphans
    dphans over 4 years
    No. Binding Resource (.BR.) like Resource (.R.). It generated based on your project resource (id, drawable, color,...) so it will place inside your project package (your.project.package.BR). Not inside third party library like androidx.databinding.library.baseAdapters.BR.
  • Martin Rajniak
    Martin Rajniak over 4 years
    "Close and open project from recent" saved it.
  • Sony
    Sony over 4 years
    3rd point is more important, my class name was "CommentsDialog" and layout file was "fragment_comment_dialog" and the generated binding file was "FragmentCommentDialogBinding"
  • rmirabelle
    rmirabelle over 4 years
    Very useful. Unfortunately, NONE of these suggestions work for me. Almost comically, My project compiles and runs just fine, while LITTERED with error messages. Wouldn't it be nice if chasing binding classes around all day every day wasn't something every Android developer had to add to their workload? I'm so very tired of doing this. Dear Google, please fix data binding in Android Studio.
  • corrado4eyes
    corrado4eyes about 4 years
    If even Invalidate & restart... In my case I deleted a BindingAdapter method which was linked with an attribute called "image", but the layout didn't notify with an error the line where I used "app:image=bla bla" and the compilation crashed in the DataBinderMapperImpl where was trying to import the non generated ActivityBlaBindingImpl (since in the XML there was an error). So check if you also forget to change an attribute.
  • Ethan McTague
    Ethan McTague over 3 years
    I find Updating the gradle plugin tends to break the entire project forever rather than fix anything
  • Abhinav Chauhan
    Abhinav Chauhan over 3 years
    In my case there was nothing wrong. I did Build > Make project and then it was successful. Thanks.
  • Ybz
    Ybz over 3 years
    As to Android Studio 4.1, the correct way to add databinding in the gradle files is: android { ... buildFeatures { dataBinding true } ... } maybe you'd like to edit your answer as it's a very popular issue/answer
  • Elron
    Elron over 3 years
    I tried all this variants and helped only to rename layout file, for example rename activity_home1.xml to activity_home2.xml then use ActivityHome2Binding class. "excellent" magic
  • Titus Sutio Fanpula
    Titus Sutio Fanpula about 3 years
    Thanks in advance. It's reallly helpfull
  • mickecast
    mickecast over 2 years
    Here in 2021, I still believe this is a bug with Artic fox 3.1 patch 3, but ater wraping the xml with the layout tag with no extra properties my ide stoped showing the databinding auto generated classes in red. My ide showed them in red, but everything compiled fine.
  • Blake
    Blake over 2 years
    As of 2022, the enableV2 property is deprecated
  • Samir
    Samir over 2 years
    Good options thanks we just need to confess binding is broken.
  • Antonio Labra
    Antonio Labra over 2 years
    You're a life saver!