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

旧时模样 提交于 2019-11-27 07:07:30
Yenchi

Update (9/07/2015):

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

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.

Update (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.

Update (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

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.0 as of this writing)

Update (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):

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

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

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'):

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:

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.

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.1 should work.

See the Forcing consistent version for a group of libraries section in the Gradle documentation for more information.

James P

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

dexOptions {
    javaMaxHeapSize "2g"
    incremental true
}

This was resolved by disabling predexing libraries.

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

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

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

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

According to this bug report, you can also use 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!