skip to Main Content

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


  1. 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.

    <layout 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">
    

    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,

    <layout 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">
    
    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroundColor"
        tools:context=".presentation.ui.activity.FullImageViewActivity">
    
        </androidx.constraintlayout.widget.ConstraintLayout>
      </layout>
    

    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,

    binding.root  
    

    will refer to Contraint Layout in my case.

    Login or Signup to reply.
  2. If you are only planning to use view references like you do by using

    • Kotlin synthetics (Deprecated)
    • findViewById (very bad for performance, especially if you are not storing the references)
    • ButterKnife (let’s just call it old)

    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,

        static <T extends ViewDataBinding> T bind(DataBindingComponent bindingComponent, View root,
            int layoutId) {
        return (T) sMapper.getDataBinder(bindingComponent, root, layoutId);
    }
    

    getDataBinder called on that mapper returns the particular ViewDataBinding.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search