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
The BindingHolder object has a getBinding() method returning the ViewDataBinding base class. Note: The Data Binding Library generates a class named BR in the module package which contains the IDs of the resources used for data binding. In the example above, the library automatically generates the BR. item variable.
Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression. Expressions can be used for many purposes depending on your requirement.
For AndroidX projects:
I faced this issue after migrating to androidx, this is how I solved it.
But if the error persists, add the code below in your imports then rebuild the project.
import androidx.databinding.library.baseAdapters.BR;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With