Android - DataBinding - How and when the Binding classes will be generated?
AndroidData BindingAndroid Problem Overview
DataBinding Guide States
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 activity_main.xml so the generate class was ActivityMainBinding.
When will the Binding class , here say ActivityMainBinding, will be generated. I have compile time error. "cannot resolve ActivityMainBinding".
ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
Any help is appreciated. Thanks
Android Solutions
Solution 1 - Android
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
Solution 2 - Android
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.
Solution 3 - Android
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.
Solution 4 - Android
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.
Solution 5 - Android
###Rule
Layout name is in snake_case
, and generated binding class name will be PascalCase
.
> If you layout name is activity_home.xml
then binding class name will
> be ActivityHomeBinding.class
.
###Problem
- Many times you don't get import suggestion of DataBinding class.
- Binding class is not generated when there is some issue in layout.
- Binding class are not available when build is failed
###Here is hack
###When you Don't get Import Suggestion
-
When you don't get import suggestion. Import manually your binding class like this. (IDE often does not show suggestions for binding classes)
import <yourpackage>databinding.ActivityMainBinding;
Import line still shows error?
- If your import line shows error, then try make project (ctrl + F9) / Build> Make Project. .
- If Build is failed due to some error, then solve it.
- If build is successful then binding class will be generated.
Quick hack for generating binding class-
- If your binding class not generated then close project (File > Close Project) and open from recent.
Note that I recommend close and open from recent because it takes less time than Rebuild / Restart IDE.
Quick hack for generating layout variables in binding class-
- If your layout data variable are not generated then close project (File > Close Project) and open from recent.
If you still have issues. Let me know in comments, or see this answer for better understanding.
Solution 6 - Android
Make sure that the layout of the:
<data>
<variable name="user" type="com.example.User"/>
</data>
Is in the activity_main.xml if you are looking for ActivityMainBinding. In most boilerplate projects you set your content view to activity_main.xml, but then inflate you fragment_main.xml.
If you put your <data>
in fragment_main.xml layout then the class that is generated will be FragmentMainBinding.
Note: This may seem obvious after reading, but it is something that can be easily overlooked when following the android documentation.
Solution 7 - Android
If you ever wanted to give your own Binding class name just add class
attribute to the data
element with class name
>Binding classes may be renamed or placed in different packages by adjusting the class attribute of the data element.
<data class="ContactItem">
...
</data>
<data class="com.example.ContactItem">
...
</data>
Solution 8 - Android
In my case Restarting Android Studio does work. It is bug in android-apt
Solution 9 - Android
Just a simple restart didn't work for me. I had to Invalidate Caches and Restart
then I could see the binding classes. It's under the File
menu for those who don't know.
Solution 10 - Android
For android databinding to work properly you have to use android tools for gradle (com.android.tools.build:gradle) >=1.3.0.
So your project build.gradle must look like:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath "com.android.databinding:dataBinder:1.0-rc1"
}
}
allprojects {
repositories {
jcenter()
}
}
And module build.gradle must have that plugin:
apply plugin: 'com.android.databinding'
After all, check in your module you're using latest buildToolsVersion (right now it is 22.0.1). I am not sure that's required but it possibly will make you feel good about you're on the "bleeding edge of technology" ^_^ :
buildToolsVersion '22.0.1'
Resync gradle and rebuild your project. It is likely possible that without rebuild of project you might not get SomeLayoutBinding
classes generated.
In Android Studio that could be done from application menu: Build -> Rebuild project
Solution 11 - Android
Faced the same issue.
Reason for "cannot resolve ActivityMainBinding" is that your Binding file isn't generated. Try to clean and rebuild project. Also make sure you have followed the steps :
- classpath "com.android.databinding:dataBinder:1.0-rc4" --> include this in main module build file
- apply plugin: 'com.android.databinding' to your app module you are building
- Clean
- Rebuild. Check under ,if build->indermediates->your package name->databinding folder is generated containing ActivityMainBinding class.
Alternatively if you want to see the generated source.
- classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7' -->apply to your main module.
- apply plugin: 'com.neenbedankt.android-apt' --> to you app module
- apt 'com.android.databinding:compiler:1.0-rc0' --> include in your dependencies.
These classes will be generated on first build. Also make sure the applicationId in your application module is same as the package name in AndroidManifest.xml of your app module. Hope this helps.
Solution 12 - Android
My experience is that Android Studio 3.1.3 generated the databinding class after I clicked "Build > Rebuild Project". The file was stored in:
\Project\app\build\generated\source\apt\development\debug\project\android\app\databinding
However, Android Studio still reported the binding class as "Unknown" in source code.
To finally fix it, I manually added the import statement:
> import project.android.app.databinding.*;
Solution 13 - Android
A weird "bug" in android-studio causes the generated BR.java file not to use the @Bindable
fields and still only have the _all
property if a res/layout directory doesn't exist.
It happened to me when I wanted to create a "No Activity" App to have a library that contained my different bindable data.
Solution 14 - Android
In my case i just enclosed my layout xml in <layout></layout>
tags and that's it, without adding the data tag, i was successful to generate Data Bindings. Hope this helps.
<data>
<variable name="user" type="com.example.User"/>
</data>
Solution 15 - Android
<data class="MainActivityBinding">
<variable
name="user"
type="com.example.administrator.my_mvvm.User" />
</data>
//Remember need Build-> Rebuild Project
Solution 16 - Android
check the fields access level of your object, when i turned public it worked here
Solution 17 - Android
If you don't want to restart android studio, one way is to rename activity_main.xml to something else e.g activity_m.xml and then rename it back to activity_main.xml. It is much faster than restarting android studio.
To rename, right click on activity_main.xml and choose Refactor => Rename
Solution 18 - Android
I ran into a similar issue that hasn't been mentioned here yet. In my case, I incorrectly added <Layout>
as the tag versus <layout>
. <Layout>
is apparently a valid tag because Android Studio didn't complain (other than requiring layout_width and layout_height which aren't required for <layout>
). The capitalization difference will manifest in similar behavior where you cannot import the databinding class (import com.example.android.aboutme.databinding.ActivityMainBinding in my case) in the MainActivity class even though <Layout>
is a valid tag and the build.gradle file is defined properly to support data binding.
In my case, I did not need the <data>
tags defined yet in order for ActivityMainBinding to validly import and be referenced in:
private lateinit var binding: ActivityMainBinding
I was working through the this codelab when I ran into this behavior: https://developer.android.com/codelabs/kotlin-android-training-data-binding-basics/index.html
Solution 19 - Android
Hi maybe you can try this as well include this in build.gradle(:app)
inside the android block
buildFeatures { viewBinding true }
worked for me, maybe it could for you as well
Solution 20 - Android
activity_main.xml file needs to start with layout tag as shown below.