带有MultiDex-App的AndroidJUnitRunner @ java.util.Objects的NoClassDefFoundError

时间:2016-06-17 12:50:17

标签: java android unit-testing testing android-espresso

我在MultiDex-Application中使用NoClassDefFoundError和AndroidJUnitRunner几天就挣扎......

因为我们想开始使用Espresso进行Click-Tests,我们将TestRunner从'MultiDexTestRunner'更改为'AndroidJUnitRunner'。有了这个改变,我们在API 19下面的设备遇到了一些问题。当我们执行@SmallTests

时,我们得到“没有测试执行”的响应导致NoClassDefFoundError

在我们的Logcat中,我们获得以下信息:

android:launchMode

以下是WakefulTestRunner所需的部分:

06-17 14:19:13.832 26531-26779/com.xxxxxx.debug E/AndroidRuntime: FATAL EXCEPTION: Instr: com.xxxxxxxx.test.WakefulTestRunner
java.lang.ExceptionInInitializerError
at android.support.test.internal.runner.TestRequestBuilder.addFromRunnerArgs(TestRequestBuilder.java:713)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:350)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:260)
at com.xxxxxxxxxx.test.WakefulTestRunner.onStart(WakefulTestRunner.java:54)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1619)
Caused by: java.lang.NoClassDefFoundError: java.util.Objects
at android.support.test.internal.runner.TestSize.hashCode(TestSize.java:212)
at java.util.HashMap.put(HashMap.java:390)
at java.util.HashSet.add(HashSet.java:95)
at java.util.HashSet.<init>(HashSet.java:77)
at android.support.test.internal.runner.TestSize.<clinit>(TestSize.java:65)
at android.support.test.internal.runner.TestRequestBuilder.addFromRunnerArgs(TestRequestBuilder.java:713) 
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:350) 
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:260) 
at com.xxxxxxxx.test.WakefulTestRunner.onStart(WakefulTestRunner.java:54) 
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1619) 

我们在build.gradle中也排除了很多不同的东西,但没有任何帮助:

public class WakefulTestRunner extends AndroidJUnitRunner {

private PowerManager.WakeLock        wakeLock;
private KeyguardManager.KeyguardLock keyguardLock;

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle arguments) {
    MultiDex.install(getTargetContext());
    super.onCreate(arguments);

    String simpleName = WakefulTestRunner.class.getSimpleName();

    // Unlock the device so that the tests can input keystrokes.
    keyguardLock = ((KeyguardManager) getContext().getSystemService(KEYGUARD_SERVICE)).newKeyguardLock(simpleName);

    // Wake up the screen.
    int levelAndFlags = android.os.PowerManager.FULL_WAKE_LOCK
                        | android.os.PowerManager.ACQUIRE_CAUSES_WAKEUP
                        | android.os.PowerManager.ON_AFTER_RELEASE;
    wakeLock = ((PowerManager) getContext().getSystemService(POWER_SERVICE)).newWakeLock(levelAndFlags, simpleName);
}

@Override
public void onStart() {
    runOnMainSync(new Runnable() {
        @Override
        public void run() {
            keyguardLock.disableKeyguard();
            wakeLock.acquire();
        }
    });

    super.onStart();
}

1 个答案:

答案 0 :(得分:0)

这是AndroidJUnitRunner中的一个错误。 https://code.google.com/p/android/issues/detail?id=213677

解决方法是不在API 19之前的设备上使用测试大小标志。

在api&lt; 19

上崩溃
./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.size=small

适用于所有设备

./gradlew connectedAndroidTest