在启用多索引的情况下,仪器测试随机失败

whi*_*arl 6 android gradle robotium android-instrumentation android-multidex

在我的Android应用程序中,我启用了多索引.该应用程序在模拟器上运行良好.我正在使用robotium来测试应用程序.但是当我执行检测测试用例时,有时测试会通过,但大多数情况下它们在系统重启后也会失败.通过和失败之间没有代码更改.

默认gradle配置:

android {
        defaultConfig {
        applicationId "com.example.androidapp"
        minSdkVersion 16
        targetSdkVersion 23
        multiDexEnabled true
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
        testProguardFile "proguard-test.txt"
    }
}
Run Code Online (Sandbox Code Playgroud)

还要为测试添加依赖项:

androidTestCompile fileTree(dir: 'libs', include:'robotium-solo-5.3.0.jar') 

androidTestCompile ('com.android.support:multidex-instrumentation:1.0.1') {
         exclude group: 'com.android.support', module: 'multidex' }
Run Code Online (Sandbox Code Playgroud)

在AndroidManifest.xml中,我提到了应用程序标记:

<application
        android:name="StartupActivity"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name" ...../>
Run Code Online (Sandbox Code Playgroud)

我在StartupActivity中扩展了"android.support.multidex.MultiDexApplication".仪器测试用例下降的时间我得到以下错误:

INSTRUMENTATION_RESULT: shortMsg=java.lang.IllegalAccessError
INSTRUMENTATION_RESULT: longMsg=java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
INSTRUMENTATION_CODE: 0
Run Code Online (Sandbox Code Playgroud)

logcat中的错误消息是:

W/dalvikvm? Class resolved by unexpected DEX: Lcom/example/androidapp/StartupActivity;(0xa695df08):0x9910e000 ref [Landroid/support/multidex/MultiDexApplication;] Landroid/support/multidex/MultiDexApplication;(0xa695df08):0x99a2c000
W/dalvikvm? (Lcom/example/androidapp/StartupActivity; had used a different Landroid/support/multidex/MultiDexApplication; during pre-verification)
W/dalvikvm? Unable to resolve superclass of Lcom/example/androidapp/StartupActivity; (540)
W/dalvikvm? Link of class 'Lcom/example/androidapp/StartupActivity;' failed
D/AndroidRuntime? Shutting down VM
W/dalvikvm? threadid=1: thread exiting with uncaught exception (group=0xa628c288)
Run Code Online (Sandbox Code Playgroud)

测试类看起来有点像:

public class HelloActivityTest extends ActivityInstrumentationTestCase2<HelloActivity> {
private Solo solo;
public HelloActivityTest() {
    super(HelloActivityTest.class);
}
  @Override
  public void setUp() throws Exception {
    setActivityInitialTouchMode(false);
    solo = new Solo(getInstrumentation(), getActivity());
  }

  public void test1() {}

  public void test2() {}

}
Run Code Online (Sandbox Code Playgroud)

我正在运行测试用例作为android测试.我无法理解哪个依赖项会弄乱代码.除此之外,代码的随机失败是持怀疑态度的.请帮忙.

whi*_*arl 5

找到了相同的解决方案,即设置dex验证和优化参数.您还可以将dalvik.vm.dexopt-flags设置为v = n,以使框架通过-Xverify:none -Xdexopt:验证以禁用验证.

执行:

adb shell setprop dalvik.vm.dexopt-flags v=n,o=v
adb shell stop installd
adb shell start installd
Run Code Online (Sandbox Code Playgroud)

执行命令后不得不等待几秒钟.具有多索引的仪表测试可以顺利运行.