Intereting Posts

Gradle dependencies: как выяснить, что вызывает это дублирование

У меня есть проект Android, который содержит приложение MultiDex .

Я пытаюсь запустить тесты Espresso, используя ./gradlew clean connectectedAndroidTest . В результате эта команда вызывает следующее сообщение об ошибке:

 :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 .

Из примечаний к выпуску Espresso 2.1:

«… test runner артефакт разбит на две части, а имя изменилось с com.android.support.test: testing-support-lib: от 0,1 до com.android.support.test: runner: 0.2 + com.android.support.test : правила: 0.2 …»

В ваших зависимостях вы смешиваете двух тестовых бегунов, включая старые теги-поддержки-lib и новые правила.

Таким образом, вы либо полностью переключаетесь на новый тестовый бегун, добавляя зависимость бегуна: 0.2 вместо старой поддержки-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' }