Unit testing Android studio,assembleDebugTest导致多个dex文件定义

Unit testing Android studio,assembleDebugTest导致多个dex文件定义,unit-testing,android-studio,android-gradle-plugin,Unit Testing,Android Studio,Android Gradle Plugin,正在尝试在android studio 0.8.1中运行android测试 我可以正确运行assembleDebug和assembleTest。但是,当我尝试运行android测试时,它称为assembleDebug和assembleDebug测试,而对于后者,我遇到了“多个dex文件定义”的问题 一些图片: 还有这个项目的build.gradle dependencies { compile project(':libraries:someLib') compile ('c

正在尝试在android studio 0.8.1中运行android测试

我可以正确运行assembleDebug和assembleTest。但是,当我尝试运行android测试时,它称为assembleDebug和assembleDebug测试,而对于后者,我遇到了“多个dex文件定义”的问题

一些图片:

还有这个项目的build.gradle

dependencies {
    compile project(':libraries:someLib')
    compile ('com.google.android.gms:play-services:5.+')
    compile ('fr.avianey:facebook-android-api:+@aar')
    compile ('com.fasterxml.jackson.core:jackson-databind:2.3.1')
    compile ('com.fasterxml.jackson.core:jackson-core:2.3.1')
    compile ('com.fasterxml.jackson.core:jackson-annotations:2.3.0')
    compile fileTree(dir: 'libs', include: '*.jar')

    //androidTestCompile 'junit:junit:4.10'
    //androidTestCompile 'org.robolectric:robolectric:2.1.+'
    //androidTestCompile 'com.squareup:fest-android:1.0.+'
    //androidTestCompile 'org.powermock:powermock-api-mockito:1.5.1'
}
还有“someLib”的build.gradle

其余的他们都分享

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
    }
}
apply plugin: 'com.android.library'

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 19
    }

    lintOptions {
        abortOnError false
    }
}

到目前为止,我尝试了两种似乎有效的解决方案,但我确信一定会有更好的解决方案

第1种解决方案:

tasks.whenTaskAdded { theTask ->
    if("assembleDebugTest".toString().equals(theTask.name.toString())) {
        def yourTaskName = "cleanLibs"
        project.task(yourTaskName) << {
            println "${project.buildDir}/intermediates/pre-dexed/test/debug/"
            delete fileTree(dir: ("${project.buildDir}/intermediates/pre-dexed/test/debug/"))
        }
        theTask.dependsOn(yourTaskName)
        def processTask = "preDexDebugTest"
        project.(yourTaskName.toString()).dependsOn(processTask)
        project.(processTask.toString()).dependsOn("compileDebugTestJava")
    }
}
只需跳过创建第一个LIB的任务,这比第一个要好得多

那么,对于这两种情况,你都必须扩展

android {

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'com/flurry/org/codehaus/jackson/map/VERSION.txt'
        exclude 'com/flurry/org/codehaus/jackson/impl/VERSION.txt'
        exclude 'com/flurry/org/apache/avro/data/Json.avsc'
        exclude 'META-INF/ASL2.0'
    }
}

无论什么是两次

我都面临着同样的问题,我应该把任务放在哪里。。。等感谢您构建的根源。gradle
tasks.whenTaskAdded { theTask ->
    if("assembleDebugTest".toString().equals(theTask.name.toString())) {
        def processTask = "preDexDebugTest"
        project.(processTask.toString()).enabled = false
    }
}
android {

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'com/flurry/org/codehaus/jackson/map/VERSION.txt'
        exclude 'com/flurry/org/codehaus/jackson/impl/VERSION.txt'
        exclude 'com/flurry/org/apache/avro/data/Json.avsc'
        exclude 'META-INF/ASL2.0'
    }
}