Gradle依赖项:如何找出导致此重复的原因

时间:2015-05-28 13:26:44

标签: android junit gradle android-gradle android-espresso

我有一个包含MultiDex应用程序的Android项目。

我正在尝试使用./gradlew clean connectectedAndroidTest运行Espresso测试。此命令最终会导致出现此错误消息:

:app:dexDebugAndroidTest
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/test/InstrumentationRegistry;

我的顶级build.gradle包含:

classpath 'com.android.tools.build:gradle:1.2.3'

我通过包装器使用Gradle 2.4。

我的项目build.gradle看起来像这样:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
//        classpath 'me.tatarka:gradle-retrolambda:3.1.0'
    }
}

apply plugin: 'android-sdk-manager'
apply plugin: 'com.android.application'
//apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'org.robolectric'

android {
    compileSdkVersion 21
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.something"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        multiDexEnabled true
        testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
    }

    dexOptions {
        javaMaxHeapSize "2g"
        preDexLibraries = false
    }

    buildTypes {
        debug {
            versionNameSuffix '-DEBUG'

            minifyEnabled false
            shrinkResources = false
            zipAlignEnabled = false
        }

        release {
            minifyEnabled false

            shrinkResources = true
            zipAlignEnabled = true
        }
    }

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'    }
}

dependencies {
    //compile 'com.google.dexmaker:dexmaker:1.0'
    compile 'com.android.support:multidex:1.0.1'

    // Auth0
    compile 'com.auth0.android:lock:1.7.0'
    compile 'com.auth0.android:lock-googleplus:1.7.0'

    compile 'com.android.support:appcompat-v7:22.1.1'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.squareup.picasso:picasso:2.1.1'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.google.android.gms:play-services-plus:7.3.0'
    compile 'io.reactivex:rxandroid:0.24.0'
    compile 'com.amazonaws:aws-android-sdk-cognito:2.2.1'
    compile 'com.amazonaws:aws-android-sdk-core:2.2.1'
    compile 'com.amazonaws:aws-android-sdk-mobileanalytics:2.2.1'

    // Robolectric
    //testCompile 'junit:junit:4.12'
    testCompile "org.robolectric:robolectric:3.0-rc3"
    testCompile "org.robolectric:shadows-multidex:3.0-rc3" // otherwise robolectric tests fail on MultiDex.install

    androidTestCompile ('com.android.support.test:rules:0.2') {
        exclude module: 'support-v4'
        exclude module: 'support-annotations'
    }

    androidTestCompile ('com.android.support.test:testing-support-lib:0.1') {
        exclude group: 'junit'
    }

    androidTestCompile ('com.android.support.test.espresso:espresso-core:2.0') {
        exclude module: 'testing-support-lib'
        exclude module: 'support-v4'
        exclude module: 'support-annotations'
        exclude group: 'javax.inject'
    }

}

robolectric {
    // Configure includes / excludes
    include '**/*Test.class'
    exclude '**/espresso/**/*.class'

    // Configure max heap size of the test JVM
    maxHeapSize = '2048m'

    // Configure the test JVM arguments - Does not apply to Java 8
    //jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'

    // Specify max number of processes (default is 1)
    maxParallelForks = 4

    // Specify max number of test classes to execute in a test process
    // before restarting the process (default is unlimited)
    forkEvery = 150

    // configure whether failing tests should fail the build
    ignoreFailures true

    // use afterTest to listen to the test execution results
    afterTest { descriptor, result ->
        println "Executing test for ${descriptor.name} with result: ${result.resultType}"
    }
}

现在我一直试图找出正在发生的事情,例如通过使用./gradlew -q app:dependencies查看依赖关系(导致其他内部配置):

androidJacocoAgent - The Jacoco agent to use to get coverage data.
\--- org.jacoco:org.jacoco.agent:0.7.4.201502262128

androidJacocoAnt - The Jacoco ant tasks to use to get execute Gradle tasks.
\--- org.jacoco:org.jacoco.ant:0.7.4.201502262128
     +--- org.jacoco:org.jacoco.core:0.7.4.201502262128
     |    \--- org.ow2.asm:asm-debug-all:5.0.1
     +--- org.jacoco:org.jacoco.report:0.7.4.201502262128
     |    +--- org.jacoco:org.jacoco.core:0.7.4.201502262128 (*)
     |    \--- org.ow2.asm:asm-debug-all:5.0.1
     \--- org.jacoco:org.jacoco.agent:0.7.4.201502262128


androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.android.support.test:rules:0.2
|    \--- com.android.support.test:runner:0.2
|         +--- junit:junit-dep:4.10
|         |    \--- org.hamcrest:hamcrest-core:1.1
|         \--- com.android.support.test:exposed-instrumentation-api-publish:0.2
+--- com.android.support.test:testing-support-lib:0.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
     +--- com.android.support.test.espresso:espresso-idling-resource:2.0
     +--- com.google.code.findbugs:jsr305:2.0.1
     +--- javax.annotation:javax.annotation-api:1.2
     \--- org.hamcrest:hamcrest-core:1.1


