Data Binding: Cannot find symbol class BR

戏子无情 提交于 2020-01-11 02:06:10

问题


I try to use Data Binding in my existing project, but I can't get it to build.

Using in Project build.gradle:

dependencies {
    classpath 'com.android.tools.build:gradle:1.3.1'
    //Data Binding Beta
    classpath "com.android.databinding:dataBinder:1.0-rc4"

    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7+'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

applying

apply plugin: 'com.android.databinding'
apply plugin: 'com.neenbedankt.android-apt'`

and in Module:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    apt 'com.android.databinding:compiler:1.0-rc1+'
}

I tried to use Data Binding with RecyclerView/Fragment/ViewHolder/Adapter.. I use Android Studio 1.4.1. With compileSdkVersion 23 and buildToolsVersion "23.0.1". I tried it without apt, different gradle versions, proof read my classes/xml.. I also use still Butterknife(since my project is too big to change everything at once) and other (but unrelevant) libraries. I synced and rebuild my project, I closed/opened Android Studio, I tried to invalidate caches. I build a 'fresh' example from start, which worked fine.

My getter Methods in my "data class" are @Bindable.

Also, the databinding package is not created. (error message says it doesn't exist) and Error:cannot generate view binders java.lang.StringIndexOutOfBoundsException: String index out of range: -21


回答1:


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.




回答2:


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'



回答3:


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.




回答4:


I got the Error:cannot generate view binders java.lang.StringIndexOutOfBoundsException: String index out of range: -21 error almost everytime when I tried to use a ObservableList with custom objects. But I found out, that almost everytime I forgot a > after my type. Silly me. So, my solution was to check for it:

    <data>

        <import type="android.databinding.ObservableArrayList"/>

        <variable
            name="dices"
            type="ObservableArrayList&lt;Dice>" /> //don't forget the '>' after Dice
    </data>



回答5:


Sometimes DataBinding will show unrelated errors but in my case I forgot to add apply plugin: 'kotlin-kapt' in your Gradle App file




回答6:


Error might actually not be due to databinding. Javac truncates error logs, hence might not display actual issue.

Check out this link. https://movieos.org/2017/android-room-data-binding-compile-time-errors/




回答7:


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;
    



回答8:


"BR" import file is missing.

Add the below import file in your model class:

import com.yourpackagename.BR;


来源:https://stackoverflow.com/questions/33343411/data-binding-cannot-find-symbol-class-br

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!