Data Binding class not generated

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

Answers


I did not get any satisfying answer. So here are the tips which is 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 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 {
...
   dataBinding {
        enabled = 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>
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 build fails then see what is error, usually we have errors in layout fields. Error logs will point out error line number with 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 bug fix in data binding auto generation. So you should also have latest AS.

Solution for <variables

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

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

1. Close and open project from recent

If you have created an 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 type

If you changed 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:D

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

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


DataBinding class generated automatically.

if your xml name is activity_test then 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 :)


After having set up it properly (including xml namespace and wrapping everything into <layout>), the one which worked for me was doing Build -> Make project. Nor Clean Project or Rebuild Project did. I'm in Android Studio 2.3.1, using 26.0.2 build tools. There's no need for <data> tags.


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.


dataBinding {
        enabled = true
    }

To enable the new data binding compiler, add the following option to your gradle.properties file:

android.databinding.enableV2=true

Data binding classes are generated during the build so after you enable data binding in the Gradle build of the app and surround your xml with the layout tag you should rebuild your app. If that doesn't help, delete your build folder and do it again.


Thanks to this answer here - it looks like the "layout namespace" needs to either be cleared out, or you need a new unique name.

Here are the options that worked for me:

  1. Create a fresh name for the layout to make sure it can be generated. This solved my issue, where I had a layout that was first created, without data binding - let's call it fragment_abc.xml. When I tried to add data binding to it, it was not recognized - even after multiple clear cache & restart calls. However, soon as I made a copy of the layout fragment_abc2.xml, immediately I got the generated data-binding .java/.class object.

  2. After seeing the above work, I tried to just remove the /build folder from the module, and rebuilt the project - this worked to get data binding for the original layout.


The only thing I can imagine if possible is if you don't have

dataBinding {
    enabled true
}

in your gradle file. If not just add it to the gradle file. i.e

android {
  ......

  dataBinding {
    enabled true
  }
}

then sync your project. If it still fail, you may need to do Invalidate/Restart of your android studio


When you work with a multimodule Android application check your binding class path. Maybe you should use:

import com.yourcompany.app.android.modulename.databinding.FragmentABCtBinding insted of:

import com.yourcompany.app.android.databinding.FragmentABCtBinding


I had same problem. Everything you done correct. Thing is you need add variable inside data tag in xml. For that you should create a sample model class and add it as variable in data tag.

Until that you won't get to see generated ActivityMainBinding.


I do not know it will work for you or not. Just rename the layout XML file name. Like suppose your layout name is activity_main.xml just change rename it to anything like main.xml and again rename it to activity_main.xml. Then you can able to see the import option on ActivityMainBinding.

Hope it works for you.


I had a similar issue where I had the layout wrapped and my data binding enabled in the gradle file. Main activity still couldn't intellisense or see my classes. What fixed it for me was adding the binding variable and importing the binding anyway. From there I just built the solution and then it seemed to know what the class was. From there I was able to import my camel case classes that were generated.


I got the issue and the problem was in the layout the field used was not a String, it was a Date.

Looks that all field got to be text to work, at least with the TexView component.

If you build with the command ./gradlew build --stacktrace

This show better the errors.


In my case, I thought that generated class had to appear with my usual classes in src folder. In addition, I thought that the name of the generated class should be slightly different. It all was my mistake. The generated class can be found in build folder, build -> generated -> ... path. If there is no import of the generated class in your activity, add the import

import com.yourcompany.app.databinding.ActivityMainBinding;"

1.Add Below in app gradle

 dataBinding {
        enabled = true
    }

2.In xml layout write below code

<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
  <data></data>
</layout>if you don't get data binding class just rename the layout file name and you will get data binding class.

use class path 'com.android.databinding:dataBinder:1.0-rc0'


Need Your Help

(Why) do we need to call cache or persist on a RDD

scala apache-spark rdd

When a resilient distributed dataset (RDD) is created from a text file or collection (or from another RDD), do we need to call "cache" or "persist" explicitly to store the RDD data into memory? Or ...

How to redirect all HTTP requests to HTTPS

security http .htaccess redirect https

I'm trying to redirect all insecure HTTP requests on my site (e.g. http://www.example.com) to HTTPS (https://www.example.com). I'm using PHP btw. Can I do this in .htaccess?