web-dev-qa-db-ja.com

Androidテストビルドエラー:複数のdexファイルがLandroid / support / test / BuildConfigを定義しています

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

pdate: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.
20
Dan J

アップデート(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日):

ランナー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'

最新のサポートライブラリ。 (これを書いている時点での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.1com.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'

元の回答:

Espressoのドキュメントによると とは異なり、この依存関係を削除する必要があります。

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

ライブラリバージョンの競合の原因です。

また、Android gradle plugin 1.1.1に更新する必要があります。そのバージョンは、正確なバージョンの競合を通知するため、この場合に役立ちます。

46
Yenchi

もう1つの便利なヒントは、依存関係の解決を特定のバージョンに強制する方法です。

ここに1つの方法があります:

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で使用すると機能するはずです。

詳細については、 GradleのドキュメントForcing consistent version for a group of librariesセクションを参照してください。

6
Dan J

増分gradleビルドを有効にした後、私は最近このエラーに遭遇しました。

dexOptions {
    javaMaxHeapSize "2g"
    incremental true
}

これは、プレデキシングライブラリを無効にすることで解決しました。

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

私の解決策:

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'
   }
}
3
Max Kammerer

Espressoをセットアップしようとしても、このエラーが発生しました。 espresso-contrib:2.1ではなく2.0を使用してみてください

1
c2knaps

以下をエスプレッソから除外してみてください(一度に1つ)。

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でも同じことをする必要があります

0
Eric

このバグレポート によると、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の最新バージョンを使用できるようにしました。

0
Code-Apprentice