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

时间:2014-07-11 08:00:34

标签: unit-testing android-studio android-gradle

试图在android studio 0.8.1中运行android测试

我可以正确运行assembleDebug和assembleTest。但是当我尝试运行android测试时,它会调用assembleDebug和assembleDebugTest,而后者则会遇到问题'多个dex文件定义'

几张照片:

enter image description here enter image description here enter image description here

和项目的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
dependencies {
    compile ('com.android.support:appcompat-v7:19.1.+')
    compile ('com.nineoldandroids:library:2.4.0')
    compile (group: 'com.google.guava', name: 'guava', version: '16.0-rc1')
}

他们都分享其余的

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 个答案:

答案 0 :(得分:1)

到目前为止,我尝试了两种似乎有效的解决方案,但我很确定必须有更好的方法。

无论如何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之前删除它们,它很慢而且很笨拙,让libs被创建,删除它们并再次创建它们

解决方案2:

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

任何两次