2015-10-14 24 views
6

在我的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" ...../> 

我已經擴展「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 

在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測試。我無法理解哪個依賴關係搞亂了代碼。除此之外,代碼的隨機失敗是懷疑的。請幫忙。

+1

我的團隊成員對espresso測試和multidex有類似的評論。更多的是,它沒有確定有測試運行multidex使能... – OceanLife

+0

@OceanLife你有沒有找到任何解決方案? – whitepearl

+0

不,我們還沒有。沒有multidex,它是可靠的,因此我建議我們編譯出一些分析庫,這些庫是作爲臨時解決方案來擴充的......只是爲了回到不需要multidex。你的錯誤信息(意外的impl)讓我想起了Java的SDK不兼容性錯誤,所謂的「VerifyError」(s)...在旅途中得到一些proguarding去除龐大的位... – OceanLife

回答

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 

只好執行命令後,等待幾秒鐘。使用多分辨率的儀器測試可以順利運行。

+0

謝謝你 - 在這裏工作的很好。希望谷歌能儘快解決這個問題。 – x2on

0

如果您使用的是1.4.0-beta3以上的gradle插件。 Multi-Dex支持已添加到gradle插件,這意味着和multidex-instrumentation依賴關係已包含在內,您不必明確指定它們。不幸的是,它似乎是前棒棒糖設備上的錯誤,看起來像MultiDexApplication的不同版本用於目標和測試應用程序。其結果是儀器無法運行,並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 

解決辦法是使用的gradle 1.3.1插件,並且要小心明確指定和multidex-instrumentation(如果你還需要這)相同版本的依賴關係。您也可能想要使用AndroidJUnitRunner,因爲它具有內置的multi-dex支持。

隨意明星相關的問題:https://code.google.com/p/android/issues/detail?id=194609

1

對於gradle這個插件1.5.0,你可以在你的編譯使用此解決方法。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") 
      } 
     } 
    } 
} 
+0

對我來說,它仍然不起作用,這段代碼會進入我自己項目的build.gradle中? –

+0

是的只是將它添加到您的build.gradle,它應該工作。也許你也有其他的依賴問題。 – x2on