java Android 测试构建错误:多个 dex 文件定义了 Landroid/support/test/BuildConfig

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29857695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:01:32  来源:igfitidea点击:

Android tests build error: Multiple dex files define Landroid/support/test/BuildConfig

javaandroidandroid-gradle-pluginbuild.gradleandroid-espresso

提问by Dan J

I'm trying to add Espresso 2 to my project (which also has lots of other dependencies), but I'm hitting this error when trying to run tests:

我正在尝试将 Espresso 2 添加到我的项目中(它还有很多其他依赖项),但是在尝试运行测试时遇到了这个错误:

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/test/BuildConfig;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
    ...

The non-test build works fine.

非测试构建工作正常。

Does anybody have any tips on the best way to debug this?

有人对调试此问题的最佳方法有任何提示吗?

I've tried running ./gradlew -q :<my_project>:dependencies, but I can't see any obvious problems (though I don't know much about interpreting its output):

我试过运行./gradlew -q :<my_project>:dependencies,但我看不到任何明显的问题(虽然我不太了解解释其输出):

<snip>

androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.squareup.spoon:spoon-client:1.1.2
+--- com.google.dexmaker:dexmaker-mockito:1.0
|    +--- com.google.dexmaker:dexmaker:1.0
|    \--- org.mockito:mockito-core:1.9.5
|         +--- org.hamcrest:hamcrest-core:1.1
|         \--- org.objenesis:objenesis:1.0
+--- com.google.dexmaker:dexmaker:1.0
+--- org.mockito:mockito-core:1.9.5 (*)
+--- com.jayway.android.robotium:robotium-solo:5.3.1
+--- com.android.support.test.espresso:espresso-core:2.0
|    +--- com.squareup:javawriter:2.1.1
|    +--- org.hamcrest:hamcrest-integration:1.1
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- org.hamcrest:hamcrest-library:1.1
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- javax.inject:javax.inject:1
|    +--- com.android.support.test.espresso:espresso-idling-resource:2.0
|    +--- com.android.support.test:testing-support-lib:0.1
|    |    \--- junit:junit-dep:4.10
|    |         \--- org.hamcrest:hamcrest-core:1.1
|    +--- com.google.code.findbugs:jsr305:2.0.1
|    +--- javax.annotation:javax.annotation-api:1.2
|    \--- org.hamcrest:hamcrest-core:1.1
\--- com.android.support.test:runner:0.2
     +--- junit:junit-dep:4.10 (*)
     +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
     \--- com.android.support:support-annotations:22.0.0

<snip>

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.1.0
|    \--- com.android.support:support-v4:22.1.0
|         \--- com.android.support:support-annotations:22.1.0
+--- com.android.support:support-v4:22.1.0 (*)
+--- com.afollestad:material-dialogs:0.7.2.4
|    +--- com.android.support:support-v4:22.0.0 -> 22.1.0 (*)
|    +--- com.android.support:appcompat-v7:22.0.0 -> 22.1.0 (*)
|    +--- com.android.support:support-annotations:22.0.0 -> 22.1.0
|    \--- com.android.support:recyclerview-v7:22.0.0
|         +--- com.android.support:support-v4:22.0.0 -> 22.1.0 (*)
|         \--- com.android.support:support-annotations:22.0.0 -> 22.1.0
+--- com.google.android.gms:play-services-base:7.0.0
|    \--- com.android.support:support-v4:22.0.0 -> 22.1.0 (*)
+--- com.google.android.gms:play-services-gcm:7.0.0
|    \--- com.google.android.gms:play-services-base:7.0.0 (*)
+--- de.greenrobot:greendao:1.3.7
+--- de.greenrobot:eventbus:2.2.0
+--- com.squareup.wire:wire-runtime:1.4.0
|    \--- com.squareup.okio:okio:0.6.0 -> 1.0.1
+--- com.squareup.okio:okio:1.0.1
+--- com.squareup.okhttp:okhttp-urlconnection:2.0.0
|    \--- com.squareup.okhttp:okhttp:2.0.0
|         \--- com.squareup.okio:okio:1.0.0 -> 1.0.1
+--- com.squareup.retrofit:retrofit:1.6.0
|    \--- com.google.code.gson:gson:2.2.4
+--- com.squareup.retrofit:converter-wire:1.6.0
|    +--- com.squareup.retrofit:retrofit:1.6.0 (*)
|    \--- com.squareup.wire:wire-runtime:1.2.0 -> 1.4.0 (*)
+--- com.squareup.okhttp:okhttp:2.0.0 (*)
+--- com.squareup.picasso:picasso:2.4.0
+--- com.path:android-priority-jobqueue:1.1.2
|    \--- com.google.android:android:2.2.1
|         +--- commons-logging:commons-logging:1.1.1
|         +--- org.apache.httpcomponents:httpclient:4.0.1
|         |    +--- org.apache.httpcomponents:httpcore:4.0.1
|         |    +--- commons-logging:commons-logging:1.1.1
|         |    \--- commons-codec:commons-codec:1.3
|         +--- org.khronos:opengl-api:gl1.1-android-2.1_r1
|         +--- xerces:xmlParserAPIs:2.6.2
|         +--- xpp3:xpp3:1.1.4c
|         \--- org.json:json:20080701
+--- com.netflix.rxjava:rxjava-android:0.16.1
|    \--- com.netflix.rxjava:rxjava-core:0.16.1
+--- com.nineoldandroids:library:2.4.0
+--- it.sephiroth.android.library.horizontallistview:hlistview:1.2.2
|    \--- com.android.support:support-v4:19.1.+ -> 22.1.0 (*)
+--- com.joooonho:selectableroundedimageview:1.0.1
+--- com.crashlytics.sdk.android:crashlytics:2.0.1
|    +--- com.crashlytics.sdk.android:answers:1.0.1
|    |    \--- io.fabric.sdk.android:fabric:1.0.1
|    +--- io.fabric.sdk.android:fabric:1.0.1
|    \--- com.crashlytics.sdk.android:beta:1.0.1
|         \--- io.fabric.sdk.android:fabric:1.0.1
+--- com.mixpanel.android:mixpanel-android:4.5.3
\--- com.facebook.android:facebook-android-sdk:4.0.0
     +--- com.android.support:support-v4:[21,22) -> 22.1.0 (*)
     \--- com.parse.bolts:bolts-android:1.1.4

<snip>

I've also tried searching subfolders of my build/intermediatesdirectory, but I couldn't see any smoking guns (though I'm not sure I was searching all the external dependency JARs in the right way).

我也试过搜索我的build/intermediates目录的子文件夹,但我看不到任何确凿的证据(虽然我不确定我是否以正确的方式搜索所有外部依赖 JAR)。

Here is the dependency section of my build.gradle:

这是我的 build.gradle 的依赖部分:

dependencies {
    compile 'com.android.support:appcompat-v7:22.1.0'
    compile 'com.android.support:support-v4:22.1.0'
    compile 'com.afollestad:material-dialogs:0.7.2.4'
    compile 'com.google.android.gms:play-services-base:7.0.0'
    compile 'com.google.android.gms:play-services-gcm:7.0.0'
    compile 'de.greenrobot:greendao:1.3.7'
    compile 'de.greenrobot:eventbus:2.2.0'
    compile 'com.squareup.wire:wire-runtime:1.4.0'
    compile 'com.squareup.okio:okio:1.0.1'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.retrofit:retrofit:1.6.0'
    compile 'com.squareup.retrofit:converter-wire:1.6.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.squareup.picasso:picasso:2.4.0'
    compile 'com.path:android-priority-jobqueue:1.1.2'
    compile 'com.netflix.rxjava:rxjava-android:0.16.1'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'it.sephiroth.android.library.horizontallistview:hlistview:1.2.2'
    compile 'com.joooonho:selectableroundedimageview:1.0.1'
    compile('com.crashlytics.sdk.android:crashlytics:2.0.1@aar') {
        transitive = true;
    }
    compile "com.mixpanel.android:mixpanel-android:4.5.3"
    compile "com.facebook.android:facebook-android-sdk:4.0.0"

    // For the Rollbar JAR, as it is not available in Maven central
    compile fileTree(dir: 'libs', include: '*.jar')

    androidTestCompile 'com.squareup.spoon:spoon-client:1.1.2'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'
    androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.3.1'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:runner:0.2'
}

Update:when I update from com.android.tools.build:gradle:1.0.0to com.android.tools.build:gradle:1.1.1, I get this warning:

更新:当我从com.android.tools.build:gradle:1.0.0to更新com.android.tools.build:gradle:1.1.1,我收到此警告:

Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (22.1.0) and test app (22.0.0) differ.

回答by Yenchi

Update (9/07/2015):

更新 (9/07/2015):

You can continue to work with 22.2.1 if you use the following excludes:

如果您使用以下排除项,您可以继续使用 22.2.1:

androidTestCompile ('com.android.support.test.espresso:espresso-core:2.2') {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test:runner:0.3') {
    exclude group: 'com.android.support', module: 'support-annotations'
}
androidTestCompile ('com.android.support.test:rules:0.3') {
    exclude group: 'com.android.support', module: 'support-annotations'
}

If you depend on espresso-contrib, you need the exclude as well.

如果您依赖espresso-contrib,则还需要排除。

Update (8/03/2015):

更新 (8/03/2015):

With support library 22.2.1, the dependencies are broken again; please don't upgrade to 22.2.1 until a new runner is released.

使用支持库 22.2.1,依赖项再次被破坏;在新的运行程序发布之前,请不要升级到 22.2.1。

Update (6/04/2015):

更新 (6/04/2015):

With the latest release of runner 0.3 and rules 0.3, this answer is no longer needed. You can simply use

随着最新版本的 runner 0.3 和规则 0.3,不再需要这个答案。你可以简单地使用

androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'

with latest support libraries. (22.2.0as of this writing)

具有最新的支持库。(22.2.0截至撰写本文时)

Update (5/30/2015):

更新 (5/30/2015):

compile 'com.android.support:appcompat-v7:22.2.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'
// com.android.support.test:testing-support-lib:0.1 // <-- causes issue

Update (4/24/2015):

更新 (4/24/2015):

The problem is that com.android.support:support-v4:22.1.1is clashing with com.android.support.test:runner:0.2(as that depends on com.android.support:support-v4:22.0.0).

问题是这com.android.support:support-v4:22.1.1com.android.support.test:runner:0.2(因为这取决于com.android.support:support-v4:22.0.0)发生冲突。

com.android.support.test.espresso:espresso-core:2.1has a dependency on com.android.support.test:runner:0.2, so it also causes the same error.

com.android.support.test.espresso:espresso-core:2.1依赖于com.android.support.test:runner:0.2,因此它也会导致相同的错误。

So, this combination will work:

因此,这种组合将起作用:

compile 'com.android.support:support-v4:22.0.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'

...and so will this one (without'com.android.support.test:runner:0.2'):

......这个(没有'com.android.support.test:runner:0.2')也是如此:

compile 'com.android.support:support-v4:22.1.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

Original Answer:

原答案:

Contrary to what the Espresso documentation says, you should remove this dependency:

Espresso 文档所说的相反,您应该删除此依赖项:

androidTestCompile 'com.android.support.test:runner:0.2'

androidTestCompile 'com.android.support.test:runner:0.2'

As it is the cause for library version conflict.

因为它是库版本冲突的原因。

You should also update to Android gradle plugin 1.1.1, as that version will tell you the exact version conflict, which is useful in this case.

您还应该更新到 Android gradle 插件 1.1.1,因为该版本会告诉您确切的版本冲突,这在这种情况下很有用。

回答by Dan J

One other useful tip is how to force dependency resolution to a specific version.

另一个有用的技巧是如何强制对特定版本进行依赖解析。

Here is one way:

这是一种方法:

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:22.0.0'
}

...and here is another:

...这是另一个:

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        if (details.requested.group == 'com.android.support') {
            details.useVersion '22.0.0'
        }
    }
}

