【问题标题】:Android tests build error: Multiple dex files define Landroid/support/test/BuildConfigAndroid测试构建错误:多个dex文件定义Landroid/support/test/BuildConfig
【发布时间】:2015-04-24 21:20:03
【问题描述】:

我正在尝试将 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)
    ...

非测试版本运行良好。

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

我试过运行./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>

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

这是我的 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'
}

更新:当我从 com.android.tools.build:gradle:1.0.0 更新到 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.

【问题讨论】:

  • 发布您的build.gradle.
  • 你还记得你在错误之前添加的最后一个依赖吗?你有这么多。
  • 我们也不知道compile fileTree(dir: 'libs', include: '*.jar')compile project(':CookbriteJava')compile project(':camera')是什么。
  • 我认为 Gradle dependencies 任务的输出显示了您需要的一切?我也会用我的 build.gradle 更新问题。
  • 我最近在 Espresso 列表中添加了最后两个,为了克服一个非常相似的错误,我必须更新我的支持库和 Google Play 服务依赖项,并删除 AssertJ。

标签: java android android-gradle-plugin build.gradle android-espresso


【解决方案1】:

更新(2015 年 9 月 7 日):

如果您使用以下排除项,您可以继续使用 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'
}

如果你依赖espresso-contrib,你也需要排除。

更新(2015 年 8 月 3 日):

使用支持库 22.2.1,依赖关系再次被打破;在发布新的跑步者之前,请不要升级到 22.2.1。

更新(2015 年 6 月 4 日):

使用最新版本的 runner 0.3 和 rules 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'

带有最新的支持库。 (22.2.0 在撰写本文时)

更新(2015 年 5 月 30 日):

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

更新(2015 年 4 月 24 日):

问题在于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.1 依赖于com.android.support.test:runner:0.2,所以也会导致同样的错误。

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

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

...这个也是如此(没有 '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'

原答案:

what the Espresso documentation says相反,你应该删除这个依赖:

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

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

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

【讨论】:

  • 为什么 espresso-coreespresso-contrib
  • @IgorGanapolsky espresso-contrib 2.1 曾经包含测试运行程序 0.2 jar 文件,该文件也与支持库 22.1 存在兼容性问题。关于何时应该使用 espresso-contrib 的更一般性讨论,请检查您是否需要使用 developer.android.com/reference/android/support/test/espresso/… 中列出的任何类
  • 删除 com.android.support.test:testing-support-lib:0.1 依赖解决了我的问题。谢谢!
  • 有这方面的消息吗? compile 'com.android.support:support-annotations:22.2.1' 也坏了:-/
  • 我在使用espresso-contrib:2.0 时仍然遇到这个问题。如何解决这个问题?
【解决方案2】:

另一个有用的提示是如何强制将依赖项解析到特定版本。

这是一种方法:

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

...这是另一个:

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

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

有关详细信息,请参阅 the Gradle documentation 中的 Forcing consistent version for a group of libraries 部分。

【讨论】:

    【解决方案3】:

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

    dexOptions {
        javaMaxHeapSize "2g"
        incremental true
    }
    

    已通过禁用预索引库解决此问题。

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

    【讨论】:

    • 使用preDexLibraries = false的缺点是什么?这对我有用,但我会遇到性能问题吗?
    【解决方案4】:

    我的解决方案:

    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'
       }
    }
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

      • espresso-contrib:2.1 包含对 android 支持库 22.0.0 的引用,在这种情况下与 22.1.1 冲突。
      • 并非如此,通常 Gradle 足够聪明,可以使用新版本。
      【解决方案6】:

      尝试从 espresso 中排除以下内容(一次一个):

      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'
      }
      

      可能需要对runner做同样的事情

      【讨论】:

        【解决方案7】:

        根据this bug report,也可以使用resolutionStrategy

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-06-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多