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
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.
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 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<Dice>" /> //don't forget the '>' after Dice
</data>
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.
Sometimes DataBinding will show unrelated errors but in my case I forgot to add
apply plugin: 'kotlin-kapt'
in your Gradle App file