在我的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"
}
}
还为测试添加依赖项:
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" ...../>
我已经扩展了&#34; android.support.multidex.MultiDexApplication&#34;在StartupActivity中。仪器测试用例下降的时间我得到以下错误:
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
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)
测试类看起来有点像:
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测试。我无法理解哪个依赖项会弄乱代码。除此之外,代码的随机失败是持怀疑态度的。请帮忙。
答案 0 :(得分: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
执行命令后必须等待几秒钟。具有多索引的仪器测试可以顺利运行。
答案 1 :(得分:1)
对于gradle插件1.5.0,您可以在build.gradle中使用此解决方法:
// Workaround for Multidex bug in gradle-android-plugin
// Replace Multidex dependency with some dummy dependency to avoid dex problems
// @see https://code.google.com/p/android/issues/detail?id=194609
project.getConfigurations().all { config ->
if (config.name.contains("AndroidTest")) {
config.resolutionStrategy.eachDependency { DependencyResolveDetails details ->
if (details.requested.name == "multidex") {
details.useTarget("de.felixschulze.teamcity:teamcity-status-message-helper:1.2")
}
}
}
}
答案 2 :(得分:0)
如果您使用1.4.0-beta3以上的gradle插件。 Gradle插件中添加了Multi-Dex支持,这意味着已经包含multidex
和multidex-instrumentation
依赖项,您不必明确指定它们。不幸的是,它似乎是前Lollipop设备的错误,看起来不同版本的MultiDexApplication
用于目标和测试应用程序。因此,Instrumentation无法运行,logcat会为您提供类似的内容:
W/dalvikvm: Class resolved by unexpected DEX: Lcom/example/dexproof/App;(0x43893f90):0x64d46000 ref [Landroid/support/multidex/MultiDexApplication;] Landroid/support/multidex/MultiDexApplication;(0x43893f90):0x5de01000
W/dalvikvm: (Lcom/example/dexproof/App; had used a different Landroid/support/multidex/MultiDexApplication; during pre-verification)
W/dalvikvm: Unable to resolve superclass of Lcom/example/dexproof/App; (457)
W/dalvikvm: Link of class 'Lcom/example/dexproof/App;' failed
E/AndroidRuntime: java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
解决方案是使用1.3.1 gradle插件,并注意明确指定multidex
和multidex-instrumentation
(如果你还需要这个)相同版本的依赖项。您可能还想使用AndroidJUnitRunner
,因为它具有内置的多dex支持。
随意为相关问题加注:https://code.google.com/p/android/issues/detail?id=194609