5
java.lang.NoClassDefFoundError: android.databinding.DataBinderMapper
        at android.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:31)
        at com.example.MainActivity.onCreate(MainActivity.java:13)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

I hope this is a bug, does anyone face a similar problem? I unzipped classes.jar under the exploded-arr folder, but couldn't find the DataBinderMapper class.

Any workarounds / fixes would be appreciated.

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155

5 Answers5

22

Make sure that ALL your modules that use DataBinding have it enabled. This was the reason I got that exception.

android {
    ...
    dataBinding {
        enabled = true
    }
}
jeprubio
  • 17,312
  • 5
  • 45
  • 56
madhu527
  • 4,644
  • 1
  • 28
  • 29
  • 7
    It really works. In my case, I only enable dataBinding for `feature-home` module but forgot to enable it in `app` module too. That's why I see this case. After adding dataBinding to `app` module too then it works as expected. – anticafe Apr 27 '19 at 01:06
  • 3
    Lemme start a bounty and award this answer. tl;dr - if some `module` uses databinding, but application (`app`) module does not - it won't work anywhere. so `module` makes your `app` use it as well. – Shark Dec 17 '19 at 13:55
  • I had to enable databinding in `app` as well. This is the way. – bko Aug 25 '21 at 20:00
  • 1
    A wise man once said that don't miss the comment section part If the answer is not working. @anticafe Thanks! – Shahzad Afridi Jan 21 '22 at 05:38
1

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

Kavinda Gayashan
  • 387
  • 4
  • 17
1

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.

Rahul Sharma
  • 12,515
  • 6
  • 21
  • 30
0

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.

ColdSnickersBar
  • 297
  • 1
  • 5
0

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.

Osman Yalın
  • 620
  • 7
  • 7