本文介绍了java.lang.NoClassDefFoundError:运行Espresso测试时的android.databinding.DataBinderMapperImpl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据绑定设置:

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
    }
}

每次对此片段运行Espresso测试时,都会出现以下异常:

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)
...

推荐答案

有点晚了,但是我通过添加带有kapt作为测试依赖项的DataBinding编译器解决了这个问题:

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'

或者如果您的项目尚未使用Jetpack,则不是AndroidX版本.

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

这篇关于java.lang.NoClassDefFoundError:运行Espresso测试时的android.databinding.DataBinderMapperImpl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 17:53