android: data binding error: cannot find symbol class
I consistently run into this problem. I believe it has to do with android studio not being aware of dynamically generated files. If you have everything else right for databinding try to File > Invalidate Caches/Restart... and select Invalidate Caches and Restart. Then try and import the BR file... it should import fine.
You may have to throw in a Clean and Rebuild.
You can see Main Answer for complete information about data binding errors and solutions. I will try to tell some important points below.
5 Tips that I use to solve binding issues.
Assuming that you have enabled data binding in build.gradle
and you have converted your layout to binding layout.
First of all binding class auto generate when you convert layout to binding layout. Still sometimes it is not generated when background threads are not working. Here are some tips for you to resolve this.
(1) Name of generated class
Layout name is in snake_case activity_main.xml
Data binding class will be in PascalCase like ActivityMainBinding
.
(2). Type full name of Binding class
I felt sometimes when you type ActivityMai...
, then it does not show suggestion, but that does not mean class is not generated. In that case you should type full name of expected generated class. Like type ActivityMainBinding
and it will show import popup. (That's what i faced many times.)
Still not getting import suggestion. Try manual import.
import <yourpackage>databinding.ActivityMainBinding;
(3). 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 all times.)
(4) If you have created an <variable
in your layout and it does not show up its setter and getter in data binding class, then follow 4th point.
(5) Still if your class is not generating then you should check if build is not failing due to an error in your layout file. Data binding class will generate with a successful build.
This is all what i do to solve my data binding errors. If you get any further issue, you can comment here.