Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Binding class not generated

People also ask

How do you create a binding class?

A binding class is generated for each layout file. 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. The above layout filename is activity_main. xml so the corresponding generated class is ActivityMainBinding.

What is Br class in data binding?

The BindingHolder object has a getBinding() method returning the ViewDataBinding base class. Note: The Data Binding Library generates a class named BR in the module package which contains the IDs of the resources used for data binding. In the example above, the library automatically generates the BR. item variable.


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.


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>

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> 

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


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