gpt4 book ai didi

android - 在 AVD 模拟器上运行 Gradle connectedCheck 时出现 NoClassDefFoundError

转载 作者:太空宇宙 更新时间:2023-11-03 11:55:58 25 4
gpt4 key购买 nike

我正在尝试运行 Espresso测试 Android 应用程序。它在硬件设备上运行良好。当我在新创建的 AVD 模拟器 上运行它时,它失败,如下所示:

:ExampleApp:connectedDebugAndroidTest

com.example.MainFragmentTest > initializationError[Nexus_5_API_19(AVD) - 4.4.2]
FAILED
java.lang.NoClassDefFoundError: com/example/MainActivity
at java.lang.Class.getDeclaredFields(Native Method)
:ExampleApp:connectedDebugAndroidTest FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':ExampleApp:connectedDebugAndroidTest'.
> There were failing tests. See the report at:
file:///home/user/work/ExampleApp/build/reports/androidTests/connected/index.html

HTML 报告包含以下堆栈跟踪:

java.lang.NoClassDefFoundError: com/example/MainActivity
at java.lang.Class.getDeclaredFields(Native Method)
at java.lang.Class.getDeclaredFields(Class.java:610)
at org.junit.runners.model.TestClass.getSortedDeclaredFields(TestClass.java:77)
at org.junit.runners.model.TestClass.scanAnnotatedMembers(TestClass.java:70)
at org.junit.runners.model.TestClass.<init>(TestClass.java:57)
at org.junit.runners.ParentRunner.createTestClass(ParentRunner.java:88)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:83)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at android.support.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:38)
at android.support.test.runner.AndroidJUnit4.<init>(AndroidJUnit4.java:36)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.buildAndroidRunner(AndroidAnnotatedBuilder.java:57)
at android.support.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:45)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runner.Computer.getRunner(Computer.java:40)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:31)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at org.junit.runners.Suite.<init>(Suite.java:81)
at org.junit.runner.Computer.getSuite(Computer.java:28)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:701)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:664)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:329)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:226)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)

Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.example.MainActivity" on path: DexPathList[[zip file
"/system/framework/android.test.runner.jar", zip file
"/data/app/com.example.debug.test-1.apk", zip file
"/data/app/com.example.debug-1.apk"],
nativeLibraryDirectories=[/data/app-lib/com.example.debug.test-1,
/data/app-lib/com.example.debug-1, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
... 28 more

我基本上遵循espresso/BasicSample/ChangeTextBehaviorTest.java中的结构.此测试在模拟器上有效!

对于我的测试,当我只使用以下代码时,我可以归结为运行测试已经失败:

@RunWith(AndroidJUnit4.class)
public class MainFragmentTest {

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

完整的测试类如下:

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.example.R;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

@RunWith(AndroidJUnit4.class)
public class MainFragmentTest {

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

MainActivity mMainActivity;

MainFragment mMainFragment;

@Before
public void setUp() {
mMainActivity = mActivityRule.getActivity();
mMainFragment = (MainFragment) mMainActivity
.getSupportFragmentManager()
.findFragmentByTag(MainFragment.FRAGMENT_TAG);
}

@Test
public void testHeadline() {
onView(withId(R.id.headline)).check(matches(withText(R.string.headline)));
}
}

我目前正在使用以下工具链:

  • com.android.tools.build:gradle:1.4.0-beta2
  • buildToolsVersion "23.0.1"
  • compileSdkVersion 22
  • targetSdkVersion 22
  • 渐变包装器 2.5
  • Java 版本“1.7.0_79”OpenJDK

最佳答案

经过反复试验,我通过这个调整让我的运行起来了:

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

改为

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

不知何故,espresso-contrib 中包含的一些东西阻止了 support-v4 的正确版本被包含在测试包中,并导致类加载器失败。

关于android - 在 AVD 模拟器上运行 Gradle connectedCheck 时出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32603382/

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