androidTestCompile在Android Studio 2.1.2中不工作

androidTestCompile在Android Studio 2.1.2中不工作,android,junit,android-instrumentation,Android,Junit,Android Instrumentation,我按照说明添加了插装测试,但不幸的是,Android Studio抱怨来自test runner包(com.Android.support.test:runner:0.5)或测试规则或浓缩咖啡核心(找不到符号)的类。如果我将依赖项类型从androidTestCompile更改为compile,错误就会消失。我创建了一个instrumentation运行配置,当前已选中该运行配置 编辑: 这是我们的Gradle构建文件: apply plugin: 'com.android.application

我按照说明添加了插装测试,但不幸的是,Android Studio抱怨来自test runner包(
com.Android.support.test:runner:0.5
)或测试规则或浓缩咖啡核心(
找不到符号
)的类。如果我将依赖项类型从
androidTestCompile
更改为
compile
,错误就会消失。我创建了一个instrumentation运行配置,当前已选中该运行配置

编辑: 这是我们的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工作室的特性,被称为一个特性,但我认为它是一个bug。 要使插装测试正常工作,您需要将构建变量设置为以下值:

Test Artifact: Android Instrumentation Tests
Build Variant: debug
有关更多详细信息,请参阅

我个人认为这毫无意义;这不像您使用的是
androidTestCompileDebug
,并且运行
gradle:dependencies
反复显示拉入androidTestCompile依赖项,而不管构建变量如何。但由于某些原因,它们只能在调试中解析


我希望这能有所帮助。

我自己花了大约一天的时间才弄明白。这是一个Android工作室的特性,被称为一个特性,但我认为它是一个bug。 要使插装测试正常工作,您需要将构建变量设置为以下值:

Test Artifact: Android Instrumentation Tests
Build Variant: debug
有关更多详细信息,请参阅

我个人认为这毫无意义;这不像您使用的是
androidTestCompileDebug
,并且运行
gradle:dependencies
反复显示拉入androidTestCompile依赖项,而不管构建变量如何。但由于某些原因,它们只能在调试中解析


我希望这能有所帮助。

也许这就是运行测试本身的问题所在。您是否尝试从控制台运行测试?它跑吗?如果您没有在Android Studio中正确设置插装测试,测试也不会从终端运行。它抱怨无法找到我的构建类型(in build.gradle)中使用的变量。下面是错误消息:Canno not find symbol BuildConfig.MyVarI使用此gradle命令从终端运行测试:gradle--offline AssembleAndRoidTest您可以发布build.gradle文件吗?也许这是gradle配置本身的问题?@Rafal添加了gradle文件,也许这就是运行测试本身的问题。您是否尝试从控制台运行测试?它跑吗?如果您没有在Android Studio中正确设置插装测试,测试也不会从终端运行。它抱怨无法找到我的构建类型(in build.gradle)中使用的变量。下面是错误消息:Canno not find symbol BuildConfig.MyVarI使用此gradle命令从终端运行测试:gradle--offline AssembleAndRoidTest您可以发布build.gradle文件吗?也许这是gradle配置本身的问题?@Rafal添加了gradle文件