Android Gradle DexException:多个dex文件定义Lorg/hamcrest/Description
com.android.dex.DexException:多个dex文件定义Lorg/hamcrest/Description 在尝试通过我的应用程序上的Android Studio或Gradle命令行进行调试构建/测试时发生 发布版本(没有测试)工作正常,但一旦包含测试(Android Gradle DexException:多个dex文件定义Lorg/hamcrest/Description,android,gradle,android-studio,android-gradle-plugin,Android,Gradle,Android Studio,Android Gradle Plugin,com.android.dex.DexException:多个dex文件定义Lorg/hamcrest/Description 在尝试通过我的应用程序上的Android Studio或Gradle命令行进行调试构建/测试时发生 发布版本(没有测试)工作正常,但一旦包含测试(hamcrest是一个测试库),版本就会失败,并出现上述错误 我已经检查了我的模块依赖关系,没有重复的需求,gradle-q依赖关系证实了这一点 项目设置。渐变 项目构建。梯度 [library module]build
hamcrest
是一个测试库),版本就会失败,并出现上述错误
我已经检查了我的模块依赖关系,没有重复的需求,gradle-q依赖关系证实了这一点
项目设置。渐变
项目构建。梯度
[library module]build.gradle
[主模块]build.gradle
Robolectric2.3依赖于JUnit4.8.1(显式版本)。您正在导入JUnit4.10(显式版本)。Hamcrest可能只是dex被扼杀的众多重复版本中的第一个——尝试将JUnit需求版本更改为4.8+(或者将JUnit从Robolectric依赖项中排除)。我通过在Android Studio中查找名为“Description”的确切类来解决这个错误。结果发现它装在3个罐子里。一个来自junit,一个来自直接依赖,一个来自mockito 事实证明,junit在junit jar中包含了Hamcrest类,而不是普通的依赖项 为了能够解决这个问题,可以使用junit dep而不是junit 所以改变 androidTestCompile('junit:junit:4.8.+')) 到 androidTestCompile('junit:junit dep:4.8.+'))
Mockito也有同样的问题/解决方案:使用Mockito-core.1.9.5.jar而不是Mockito-all.1.9.5.jar我的项目依赖于,出于某种原因,它在运行时依赖于junit版本4.1.0,而junit版本本身有一个。如果我运行
gradle dependencies
或者通过检查
从json simple中排除junit工件允许我构建
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ('com.googlecode.json-simple:json-simple:1.1.1') {
exclude module: 'junit'
}
}
排除模块:
junit
如果您使用的是
json:simple
dependency,那么您可以尝试升级到更新版本的构建工具(buildToolsVersion“19.0.3”)。保持当前状态似乎对我有很大帮助。这对我使用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'}在idea+1中搜索类是个好主意。在这个主题上,网络上充满了食谱和解决方法。这是我发现的第一条可靠的诊断建议。我花了很长时间试图理解哪个库是冲突的。这是json简单junit。谢谢。这与上面的答案相同,但2年后细节较少。这是一种获得否决票的东西,但已经排在了最后。我想说的是,当你为主题添加新的/不同的解决方案时,你应该只提供另一个答案。你的答案与已经提供的答案相同。如果你看以前的答案,他们提供了两个不同的答案nt的方法来解决这个问题。你的没有。
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()
}
}
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+'
}
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:+'
}
// 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
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile ('com.googlecode.json-simple:json-simple:1.1.1') {
exclude module: 'junit'
}
}