I have added to my build script
buildFeatures {
dataBinding true
}
But when I want to use Binding class it is not recognized.
I have tried with clean and rebuild, but it doesnt work.
I tried making new xml files and trying to create for them Binding instance but nothing worked.
Did I miss some dependency, or what is going on?
Thanks!
2
Answers
You are using Data Binding which binds the whole layout file with the Activities , Fragments and Adapters and it doesn’t generates Automatically you have to explicitly tell the Android Studio ( I don’t know who does all this stuff under the hood ) by adding this Tag in your XML File.
This will just tell Android Studio to generate the Binding File for this layout because generally we don’t want to generate the Binding Files for all the layouts if we have a very large project because it increase Build time.
You can use any Layout under this Tag Like,
Remember layout Tag just tells the Android Studio that yes we want to generate Binding File for this layout but this will not be your Root View, Like for example,
will refer to Contraint Layout in my case.
If you are only planning to use view references like you do by using
you can use view binding (a lite version of data binding). This way you don’t have to add a layout tag and the binding will be generated for all the XML files.
Answering @VaibhavGoyal – Who does all the stuff under the hood ?
Check DataBinderMapperImpl located at app/build/generated/source which is generated by the annotation processor for each module. When you define an XML file with a layout tag, an entry is added to this mapper. When you call bind in DataBindingUtil,
getDataBinder called on that mapper returns the particular ViewDataBinding.