java.lang.NoClassDefFoundError: android.databindin

2020-03-01 12:20发布

问题:

Data binding setup:

apply plugin: 'kotlin-kapt'

android {
    dataBinding {
        enabled = true
    }
}

dependencies {
    kapt "com.android.databinding:compiler:3.1.0"
}

The fragment class which uses data binding:

class LandingFragment : Fragment(), Injectable {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
        val dataBinding = LandingFragmentBinding.inflate(inflater, container, false)
        return dataBinding.root
    }
}

Every time the Espresso test is run for this fragment, I get the following exception:

java.lang.NoClassDefFoundError: android.databinding.DataBinderMapperImpl
at android.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:32)
at com.sc.databinding.LandingFragmentBinding.inflate(LandingFragmentBinding.java:42)
at com.sc.ui.landing.LandingFragment.onCreateView(LandingFragment.kt:32)
...

回答1:

A bit late, but I resolved this issue by adding DataBinding compiler with kapt as a test dependency:

kaptAndroidTest 'androidx.databinding:databinding-compiler:3.3.2'

Or the version not from AndroidX if your project is not using Jetpack yet.



回答2:

I run into this very error. I did 2 things: 1. Added kaptAndroidTest 'androidx.databinding:databinding-compiler:3.5.1' in gradle 2. Used the databinding, that is to say, I create a fake bool variable and injected it for real in a view. It would seem that you cannot just use databinding for retrieving the views instead of issuing the dreaded findViewById. You have to use it at least once in your module. Once you use it you are fine for all the other classes in your module.



回答3:

Try to add the android-apt plugin as per this stackoverflow answer:

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