2014-03-28 25 views
27

com.android.dex.DexException:多DEX文件定義Lorg/hamcrest /說明Android的搖籃DexException:多DEX文件定義Lorg/hamcrest /說明

發生,同時試圖做任何調試構建/測試通過Android Studio或通過Gradle命令行在我的應用程序上。

發佈版本(沒有測試)工作正常,但只要包含測試(hamcrest是一個測試庫),構建將失敗並出現上述錯誤。

我檢查了我的模塊依賴關係,並沒有重複的要求gradle -q依賴關係 corroborates。


項目settings.gradle

include ':[library module]' 
include ':[main module]' 

項目的build.gradle

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:0.9.+' 
     classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+' 
    } 
} 

allprojects { 
    repositories { 
     mavenCentral() 
    } 
} 

[庫模塊]的build.gradle

apply plugin: 'android-library' 

android { 
    compileSdkVersion 19 
    buildToolsVersion "19.0.0" 

    defaultConfig { 
     minSdkVersion 14 
     targetSdkVersion 19 
    } 

    packagingOptions { 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'META-INF/NOTICE.txt' 
    } 
} 

dependencies { 
    compile 'com.google.zxing:core:3.0.+' 
    compile 'com.bugsnag:bugsnag-android:2.1.1+' 
} 

[主模塊]的build.gradle

apply plugin: 'android' 

android { 
    signingConfigs { 
    release { 
     [...] 
    } 
} 

    sourceSets { 
     main { 
      manifest.srcFile 'src/main/AndroidManifest.xml' 
      res.srcDirs = ['src/main/res'] 
     } 
     androidTest { 
      setRoot('src/test') 
     } 
     instrumentTest { 
     } 
    } 

    compileSdkVersion 19 
    buildToolsVersion '19.0.0' 

    defaultConfig { 
     minSdkVersion 14 
     targetSdkVersion 19 
     testPackageName "[main.packageName].tests" 
    } 

    buildTypes { 
     release { 
      runProguard false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' 
     } 
    } 

    packagingOptions { 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'META-INF/NOTICE.txt' 
    } 
} 

apply plugin: 'android-test' 



androidTest { 
    // configure the set of classes for JUnit tests 
    include '**/*Test.class' 

    // configure max heap size of the test JVM 
    maxHeapSize = "2048m" 
} 

repositories { 
    maven { url 'https://repo.commonsware.com.s3.amazonaws.com' } 
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' } 
} 

dependencies { 
    androidTestCompile 'junit:junit:4.10' 
    androidTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT' 
    androidTestCompile 'com.squareup:fest-android:1.0.+' 
    compile project(':[library module]') 
    compile 'com.github.gabrielemariotti.changeloglib:library:1.4.+' 
    compile 'com.google.code.gson:gson:2.2.4' 
    compile 'com.google.android.gms:play-services:+' 
    compile 'com.android.support:appcompat-v7:+' 
    compile ('de.keyboardsurfer.android.widget:crouton:1.8.+') { 
     exclude group: 'com.google.android', module: 'support-v4' 
    } 
    compile files('libs/CWAC-LoaderEx.jar') 
    compile 'com.squareup.okhttp:okhttp:1.5.+' 
    compile 'com.octo.android.robospice:robospice:1.4.11' 
    compile 'com.octo.android.robospice:robospice-cache:1.4.11' 
    compile 'com.octo.android.robospice:robospice-retrofit:1.4.11' 
    compile 'com.commonsware.cwac:security:0.1.+' 
    compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:+' 
} 
+0

有構建工具,你可以嘗試升級到(buildToolsVersion「19.0.3」)較新版本。保持最新對我來說似乎是一大幫助。 – AndroidGuy

回答

10

Robolectric 2.3取決於JUnit的4.8。 1(版本明確)。您正在導入JUnit 4.10(版本顯式)。 Hamcrest可能僅僅是dex令人窒息的許多重複項中的第一個 - 嘗試將您的JUnit需求版本更改爲4.8+(或將Robolectric從屬項排除JUnit)。

36

我通過在Android Studio中查找名爲'Description'的確切類來解決錯誤。原來,它出現在3個罐子裏。一個來自junit,一個來自直接依賴,另一個來自mockito。

enter image description here

事實證明的junit,代替正常的依賴性,包括在JUnit罐子Hamcrest類。

enter image description here

爲了能夠解決的問題包括的junit-DEP而不是JUnit的。

所以改變

androidTestCompile('的junit:junit的:4.8。+ ')

androidTestCompile(' 的junit:junit的-DEP:4.8 +')

的Mockito具有相同的問題/溶液:使用的Mockito-core.1.9 .jar而不是mockito-all.1.9.5.jar

+2

這對我使用powermock androidTestCompile('org.mockito:mockito-core:1.10.8'){ 排除組:'junit' 排除組:'org.hamcrest',模塊:'hamcrest-core' } androidTestCompile( 'org.powermock:powermock-API的Mockito:1.5.6'){ 排除組: '的junit' 排除組: 'org.mockito' }與所述檢索的類 – Maragues

+0

好主意在想法 – TechSpellBound

+0

+1太棒了。網絡上充斥着關於這個主題的食譜和解決方法。這是我發現 – slezica

23

我的項目依賴於json-simple version 1.1.1,由於某些原因,它對運行時間依賴於junit 4.1.0版本,該版本本身具有dependency on Hamcrest。如果我運行gradle dependencies或者通過檢查json-simple POM.xml,我可以看到這一點。

// compile - Classpath for compiling the main sources. 
    \--- com.googlecode.json-simple:json-simple:1.1.1 
     \--- junit:junit:4.10 
       \--- org.hamcrest:hamcrest-core:1.1 

剔除JSON-簡單 JUnit的神器讓我建立。

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile ('com.googlecode.json-simple:json-simple:1.1.1') { 
     exclude module: 'junit' 
    } 
} 
+1

我很難理解哪個庫存在衝突。這是json簡單的junit。謝謝。 – donnadulcinea

+0

同樣的問題在這裏 – BloodyD

0

排除模塊:junit

如果您正在使用json:simple依賴

+0

這和上面的答案是一樣的,但是2年後的細節不多。這就是那種得到低估的東西,但它已經處於最底層了。 – saywhatnow

+0

這是什麼解決了我的問題,我面臨了一個月,它爲我工作.. – user3820244

+0

我只是說,你應該只提供另一個答案,當你添加一個新的/不同的解決方案的主題。您的答案與已經提供的答案相同。 如果你看看以前的答案,他們提供了兩種不同的方法來解決這種情況。你的沒有。 – saywhatnow

相關問題