2014-10-09 36 views
2

我的build.gradle:不能編譯測試與Robolectric

buildscript { 
    repositories { 
     mavenCentral() 
    } 

    dependencies { 
     classpath 'com.stanfy.spoon:spoon-gradle-plugin:0.10.0' 
    } 
} 


apply plugin: 'android-sdk-manager' 
apply plugin: 'android' 
apply plugin: 'spoon' 
apply plugin: 'robolectric' 

android { 
    compileSdkVersion 19 
    buildToolsVersion '20.0.0' 

    defaultConfig { 
     minSdkVersion 14 
     targetSdkVersion 19 
     testInstrumentationRunner 'com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner' 
    } 


    buildTypes { 
     debug { 
     } 
     release { 
     } 
    } 

    productFlavors { 
     first { 
     //just version codes and packages 
     } 
     second { 
     //just version codes and packages 
     } 
     third { 
     //just version codes and packages 
     } 
    } 
} 

spoon { 
    debug = true 
} 

dependencies { 
    compile project(':app:libs:facebookSDK') 
    compile 'com.google.android.gms:play-services:5.0.89' 
    compile 'com.android.support:support-v4:20.0.0+' 
    compile 'com.google.code.gson:gson:2.2.4' 
    compile fileTree(dir: 'libs', include: '*.jar') 
    androidTestCompile fileTree(dir: 'libsTest', include: '*.jar') 
    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.0' 
    androidTestCompile 'junit:junit:4.+' 
    androidTestCompile 'org.robolectric:robolectric:2.3' 
} 

libsTest:濃咖啡的contrib-1.1-bundled.jar現在

,我的錯誤是以下幾點:

Error Code: 
    2 
    Output: 
    UNEXPECTED TOP-LEVEL EXCEPTION: 
    com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/SelfDescribing; 
     at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594) 
     at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552) 
     at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533) 
     at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170) 
     at com.android.dx.merge.DexMerger.merge(DexMerger.java:188) 
     at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439) 
     at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287) 
     at com.android.dx.command.dexer.Main.run(Main.java:230) 
     at com.android.dx.command.dexer.Main.main(Main.java:199) 
     at com.android.dx.command.Main.main(Main.java:103) 

此處更詳細的日誌:

http://pastebin.com/Yye3cd1c

如何解決此問題?

UPD:我現在的問題基本上是如何狩獵這些重複的部分?

+0

一般來說,這意味着這個類出現在構建路徑的多個位置(儘管它在junit的dependency子句中被明確排除)。我無法在我的設置中重現此問題;你可以在你的libsTest和libs文件夾中包含更多關於所有庫的信息嗎?這可能是罪魁禍首。 – 2014-10-09 17:52:47

+0

@ScottBarta我已經更新了我的問題。 – 2014-10-10 09:26:28

+0

仍然不爲我重現。你將不得不四處尋找,找到重複該類的地方。 – 2014-10-10 15:51:32

回答

0

我有一個類似的問題,前一段時間,如果我沒有記錯,我通過添加排除語句如下固定它:

androidTestCompile('junit:junit:4.+') { 
    exclude module: 'hamcrest-core' 
} 
0

deckard-gradle sample project啓發,我建議嘗試這樣的事:

androidTestCompile 'org.hamcrest:hamcrest-integration:1.1' 
androidTestCompile 'org.hamcrest:hamcrest-core:1.1' 
androidTestCompile 'org.hamcrest:hamcrest-library:1.1' 

androidTestCompile('junit:junit:4.11') { 
    exclude module: 'hamcrest-core' 
} 
androidTestCompile('org.robolectric:robolectric:2.3') { 
    exclude module: 'classworlds' 
    exclude module: 'commons-logging' 
    exclude module: 'httpclient' 
    exclude module: 'maven-artifact' 
    exclude module: 'maven-artifact-manager' 
    exclude module: 'maven-error-diagnostics' 
    exclude module: 'maven-model' 
    exclude module: 'maven-project' 
    exclude module: 'maven-settings' 
    exclude module: 'plexus-container-default' 
    exclude module: 'plexus-interpolation' 
    exclude module: 'plexus-utils' 
    exclude module: 'wagon-file' 
    exclude module: 'wagon-http-lightweight' 
    exclude module: 'wagon-provider-api' 
} 
1

My question now is basically how do I hunt those duplicated parts?

您使用日誌來做到這一點。您粘貼的日誌說:

com.android.dex.DexException: Multiple dex files define

這是最有可能是由於衝突的庫。日誌繼續:

Lorg/hamcrest/SelfDescribing;

這裏是矛盾的圖書館,hamcrest

在向項目添加依賴項後,如果存在庫(或庫)使用的公共子庫,則會發生此錯誤。所以它看起來像hamcrest不僅被你的一個庫使用。

我們將通過檢查依賴性來了解這個衝突。當然,有效的檢測同時需要既直觀又合理。

讓我們從hamcrest本身開始。 (我假設你以前沒有聽說過Hamcrest。)

讓我們來看看Hamcrest project page。 Hamcrest將自己定義爲用於構建測試表達式的匹配器庫。定義說典型場景包括測試框架,嘲諷庫 ...這是照亮,因爲你有像JUnit,Espresso和Robolectric這樣的測試的一些依賴關係。

現在我們應該繼續JUnit dependencies。看起來像JUnit使用Hamcrest核心Here是我們的第一個作爲子依賴的hamcrest。

讓我們繼續使用Espresso,因爲您在libsTest文件夾中有espresso-contrib-1.1-bundled.jar。 當我們檢查espresso-contrib項目dependencies時,我們可以看到Espresso大量使用了hamcrest。

我們可能會在您的項目中找到衝突的庫,最後一步是從我們的某個依賴項中排除hamcrest-core,同時添加此依賴項。你可以做到這一點:

androidTestCompile('junit:junit:4.+') { 
    exclude module: 'hamcrest-core' 
} 
0

一招追捕這些錯誤是使用Android Studio的「導航>類...」選項。

鑑於你有這樣的錯誤:

Lorg/hamcrest/SelfDescribing; 

,那麼你可以搜索「org.hamcrest.SelfDescribing」這樣你就可以使用它的.jar文件的想法。