几天以来,我一直在为我们的MultiDex-Application中的NoClassDefFoundError和AndroidJUnitRunner奋斗...

因为我们要开始使用Espresso进行点击测试,所以我们将TestRunner从“ MultiDexTestRunner”更改为“ AndroidJUnitRunner”。通过此更改,我们在API 19之下的设备上遇到了一些问题。当执行@SmallTests时,我们得到的响应是NoClassDefFoundError的原因“未执行测试”

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

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) 


这是我们的WakefulTestRunner所需的部分:

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();
}


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

configurations {
// This dependency set is for annotation processors.
// They're added below directly to the java compiler.
apt
androidTestCompile.exclude group: 'com.android.support', module: 'support-v4'
androidTestCompile.exclude group: 'com.android.support', module: 'recyclerview-v7'

androidTestCompile.exclude group: 'com.android.support', module: 'support-v13'
androidTestCompile.exclude group: 'com.android.support', module: 'appcompat-v7'
androidTestCompile.exclude group: 'com.android.support', module: 'cardview-v7'
}

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 23
    versionCode versionMajor * 10000 + versionMinor * 100 + versionMicro
    versionName "${versionMajor}.${versionMinor}.${versionMicro}"
    multiDexEnabled true

    //intended to fix install issue -505 (https://code.google.com/p/android/issues/detail?id=189079)
    applicationId = "com.xxxxxxxx"

    testInstrumentationRunner "com.xxxxxxxxx.test.WakefulTestRunner"
}

最佳答案

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

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

api

./gradlew connectedAndroidTest -Pandroid.testInstrumentationRunnerArguments.size=small


适用于所有设备

./gradlew connectedAndroidTest

10-08 18:36