java.lang.NoClassDefFoundError: android.databinding.DataBinderMapper
at android.databinding.DataBindingUtil.(DataBindingUtil.java:31)
a
I was facing same error. What I have done is that I updated the dependency for databinding in app.gradle file
kapt 'com.android.databinding:compiler:3.1.0-alpha05'
to
kapt 'com.android.databinding:compiler:3.2.0-alpha04'
after updating the databinder compiler, I started getting this error. To get rid from this problem I have to revert the databinding compiler to old version and it taken me away from this problem. Now I am waiting for stable databinding compiler version and will upgrade to that version till then I will go with old compiler version.
You should include the android-apt plugin in your build.gradle in order to generate the android.databinding.DataBinderMapper
class.
In your project build.gradle:
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
classpath 'com.android.databinding:dataBinder:1.0-rc2'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
//.... more
}
In each module build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
apply plugin: 'com.neenbedankt.android-apt'
More information
I was having this issue this last week, and was having a hard time figuring it out, but finally I discovered that I had two layouts with the same name in different libraries that each use databinding. I'm guessing this was causing the generated binding classes to end up in some kind of bad state at runtime.
I was able to troubleshoot it into a situation where, instead of getting this error on one Fragment, I got a ClassCastException
to happen on the Fragment that had a layout with the same name. The generated Binding class was trying to cast a LinearLayout into a RelativeLayout. The only way it could be doing this is if it were trying to use a layout file from a completely different module that had the same name.
After making sure there were no layouts that share the same name -- even across different library modules -- it cleared up.
If you are facing this issue when you run your tests, just add:
kaptTest "androidx.databinding:databinding-compiler:+"
to dependencies
on build.gradle
files of all your modules.
android {
....
dataBinding {
enabled = true
}
}