Android - DataBinding - How and when the Binding classes will be generated?

Did you update your layout file for data binding? They are generated only for layouts which have data binding.

It has to start with a layout tag which has 2 child tags (data & your root view).

Something like this:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout ...
   </LinearLayout>
</layout>

When your layout has this form, AndroidStudio should auto complete the class. Also, by default, it is generated in <your.app.package>.databinding package.


Try to rename layout to main_activity.xml.

In my case, it didn't work to generate binding class for "activity_main.xml".
But renaming the layout file to main_activity.xml worked just fine.


What is your layout name?

The above layout file was activity_main.xml so the generate class was ActivityMainBinding.

What this means is that the generated class name will depend on your layout's name

activity_main.xml -> ActivityMainBinding.java

I think your activity name is "main_activity", so the generated binding class name should be MainActivityBinding not ActivityMainBinding


When it's not generating the binding class, I restart Android studio. Then the binding class will be generated. Isn't it caused by cache of android studio?

BTW, if you are using android-apt, please check it. Because it will cause binding class not to be generate.