Data Binding: Cannot find symbol class BR

I am using data binding from long time, and I came up with some key points, that can help you too.

This answer will help you generate ViewDataBinding, BindingResource(BR) class & layout variables in ViewDataBinding class.

6 Tips not to stuck in binding.

(1) You should have data binding enabled in build.gradle. If not then add this and Sync.

android {
...
   dataBinding {
        enabled = true
    }
...
}

(2) 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>

(3) Now your data binding class should be generated.

If your layout name is in snake case activity_main.xml then data binding class will be generated in camel case like ActivityMainBinding.

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

(4) 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.)

(5) 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.

(6) 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.


After update Android Studio to version 2.3 and Gradle 3.3 I had this error and the solution for me was add this to my build.gradle(app) file

apt 'com.android.databinding:compiler:2.3.0'

I have had different issues with DataBinding similar to your, for instance that Android Studio cannot find the generated Binding classes like "MainActivityBinding". Restarting Android Studio solved it for me.

As to the BR classes, I have had issues with them not being found, but usually it was because of an error I had made in the layout-class. For instance binding to a field that does not exist. Make sure everything else is correct, then restart Android Studio.


For AndroidX projects:

I faced this issue after migrating to androidx, this is how I solved it.

  • BR class is auto-generated from data binding when you rebuild the project.
  • But if the error persists, add the code below in your imports then rebuild the project.

    import androidx.databinding.library.baseAdapters.BR;