compile - Classpath for compiling the main sources.
+--- com.android.support:multidex:1.0.1
+--- com.auth0.android:lock:1.7.0
|    +--- com.android.support:appcompat-v7:22.0.0 -> 22.1.1
|    |    \--- com.android.support:support-v4:22.1.1
|    |         \--- com.android.support:support-annotations:22.1.1
|    +--- com.squareup:otto:1.3.5
|    +--- com.auth0.android:identity-core:1.7.0
|    |    +--- com.hannesdorfmann.smoothprogressbar:library:1.0.0
|    |    +--- com.android.support:appcompat-v7:21.0.2 -> 22.1.1 (*)
|    |    \--- com.auth0.android:core:1.7.0
|    |         +--- com.fasterxml.jackson.core:jackson-annotations:2.4.1
|    |         +--- com.fasterxml.jackson.core:jackson-core:2.4.1
|    |         +--- com.loopj.android:android-async-http:1.4.5
|    |         \--- com.fasterxml.jackson.core:jackson-databind:2.4.1
|    |              +--- com.fasterxml.jackson.core:jackson-annotations:2.4.0 -> 2.4.1
|    |              \--- com.fasterxml.jackson.core:jackson-core:2.4.1
|    \--- com.auth0.android:core:1.7.0 (*)
+--- com.auth0.android:lock-googleplus:1.7.0
|    +--- com.android.support:appcompat-v7:21.0.2 -> 22.1.1 (*)
|    +--- com.auth0.android:identity-core:1.7.0 (*)
|    \--- com.google.android.gms:play-services-plus:6.5.87 -> 7.3.0
|         \--- com.google.android.gms:play-services-base:7.3.0
|              \--- com.android.support:support-v4:22.0.0 -> 22.1.1 (*)
+--- com.android.support:appcompat-v7:22.1.1 (*)
+--- com.google.code.gson:gson:2.3.1
+--- com.squareup.picasso:picasso:2.1.1
+--- com.squareup.retrofit:retrofit:1.9.0
|    \--- com.google.code.gson:gson:2.3.1
+--- com.squareup.okhttp:okhttp:2.4.0
|    \--- com.squareup.okio:okio:1.4.0
+--- com.google.android.gms:play-services-plus:7.3.0 (*)
+--- io.reactivex:rxandroid:0.24.0
|    \--- io.reactivex:rxjava:1.0.4
+--- com.amazonaws:aws-android-sdk-cognito:2.2.1
|    \--- com.amazonaws:aws-android-sdk-core:2.2.1
|         +--- commons-logging:commons-logging:1.1.1
|         \--- com.google.code.gson:gson:2.2.4 -> 2.3.1
+--- com.amazonaws:aws-android-sdk-core:2.2.1 (*)
\--- com.amazonaws:aws-android-sdk-mobileanalytics:2.2.1
     \--- com.amazonaws:aws-android-sdk-core:2.2.1 (*)


testCompile - Classpath for compiling the test sources.
+--- org.robolectric:robolectric:3.0-rc3
|    +--- junit:junit:4.8.2
|    +--- org.robolectric:robolectric-annotations:3.0-rc3
|    +--- org.robolectric:robolectric-resources:3.0-rc3
|    |    +--- org.robolectric:robolectric-utils:3.0-rc3
|    |    |    +--- org.robolectric:robolectric-annotations:3.0-rc3
|    |    |    \--- com.google.android.apps.common.testing.accessibility.framework:accessibility-test-framework:1.0
|    |    +--- org.robolectric:robolectric-annotations:3.0-rc3
|    |    \--- com.ximpleware:vtd-xml:2.11
|    +--- org.robolectric:robolectric-utils:3.0-rc3 (*)
|    +--- org.robolectric:shadows-core:3.0-rc3
|    |    +--- org.robolectric:robolectric-annotations:3.0-rc3
|    |    +--- org.robolectric:robolectric-utils:3.0-rc3 (*)
|    |    +--- org.robolectric:robolectric-resources:3.0-rc3 (*)
|    |    +--- com.ibm.icu:icu4j:53.1
|    |    \--- com.almworks.sqlite4java:sqlite4java:0.282
|    +--- org.bouncycastle:bcprov-jdk16:1.46
|    +--- org.ow2.asm:asm:5.0.1
|    +--- org.ow2.asm:asm-commons:5.0.1
|    |    \--- org.ow2.asm:asm-tree:5.0.1
|    |         \--- org.ow2.asm:asm:5.0.1
|    +--- org.ow2.asm:asm-util:5.0.1
|    |    \--- org.ow2.asm:asm-tree:5.0.1 (*)
|    +--- org.ow2.asm:asm-analysis:5.0.1
|    |    \--- org.ow2.asm:asm-tree:5.0.1 (*)
|    +--- com.ximpleware:vtd-xml:2.11
|    +--- org.apache.maven:maven-ant-tasks:2.1.3
|    \--- org.apache.ant:ant:1.8.0
|         \--- org.apache.ant:ant-launcher:1.8.0
\--- org.robolectric:shadows-multidex:3.0-rc3
     \--- org.robolectric:robolectric:3.0-rc3 (*)

现在我的问题是,如何找出造成重复的原因?根据我的理解,它应该是一些库,其中包括另一个定义InstrumentationRegistry的库。

1 个答案:

答案 0 :(得分:4)

来自Espresso 2.1 release notes

" ...将测试运行者工件拆分为两个,并将名称从com.android.support.test更改为:testing-support-lib:0.1到com.android.support.test:runner: 0.2 + com.android.support.test:规则:0.2 ......"

在您的依赖项中,您通过包含旧的testing-support-lib和新规则来混合两个测试运行器。

所以你要么通过添加一个runner:0.2依赖而不是旧的support-lib或者选择不使用规则来完全切换到新的测试运行器。

如果你想保留规则,你的androidTest依赖关系将如下所示:

androidTestCompile('com.android.support.test:rules:0.2') {
    exclude module: 'support-v4'
    exclude module: 'support-annotations'
}

androidTestCompile('com.android.support.test:runner:0.2') {
    exclude module: 'support-annotations'
}

androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
    exclude module: 'testing-support-lib'
    exclude module: 'support-v4'
    exclude module: 'support-annotations'
    exclude group: 'javax.inject'
}