在我的Android应用中,开启了multidex。该应用在模拟器上运行良好。我正在使用Robotium来测试应用程序。但是,在执行仪器测试用例时,有时测试会通过,但大多数情况下在系统重新启动后也会失败。在测试通过和失败之间没有代码更改。
默认的Gradle配置:
在AndroidManifest.xml中,我将应用程序标签声明为:
日志中的错误信息为:
测试类看起来有些像:
默认的Gradle配置:
android {
defaultConfig {
applicationId "com.example.androidapp"
minSdkVersion 16
targetSdkVersion 23
multiDexEnabled true
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
testProguardFile "proguard-test.txt"
}
}
另外还需要为测试添加依赖:
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' }
在AndroidManifest.xml中,我将应用程序标签声明为:
<application
android:name="StartupActivity"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" ...../>
我在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
日志中的错误信息为:
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)
测试类看起来有些像:
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() {}
}
我正在运行一个Android测试用例。我无法理解哪个依赖项搞乱了代码。此外,代码的随机失败令人怀疑。请帮忙。