gpt4 book ai didi

Android Espresso 测试因 IncompatibleClassChangeError 而失败

转载 作者:搜寻专家 更新时间:2023-11-01 08:36:59 24 4
gpt4 key购买 nike

我正在尝试学习适用于 Android 的 Espresso。为此,我创建了一个简单的 android studio 项目。但是我在运行测试用例时遇到了一些问题。

这是我的模块级 build.gradle 文件:

应用插件:'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.abdullah.mytestapplication"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"

testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.0'

testCompile 'junit:junit:4.12'

androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'
androidTestCompile 'com.android.support:support-annotations:23.2.0'

androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.2.1'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.1'
}

我的测试用例是:

@RunWith(AndroidJUnit4.class)
@SmallTest
public class MainActivityTest {

@Rule
public ActivityTestRule<MainActivity> mainActivityRule =
new ActivityTestRule<>(MainActivity.class);

@Test
public void myTest() {
Assert.assertNotNull(new Object());
}
}

测试运行的输出是:

Running tests
Test running started
java.lang.IncompatibleClassChangeError: com.abdullah.mytestapplication.MainActivity
at dalvik.system.DexFile.defineClassNative(Native Method)
at dalvik.system.DexFile.defineClass(DexFile.java:226)
at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219)
at dalvik.system.DexPathList.findClass(DexPathList.java:338)
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.abdullah.mytestapplication.MainActivityTest.<init>(MainActivityTest.java:19)
at java.lang.reflect.Constructor.newInstance(Native Method)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$

----------

.run(Instrumentation.java:1970)

Finish

我不确定这个异常的原因是什么以及如何摆脱它。当我在设备或模拟器上运行时,行为是相同的。

最佳答案

我从 this 找到了解决方法链接。

androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.2.1') {
exclude module: 'support-v4'
}

排除 support-v4 frm expresso-contrib 使其工作。


编辑

对于2.2.2版本需要做以下事情:

androidTestCompile('com.android.support.test.espresso:espresso-contrib:2.2.2') {
exclude module: 'support-annotations'
exclude module: 'support-v4'
exclude module: 'design'
exclude module: 'recyclerview-v7'
}

关于Android Espresso 测试因 IncompatibleClassChangeError 而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35910146/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com