2017-04-27 38 views
1

Gradle 3.3,WireMock 2.6.0。 由Gradle構建我的Android項目。我寫了Espresso,Mockito的單元測試,並且一切正常。Android:WireMock:com.android.dex.DexIndexOverflowException:方法ID不在[0,0xffff]:65536

這裏我的build.gradle:

dependencies { 
    // for folder "androidTest" 
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' 
    androidTestCompile "org.mockito:mockito-android:2.7.21" 
    androidTestCompile "org.powermock:powermock-api-mockito:1.6.6" 
    androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6' 

    // for folder "test" 
    testCompile 'junit:junit:4.12' 
    testCompile 'org.powermock:powermock-api-mockito:1.6.6' 
    testCompile 'org.powermock:powermock-module-junit4:1.6.6' 
    } 

我的項目是成功構建和單元測試成功運行。好。 現在我想添加WireMock單元測試。所以,我改變我的build.gradle(如顯示此博客:http://handstandsam.com/2016/01/30/running-wiremock-on-android/):

dependencies { 
// for folder "androidTest" 
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' 
androidTestCompile "org.mockito:mockito-android:2.7.21" 
androidTestCompile "org.powermock:powermock-api-mockito:1.6.6" 
androidTestCompile 'org.powermock:powermock-module-junit4:1.6.6' 

//WireMock 
androidTestCompile("com.github.tomakehurst:wiremock:2.6.0") { 
    //Using Android Version Instead 
    exclude group: 'org.apache.httpcomponents', module: 'httpclient' 

    //Version conflict with our app's slf4j version 
    exclude group: 'org.slf4j', module: 'slf4j-api' 

    //Was getting a classpath conflict for org.objectweb.asm.AnnotationVisitor which is a part of 'net.minidev:asm' 
    exclude group: 'org.ow2.asm', module: 'asm' 

    //Was getting this warning, so decided to ignore this version included by WireMock. 
    //Warning:Dependency org.json:json:20090211 is ignored as it may be conflicting with the internal version provided by Android. 
    //In case of problem, please repackage with jarjar to change the class packages 
    exclude group: 'org.json', module: 'json' 
} 

// for folder "test" 
testCompile 'junit:junit:4.12' 
testCompile 'org.powermock:powermock-api-mockito:1.6.6' 
testCompile 'org.powermock:powermock-module-junit4:1.6.6' 
} 

當我運行我的應用程序一切正常。但是,當我嘗試啓動Android的我插樁的單元測試,我得到了一個錯誤:

:app:compileDevAndroidTestShaders 
:app:generateDevAndroidTestAssets 
:app:mergeDevAndroidTestAssets 
:app:transformClassesWithDexForDevAndroidTest FAILED 

FAILURE: Build failed with an exception. 

What went wrong: 
Execution failed for task ':app:transformClassesWithDexForDevAndroidTest'. 
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]: 65536 
Try: 
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. 
BUILD FAILED 

回答

0

當訪問以大於64K(敏捷方法計數限制)的索引方法的DexIndexOverFlowException發生。這意味着android項目中實際的方法計數(引用)數量大於64K,應用程序的最終.dex文件將被拆分爲多個dex文件。在這種情況下,你應該讓你的應用程序支持multidex,以下列方式:

1-添加multidex依賴性:

compile 'com.android.support:multidex:1.0.1' 

2-以下配置添加到應用程序/的build.gradle:

android { 

     defaultConfig { 

      multidexEnabled true 
     } 
    } 

3-創建自定義應用程序的類來擴展MultiDexApplication

public MyApplication extends MultiDexApplication 

4-將此自定義類作爲應用程序的入口點,在AndroidManifest.xml

<application 
    android:name=".MyApplication" 
相關問題