[英]java.lang.NoClassDefFoundError: android.databinding.DataBinderMapperImpl when running Espresso tests
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:每次为此片段运行 Espresso 测试时,我都会收到以下异常:
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)
...
A bit late, but I resolved this issue by adding DataBinding compiler with kapt as a test dependency:有点晚了,但我通过添加带有 kapt 的 DataBinding 编译器作为测试依赖项解决了这个问题:
kaptAndroidTest 'androidx.databinding:databinding-compiler:3.3.2'
Or the version not from AndroidX if your project is not using Jetpack yet.或者,如果您的项目尚未使用 Jetpack,则该版本不是来自 AndroidX。
Add添加
kaptTest "androidx.databinding:databinding-compiler:+"
to dependencies
on build.gradle
files of all your modules. dependencies
于所有模块的build.gradle
文件。
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.
我做了两件事:1.在gradle中添加了kaptAndroidTest 'androidx.databinding:databinding-compiler:3.5.1' 2.使用了数据绑定,也就是说,我创建了一个fake bool变量并将其注入到视图中。 It would seem that you cannot just use databinding for retrieving the views instead of issuing the dreaded findViewById.
似乎您不能只使用数据绑定来检索视图而不是发出可怕的 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.
一旦你使用它,你就可以很好地处理模块中的所有其他类。
I have the same this issue, and fixed by adding我有同样的这个问题,并通过添加修复
kapt {
generateStubs = true
}
in build.gradle app (all module if using dataBinding)在 build.gradle 应用程序中(如果使用数据绑定,则为所有模块)
apply plugin: 'kotlin-kapt'
android {
...
dataBinding {
enabled = true
}
}
kapt {
generateStubs = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
...
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
kapt "com.android.databinding:compiler:$android_plugin_version"
}
In build.gradle project在 build.gradle 项目中
buildscript {
ext.kotlin_version = '1.3.70'
ext.android_plugin_version = '3.5.2'
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:$android_plugin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
尝试根据此stackoverflow 答案添加 android-apt 插件:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.