androidTestCompile 在 Android Studio 2.1.2 中不工作

androidTestCompile not working in Android Studio 2.1.2

我按照说明 here 添加了一个仪器测试,但不幸的是 Android Studio 抱怨 类 来自测试 运行ner 包(com.android.support.test:runner:0.5) 或测试规则或 espresso-core (cannot find symbol)。如果我将依赖类型从 androidTestCompile 更改为 compile,错误就会消失。我创建了一个检测 运行 配置,目前选择了 运行 配置。

编辑: 这是我们的 Gradle 构建文件:

apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'realm-android'
apply plugin: 'pmd'

buildscript {
    repositories {
        jcenter()
        maven { url 'https://maven.fabric.io/public' }
        mavenCentral()
        maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.google.gms:google-services:2.0.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        classpath 'me.tatarka:gradle-retrolambda:3.2.5'
        classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2'
        classpath 'io.realm:realm-gradle-plugin:1.0.0'
        classpath 'io.fabric.tools:gradle:1.21.6'
    }

    configurations.classpath.exclude group: 'com.android.tools.external.lombok'

}

repositories {
    jcenter()
    mavenCentral()
    maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    maven { url 'http://oss.jfrog.org/artifactory/oss-snapshot-local' }
    maven { url 'https://repo.commonsware.com.s3.amazonaws.com' }
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile 'com.android.support:multidex:1.0.1'
//    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:support-v13:24.0.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:percent:24.0.0'
    compile 'com.google.android.gms:play-services-auth:9.0.2'
    compile 'com.google.android.gms:play-services-gcm:9.0.2'
    compile "com.mixpanel.android:mixpanel-android:4.8.6"
    compile 'com.squareup.retrofit2:retrofit:2.0.1'
    compile 'com.squareup.retrofit2:converter-gson:2.0.1'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
    compile 'com.squareup.okhttp3:okhttp:3.2.0'
    compile 'com.squareup.okhttp3:okhttp-urlconnection:3.2.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.2.0'
    compile 'com.squareup:otto:1.3.8'
    compile 'com.squareup.dagger:dagger:1.2.2'
    apt 'com.squareup.dagger:dagger-compiler:1.2.2'
    compile('com.facebook.android:facebook-android-sdk:4.13.1') {
        exclude module: 'bolts-android'
        exclude module: 'bolts-applinks'
        exclude module: 'bolts-tasks'
    }
    compile 'com.facebook.fresco:fresco:0.11.0'
    compile 'me.relex:photodraweeview:1.0.0'
    compile 'com.jakewharton.rxrelay:rxrelay:1.0.0'
    compile 'com.jakewharton:butterknife:8.1.0'
    apt 'com.jakewharton:butterknife-compiler:8.1.0'
    compile 'com.jakewharton.timber:timber:4.0.1'
    compile 'javax.annotation:javax.annotation-api:1.2'
    compile 'com.cloudinary:cloudinary-android:1.2.2:'
    compile 'com.soundcloud.android:android-crop:1.0.1@aar'
    compile 'com.rockerhieu.emojicon:library:1.3.3'
    compile 'io.reactivex:rxandroid:1.2.0'
    compile 'com.trello:rxlifecycle:0.5.0'
    compile 'com.trello:rxlifecycle-components:0.5.0'
    compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
    compile 'com.jakewharton.rxbinding:rxbinding-recyclerview-v7:0.4.0'
    compile 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'
    compile 'com.jakewharton.rxbinding:rxbinding-design:0.4.0'
    compile 'com.github.hotchemi:permissionsdispatcher:2.1.1'
    apt 'com.github.hotchemi:permissionsdispatcher-processor:2.1.1'
    compile 'net.sourceforge.streamsupport:streamsupport:1.4.3'
    compile('com.crashlytics.sdk.android:crashlytics:2.5.5@aar') {
        transitive = true;
    }

    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
    androidTestCompile 'junit:junit:4.12'
}

android {
    // General configuration goes here
    compileSdkVersion 23
    buildToolsVersion '23.0.3'


    defaultConfig {
        def buildNumber = computeVersionCode()
        applicationId 'com.xxx.xxx'
        minSdkVersion 19
        targetSdkVersion 23
        versionCode = buildNumber
        versionName '0.2.6'
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true

        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }

    dexOptions {
        incremental true
        javaMaxHeapSize "6g"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    signingConfigs {
        debug {
            storeFile rootProject.file('debug.keystore')
            storePassword 'xxx'
            keyAlias 'xxx'
            keyPassword 'xxx'
        }

        beta {
            String password = getKeyStorePassword()
            println("password::$password")
            if (password) {
                storeFile file("beta_release.keystore")
                storePassword xxx
                keyAlias "xxx"
                keyPassword xxx
            } else {
                println "Environment variable BETA_KEYSTORE_PASSWORD needs to be set"
            }
        }
    }

    buildTypes {
        localDebug {
            minifyEnabled false
            debuggable true;
            signingConfig signingConfigs.debug
            buildConfigField "String", "BACKEND_ADDRESS", "\"http://xxx.xxx.dev/\""
            buildConfigField "boolean", "MYVAR2", "false"
            buildConfigField "String", "MYVAR3", "\"value\""
            resValue "string", "BUILD_TYPE_DISPLAY", "Debug(Local)"
            applicationIdSuffix ".debug"
        }
    }

    packagingOptions {
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }

    lintOptions {
        abortOnError true
    }

    splits {
        abi {
            enable true
            reset()
            include 'x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a', 'armeabi'
            universalApk false
        }
    }
}

task pmd(type: Pmd) {
    ruleSetFiles = files("${project.rootDir}/pmd-ruleset.xml")
    ignoreFailures = false
    ruleSets = []

    source 'src'
    include '**/*.java'
    exclude '**/gen/**'

    reports {
        xml.enabled = false
        html.enabled = true
        xml {
            destination "$project.buildDir/reports/pmd/pmd.xml"
        }
        html {
            destination "$project.buildDir/reports/pmd/pmd.html"
        }
    }
}

def computeVersionCode() {
    def buildNumber = System.getenv('BUILD_NUMBER')
    if (buildNumber == null || buildNumber.isEmpty()) {
        println "No BUILD_NUMBER in environment, using 1 as revision"
        return 1
    }
    buildNumber = buildNumber.toInteger()
    println "New revision is ${buildNumber}"
    return buildNumber
}

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-annotations:24.0.0'
        force 'com.android.support:support-v4:24.0.0'
        force 'com.android.support:appcompat-v7:24.0.0'
        force 'com.android.support:design:24.0.0'
        force 'com.android.support:recyclerview-v7:24.0.0'
    }
}

我自己花了大约一天的时间,终于弄明白了。这是一个 Android Studio 功能 - 称为功能,但我认为它是一个错误。

要使仪器测试正常工作,您需要将构建变体设置为以下内容:

Test Artifact: Android Instrumentation Tests
Build Variant: debug

有关详细信息,请参阅 here

我个人认为是无意义的;这不像您正在使用 androidTestCompileDebug,并且 运行 gradle <app_name>:dependencies 反复显示拉入 androidTestCompile 依赖项,无论构建变体如何。但出于某种原因,它们只能在调试中解决。

希望对您有所帮助。