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
I did not get any satisfying answers. So here are the tips which are summary of my data binding knowledge.
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.
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.
You should have data binding enabled in build.gradle
. If not then add this and Sync.
android {
...
buildFeatures {
dataBinding true
}
}
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
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 likeActivityMainBinding
.
Sometimes when you type ActivityMai...
, then it does not show suggestion, in that case import manually.
import <yourpackage>databinding.ActivityMainBinding;
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).
I always do this because it takes much less time than Rebuild
/ Make
project.
Note that I prefer Close and Open from Recent because it takes much less time than Rebuild / Restart IDE.
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.)
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.
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.
If you changed the type of some <variable
in your layout and getter setter type is not changing then Clean project (Build> Clean Project
)
Finally if still your binding class is not generated, then we have our most powerful weapon. - Restart Android Studio
This is all that i do to solve my data binding errors. If you get any further issues, you can comment here.