Data Binding class not generated
I did not get any satisfying answers. So here are the tips which are 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 the 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 {
...
buildFeatures {
dataBinding 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>
Along with this check whether the binding variable names are correct as in the view model class
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 likeActivityMainBinding
.
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 a build fails then see what is an error, usually we have errors in layout fields. Error logs will point out the error line number with the 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 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.
1. Close and open project from recent
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.
2. Clean project after changing the type
If you changed the 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
- First, try just restart, this always generates variables of my binding layout after restart.
- If it does not work then Invalidate Cache & Restart.
This is all that i do to solve my data binding errors. If you get any further issues, you can comment here.
Data binding class not generated in latest studio 3.6
This happened to me as well. The binding classes are actually generated. The project builds fine. Only Android Studio 3.6.1 (or underlying Gradle build system, I do not care) is buggy and cannot see these classes.
As an intermediate solution, I just hacked the source sets (please note that build variants in the fragment below are specific to my project, you need to rewrite it).
android {
...
sourceSets {
demoDebug {
java.srcDirs += 'build/generated/data_binding_base_class_source_out/demoDebug/out'
}
fullDebug {
java.srcDirs += 'build/generated/data_binding_base_class_source_out/fullDebug/out'
}
espressoDebug {
java.srcDirs += 'build/generated/data_binding_base_class_source_out/espressoDebug/out'
}
demoRelease {
java.srcDirs += 'build/generated/data_binding_base_class_source_out/demoRelease/out'
}
fullRelease {
java.srcDirs += 'build/generated/data_binding_base_class_source_out/fullRelease/out'
}
espressoRelease {
java.srcDirs += 'build/generated/data_binding_base_class_source_out/espressoRelease/out'
}
}
...
}
As pointed by Steve above: In the mean time, we have to patiently wait for Google to fix it...
EDIT
I have just realised it is MUCH more buggy than I expected, the layouts are broken too:
I hope Google will fix this mess soon...
EDIT 2
I have realized again that Android Studio 3.6 is even more buggy than described above.
The execution of existing Espresso tests is broken too.
I strongly discourage everyone from upgrading to Android Studio 3.6.
I the mean time, we will probably downgrade back to Android Studio 3.5.
Data Binding class not generated
I did not get any satisfying answers. So here are the tips which are 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 the 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 {
...
buildFeatures {
dataBinding 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>
Along with this check whether the binding variable names are correct as in the view model class
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 likeActivityMainBinding
.
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 a build fails then see what is an error, usually we have errors in layout fields. Error logs will point out the error line number with the 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 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.
1. Close and open project from recent
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.
2. Clean project after changing the type
If you changed the 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
- First, try just restart, this always generates variables of my binding layout after restart.
- If it does not work then Invalidate Cache & Restart.
This is all that i do to solve my data binding errors. If you get any further issues, you can comment here.
View Bindings not generating
I couldn't find my ViewBinding
files until it dawned on me that the bindings were being named after the XML
files ("fragment_home.xml") and not after the class ("HomeFragment.kt"). So I couldn't find them at HomeFragmentBinding
but I found them at FragmentHomeBinding.
I found it helpful to think of each ViewBinding as a helper singleton that has been created as that XML file's delegate.
(Edited to removed obsolete Gradle stuff)
Can't resolve Android databinding class
Thanks to all for your answer.I found solution with ContentMainBinding
class name for data binding.
Lets me explain.
NOTE: While using layout with <include ...
here is <include layout="@layout/content_main"
having Data Binding functionality, the class name related to include layout name. Here is the ContentMainBinding
My layout file are as below:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.databindingdemo.app.MainActivity">
...
<include layout="@layout/content_main" />
...
</android.support.design.widget.CoordinatorLayout>
And content_main.xml is layout where I added my Data Binding layout code.
So instead of using MainActivityBinding
it can resolved with ContentMainBinding
The code which work for me is below:
//Code for data binding
ContentMainBinding contentMainBinding = DataBindingUtil.setContentView(this, R.layout.content_main);
user = new User("Pranay", "Patel", "demoemail@gmail.com", "9999999999");
contentMainBinding.setUser(user);
DONE.
Data Binding class not generated
I did not get any satisfying answers. So here are the tips which are 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 the 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 {
...
buildFeatures {
dataBinding 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>
Along with this check whether the binding variable names are correct as in the view model class
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 likeActivityMainBinding
.
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 a build fails then see what is an error, usually we have errors in layout fields. Error logs will point out the error line number with the 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 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.
1. Close and open project from recent
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.
2. Clean project after changing the type
If you changed the 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
- First, try just restart, this always generates variables of my binding layout after restart.
- If it does not work then Invalidate Cache & Restart.
This is all that i do to solve my data binding errors. If you get any further issues, you can comment here.
Android ViewBinding Won't Generate Class File
Alright, so after a lot of trial and error, this is what worked for me:
1. Make New Project
I named mine "BindTest"
(Now, you may not have to start all over, but I took no chances)
2. Added an Empty Activity
It generated a "MainActivity" class file and an "activity_main.xml" layout file, along with its layout folder
3. Edit The Module Gradle File
At the child level of the Android
block, add this:
buildFeatures.dataBinding = true
The following didn't work, and the error suggested I change it to the above (And this is what they suggest on the official documentation site!):
buildFeatures{
viewBinding{
enabled=true
}
}
4. Sync Gradle File
In activity_bind.xml
:
Exchange
androidx.constraintlayout.widget.ConstraintLayout
with simplylayout
Remove the following from the layout (Else, you'll get an error saying there's a duplicate of one of them when you add another layout inside):
android:layout_width="match_parent"
android:layout_height="match_parent"
Remove default
TextView
and add a Linear Layout (or some other layout)(I removed this because the Design Window was being a way when I tried to add a Linear Layout without deleting it. If you can do it, more power to you, and tell me your dark secrets.)
Throw in some
TextView
s and give them IDs- I gave mine these:
tv1
,tv2
, andtv3
- I gave mine these:
5. Build
It should generate ActivityBindBinding
class (I regret my nomenclature decisions), but it does NOT rename the class file associated with the layout file; it merely does it under the covers. To actually see if what you did worked...
6. Edit BindActivity
Class
- Add the variable (Kotlin):
private lateinit var binding: ActivityBindBinding
- In overridden function
onCreate
:- Add
binding = ActivityBindBinding.inflate(layoutInflater)
after thesuper
call - Change the view in
setContentView
to bebinding.root
- Add
- In overridden function
You should now be allowed to use the IDs directly (binding.tv1
)
7. Test It
- In
onCreate
, last line:binding.tv1 = "Hello from inside the class"
When I ran the app after doing all this, it worked.
Test Array Binding
Don't know about you, but arrays in Kotlin are a pain to me, so I tested them to make sure I got this right.
- In
onCreate
, remove the last line we made and call to two methods. It should look like this:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
initTextViews() //these two
setTextViews() //right chere
}
- Populate the methods in the class:
private fun initTextViews() {
textViews = arrayOf(
binding.tv1,
binding.tv2,
binding.tv3
)
}
private fun setTextViews() {
textViews.forEachIndexed { ind, tv ->
tv.text = values[ind].toString()
}
}
I ran the app again: SUCCESSSSSSSSSSS!!
I'd like to point out that, when I tried to make a project with No Activity, it didn't work properly, claiming that there was "no Default Activity set". So, if you want to save yourself from some headache (Because this is enough), just start with an Empty Activity...please
Related Topics
Googleservice Failed to Initialize
Best Method to Download Image from Url in Android
No Matching Client Found for Package Name (Google Analytics) - Multiple Productflavors & Buildtypes
Counting Chars in Edittext Changed Listener
Replace Getmap with Getmapasync
Android Asynctask Example and Explanation
How to Calculate Distance Between Two Locations Using Their Longitude and Latitude Value
Android Map V2 Zoom to Show All the Markers
How to Handle Back Button in Activity
Finish an Activity from Another Activity
How to Detect Shake Event with Android
Using the Android Recognizerintent with a Bluetooth Headset
How to Mock Location on Device
Startforeground Fail After Upgrade to Android 8.1
Locationclient Getlastlocation() Return Null