36

I am using android's data binding library for views in a library project

i have added the following line in my root gradle file

classpath 'com.android.databinding:dataBinder:1.0-rc1'

and have enabled the dataBinding as given below in module's gradle file

apply plugin: 'com.android.databinding'
...
...
android {
....
  dataBinding {
      enabled = true
  }
}

i have enabled the multiDex in my library project as well as in the host app which is using this library but i get this stack trace of error while launching the library's activity

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/databinding/DataBinderMapper;
    at android.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:31)
    at sdk.ui.activities.MyActivity.onCreate(MyActivity.java:76)
    at android.app.Activity.performCreate(Activity.java:6280)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1116)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2647)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5763)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.databinding.DataBinderMapper" on path: DexPathList[[zip file "/data/app/testApp.dev-1/base.apk"],nativeLibraryDirectories=[/data/app/testApp.dev-1/lib/arm64, /data/app/testApp.dev-1/base.apk!/lib/arm64-v8a, /vendor/lib64, /system/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
    at android.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:31) 
    at sdk.ui.activities.MyActivity.onCreate(MyActivity.java:76) 
    at android.app.Activity.performCreate(Activity.java:6280) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1116) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2534) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2647) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1502) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5763) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749) 
Suppressed: java.lang.ClassNotFoundException: android.databinding.DataBinderMapper
    at java.lang.Class.classForName(Native Method)
    at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
    at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
        ... 15 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
anon
  • 855
  • 12
  • 22
A. kanojia
  • 431
  • 1
  • 4
  • 8

9 Answers9

62

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

android {
    ....
    dataBinding {
        enabled = true
    }
}
Bolein95
  • 2,947
  • 2
  • 26
  • 31
7

finally i was able to solve this issue. It seems there was a conflict between apt version of the app and library modules.

upgraded the apt version in the app to

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
A. kanojia
  • 431
  • 1
  • 4
  • 8
6

Besides adding the following to build.gradle:

android {
    dataBinding {
        enabled = true
    }
}

I also have to add the following dependency:

dependencies {
    kapt 'com.android.databinding:compiler:3.1.4'
}
Boken
  • 4,825
  • 10
  • 32
  • 42
chengsam
  • 7,315
  • 6
  • 30
  • 38
2

Best guess. Get rid of android-apt

And if there are libraries using apt

Instead of apt 'lt.mdm.sdd:myLib:1.5.1' use annotationProcessor 'lt.mdm.sdd:myLib:1.5.1'.

I'm not sure if this will help you, and i have no idea if library it self should be changed.

It helped me (i was using androidannotations.org older version with apt) at least.

Update

And i have no idea why you apply plugin: 'com.android.databinding'? It works without it as well.

Alpha
  • 1,754
  • 3
  • 21
  • 39
1

I had the same problem after performing the migration androdx. I tried all the above methods but not resolved. I suddenly found that I used com.android.tools.build:gradle version is 3.3. I update it to 3.5. Problem is solved.

project build.gradle: classpath 'com.android.tools.build:gradle:3.3.0' Update to classpath 'com.android.tools.build:gradle:3.5.0' Redo the migration.

Alex-guo
  • 21
  • 4
0

classpath 'com.android.databinding:dataBinder:1.0-rc1'

apply plugin: 'com.android.databinding'

Remove that lib. from gradle.

Lovekesh
  • 125
  • 11
0

I had the same problem, but I solved it removing

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

from the global build.gradle and removing

apply plugin: 'android-apt'

from the local build.gradle, then it worked like a charm.

It seems it was a mixed problem with Butterknife too. Pretty weird.

0

The new way of adding DataBinding is by adding on the build.gradle of all your modules:

android {
...

  buildFeatures {
    dataBinding true
  }
}

Don't forget to add also the kotlin-kapt plugin at the top:

plugins {
  ...
  id 'kotlin-kapt'
}
ricamgar
  • 143
  • 1
  • 4
-2

Get inspiration from @Bolein95 say.Because it depend on a library that does not support androidx(no setting databinding=true),Written by a colleague who has left.I copy a few necessary files from his github repo, it work!

Guo
  • 27
  • 4