有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android测试生成错误:多个dex文件定义L安卓/support/test/BuildConfig

我正在尝试将Espresso 2添加到我的项目中(该项目还具有许多其他依赖项),但在尝试运行测试时遇到了以下错误:

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

非测试构建工作正常

有没有人对调试这个的最佳方法有什么建议

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

<snip>

安卓TestCompile - Classpath for compiling the 安卓Test 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.安卓.robotium:robotium-solo:5.3.1
+--- com.安卓.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.安卓.support.test.espresso:espresso-idling-resource:2.0
|    +--- com.安卓.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.安卓.support.test:runner:0.2
     +--- junit:junit-dep:4.10 (*)
     +--- com.安卓.support.test:exposed-instrumentation-api-publish:0.2
     \--- com.安卓.support:support-annotations:22.0.0

<snip>

compile - Classpath for compiling the main sources.
+--- com.安卓.support:appcompat-v7:22.1.0
|    \--- com.安卓.support:support-v4:22.1.0
|         \--- com.安卓.support:support-annotations:22.1.0
+--- com.安卓.support:support-v4:22.1.0 (*)
+--- com.afollestad:material-dialogs:0.7.2.4
|    +--- com.安卓.support:support-v4:22.0.0 -> 22.1.0 (*)
|    +--- com.安卓.support:appcompat-v7:22.0.0 -> 22.1.0 (*)
|    +--- com.安卓.support:support-annotations:22.0.0 -> 22.1.0
|    \--- com.安卓.support:recyclerview-v7:22.0.0
|         +--- com.安卓.support:support-v4:22.0.0 -> 22.1.0 (*)
|         \--- com.安卓.support:support-annotations:22.0.0 -> 22.1.0
+--- com.google.安卓.gms:play-services-base:7.0.0
|    \--- com.安卓.support:support-v4:22.0.0 -> 22.1.0 (*)
+--- com.google.安卓.gms:play-services-gcm:7.0.0
|    \--- com.google.安卓.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:安卓-priority-jobqueue:1.1.2
|    \--- com.google.安卓:安卓: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-安卓-2.1_r1
|         +--- xerces:xmlParserAPIs:2.6.2
|         +--- xpp3:xpp3:1.1.4c
|         \--- org.json:json:20080701
+--- com.netflix.rxjava:rxjava-安卓:0.16.1
|    \--- com.netflix.rxjava:rxjava-core:0.16.1
+--- com.nineold安卓s:library:2.4.0
+--- it.sephiroth.安卓.library.horizontallistview:hlistview:1.2.2
|    \--- com.安卓.support:support-v4:19.1.+ -> 22.1.0 (*)
+--- com.joooonho:selectableroundedimageview:1.0.1
+--- com.crashlytics.sdk.安卓:crashlytics:2.0.1
|    +--- com.crashlytics.sdk.安卓:answers:1.0.1
|    |    \--- io.fabric.sdk.安卓:fabric:1.0.1
|    +--- io.fabric.sdk.安卓:fabric:1.0.1
|    \--- com.crashlytics.sdk.安卓:beta:1.0.1
|         \--- io.fabric.sdk.安卓:fabric:1.0.1
+--- com.mixpanel.安卓:mixpanel-安卓:4.5.3
\--- com.facebook.安卓:facebook-安卓-sdk:4.0.0
     +--- com.安卓.support:support-v4:[21,22) -> 22.1.0 (*)
     \--- com.parse.bolts:bolts-安卓:1.1.4

<snip>

我也尝试过搜索我的build/intermediates目录的子文件夹,但是我看不到任何冒烟的东西(尽管我不确定我是否以正确的方式搜索了所有外部依赖jar)

这是我构建的依赖项部分。格拉德尔:

dependencies {
    compile 'com.安卓.support:appcompat-v7:22.1.0'
    compile 'com.安卓.support:support-v4:22.1.0'
    compile 'com.afollestad:material-dialogs:0.7.2.4'
    compile 'com.google.安卓.gms:play-services-base:7.0.0'
    compile 'com.google.安卓.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:安卓-priority-jobqueue:1.1.2'
    compile 'com.netflix.rxjava:rxjava-安卓:0.16.1'
    compile 'com.nineold安卓s:library:2.4.0'
    compile 'it.sephiroth.安卓.library.horizontallistview:hlistview:1.2.2'
    compile 'com.joooonho:selectableroundedimageview:1.0.1'
    compile('com.crashlytics.sdk.安卓:crashlytics:2.0.1@aar') {
        transitive = true;
    }
    compile "com.mixpanel.安卓:mixpanel-安卓:4.5.3"
    compile "com.facebook.安卓:facebook-安卓-sdk:4.0.0"

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

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

更新:当我从com.安卓.tools.build:gradle:1.0.0更新到com.安卓.tools.build:gradle:1.1.1时,我收到以下警告:

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

共 (6) 个答案

  1. # 1 楼答案

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

    dexOptions {
        javaMaxHeapSize "2g"
        incremental true
    }
    

    这可以通过禁用预先退出库来解决

    dexOptions {
        javaMaxHeapSize "2g"
        incremental true
        preDexLibraries = false
    }
    
  2. # 2 楼答案

    更新(9/07/2015):

    如果使用以下排除项,则可以继续使用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,那么还需要排除

    更新(8/03/2015):

    在支持库22.2.1中,依赖关系再次被打破;在发布新的runner之前,请不要升级到22.2.1

    更新(6/04/2015):

    在最新版本的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撰写本文时)

    更新(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
    

    更新(4/24/2015):

    问题是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,因为该版本将告诉您确切的版本冲突,这在本例中非常有用

  3. # 3 楼答案

    另一个有用的技巧是如何强制依赖项解析到特定版本

    这里有一个方法:

    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部分

  4. # 4 楼答案

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

    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执行相同的操作

  5. # 5 楼答案

    我的解决方案:

    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'
       }
    }
    
  6. # 6 楼答案

    我在试着设置浓缩咖啡时也遇到了这个错误。试用 espresso-contrib:2.1,而不是{}