Android Data Binding: missing DataBinderMapper class

后端 未结 5 1039
忘掉有多难
忘掉有多难 2020-12-11 07:49
java.lang.NoClassDefFoundError: android.databinding.DataBinderMapper
        at android.databinding.DataBindingUtil.(DataBindingUtil.java:31)
        a         


        
相关标签:
5条回答
  • 2020-12-11 07:53

    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.

    0 讨论(0)
  • 2020-12-11 08:01

    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

    0 讨论(0)
  • 2020-12-11 08:02

    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.

    0 讨论(0)
  • 2020-12-11 08:07

    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.

    0 讨论(0)
  • 2020-12-11 08:13

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

    android {
    ....
    dataBinding {
        enabled = true
    }
    }
    
    0 讨论(0)
提交回复
热议问题