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 “Bindi
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>
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.
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 beActivityHomeBinding.class
.
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;
Note that I recommend close and open from recent because it takes less time than Rebuild / Restart IDE.
If you still have issues. Let me know in comments, or see this answer for better understanding.
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
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.*;
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.