Using either of these with com.android.support.test.espresso:espresso-core:2.1should work.

使用其中任何一个都com.android.support.test.espresso:espresso-core:2.1应该可以工作。

See the Forcing consistent version for a group of librariessection in the Gradle documentationfor more information.

有关更多信息,请参阅Gradle 文档中Forcing consistent version for a group of libraries部分。

回答by James P

I recently ran into this error after we enabled incremental gradle builds.

我最近在启用增量 gradle 构建后遇到了这个错误。

dexOptions {
    javaMaxHeapSize "2g"
    incremental true
}

This was resolved by disabling predexing libraries.

这是通过禁用预索引库解决的。

dexOptions {
    javaMaxHeapSize "2g"
    incremental true
    preDexLibraries = false
}

回答by Max Kammerer

My solution:

我的解决方案:

compile 'com.android.support:appcompat-v7:22.1.0'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.1'
androidTestCompile 'com.android.support:support-annotations:22.1.0'
androidTestCompile 'com.android.support.test:runner:0.2'

+

+

android {
    packagingOptions {
        exclude 'LICENSE.txt'
   }
}

回答by c2knaps

I got this error trying to set up Espresso as well. Try using espresso-contrib:2.1, not 2.0

我在尝试设置 Espresso 时也遇到了这个错误。尝试使用 espresso-contrib:2.1,而不是2.0

回答by Code-Apprentice

According to this bug report, you can also use resolutionStrategy:

根据此错误报告,您还可以使用resolutionStrategy

allprojects {
    repositories {
        mavenCentral()
        maven {
            url 'http://download.crashlytics.com/maven'
        }
    }
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:22.2.0'
    }
}

This is the solution that worked for me and allowed me to use the most recent version of appcompat-v7 and appcompat-v4.

这是对我有用的解决方案,并允许我使用最新版本的 appcompat-v7 和 appcompat-v4。

回答by Eric

Try excluding the following from espresso (one at a time):

尝试从浓缩咖啡中排除以下内容(一次一个):

androidTestCompile('com.android.support.test.espresso:espresso-core:2.1') {
  exclude group: 'javax.inject'
  exclude group: 'com.google.code.findbugs'
  exclude group: 'com.android.support', module: 'support-annotations'
}

Probably need to do the same with runner

可能需要做同样的事情 runner