Android Instrumented Tests上的JaCoCo失败,“代理未启动”

时间:2018-11-02 09:17:50

标签: android android-gradle android-emulator jacoco

我们有一个包含一些应用程序和库的多模块项目,并使用JaCoCo涵盖了android上的单元测试和仪表测试。因此,我们将标准的jacoco插件用于gradle,并将android插件用于gradle。我们使用的JaCoCo目标是createStandardDebugCoverageReport

但是,我们看到一些既无法解释也无法更改的行为: 在这两个模块上都可以运行测试并通过测试,但是在一个模块上可以创建覆盖率报告并从仿真器中下载。在另一个模块上,我们在测试通过后看到错误消息:

  

原因:java.lang.IllegalStateException:JaCoCo代理未启动。

在logcat上,因此创建了一个零字节的文件。

我们使用ExampleUnitTestExampleInstrumentedTest类的两个类似的应用程序对其进行了测试。

整个JaCoCo配置在项目的根目录中完成。

项目的根目录:

import org.ajoberstar.grgit.Grgit

buildscript {
    ext {
        jacocoVersion = "0.8.1"
    }
    repositories {
        maven {
            url "our.artifactory.url"
            credentials {
                username System.getenv("ARTIFACTORY_LOCAL_USERNAME") ? System.getenv("ARTIFACTORY_LOCAL_USERNAME") : project.property("ARTIFACTORY_LOCAL_USERNAME")
                password System.getenv("ARTIFACTORY_LOCAL_PASSWORD") ? System.getenv("ARTIFACTORY_LOCAL_PASSWORD") : project.property("ARTIFACTORY_LOCAL_PASSWORD")
            }
        }
        // The used repositories need to be configured within artifactory because we use it as dependency cache.
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0-alpha13'
        classpath 'com.google.gms:google-services:3.2.0'
        classpath 'io.fabric.tools:gradle:1.26.1'
        classpath 'org.jfrog.buildinfo:build-info-extractor-gradle:4.7.5'
        classpath 'org.ajoberstar:grgit:2.2.1'
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.2"

        classpath "org.jacoco:org.jacoco.core:${jacocoVersion}"
        classpath "org.jacoco:org.jacoco.report:${jacocoVersion}"
        classpath "org.jacoco:org.jacoco.agent:${jacocoVersion}"

        classpath "org.codehaus.groovy:groovy-all:2.4.15"
    }
}

plugins {
    id "org.sonarqube" version "2.6.2"
}

// This block encapsulates custom properties and makes them available to all
// modules in the project.
ext {
    versionCode = 110
    versionName = "0.17.3"

    currentBranchName = System.getenv("BRANCH")

    def gitDirectory = file(file('.').parentFile.absolutePath + File.separator + '.git')
    if (currentBranchName.equals("unknown") && gitDirectory.exists()) {
        git = Grgit.open(dir: gitDirectory.parentFile)
        currentBranchName = git.branch.current.name
    }

    artifactoryUser = System.getenv("ARTIFACTORY_LOCAL_USERNAME")
    artifactoryPassword = System.getenv("ARTIFACTORY_LOCAL_PASSWORD")

    minSdkVersion = 23
    compileSdkVersion = 28
    targetSdkVersion = 28
    supportLibVersion = "28.0.0"
    playServicesVersion = "16.2.0"
    jacocoVersion = "0.8.1"
}

allprojects {
    apply plugin: "com.jfrog.artifactory"
    apply plugin: 'maven-publish'

    repositories {
        maven {
            url "our.artifactory.url"
            credentials {
                username System.getenv("ARTIFACTORY_LOCAL_USERNAME") ? System.getenv("ARTIFACTORY_LOCAL_USERNAME") : project.property("ARTIFACTORY_LOCAL_USERNAME")
                password System.getenv("ARTIFACTORY_LOCAL_PASSWORD") ? System.getenv("ARTIFACTORY_LOCAL_PASSWORD") : project.property("ARTIFACTORY_LOCAL_PASSWORD")
            }
        }
        // The used repositories need to be configured within artifactory because we use it as dependency cache.
    }

// This is the only place of any JaCoCo configuration
    apply plugin: 'jacoco'

    tasks.withType(Test) {
        jacoco.includeNoLocationClasses = true
    }
}

以下是磨损应用程序,工作示例:

apply plugin: 'com.android.application'

configurations {
    demoDebugCompile
    demoReleaseCompile
    standardDebugCompile
    standardReleaseCompile
}

android {
    signingConfigs {
        ourSigningConfig {
            keyAlias getEnvProperty("SIGNING_KEY_ALIAS")
            keyPassword getEnvProperty("SIGNING_KEY_PASSWORD")
            storeFile file(getEnvProperty("SIGNING_STORE_FILE"))
            storePassword getEnvProperty("SIGNING_STORE_PASSWORD")
        }
    }

    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "our.working.applicationid"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.ourSigningConfig
        }

        debug {
            debuggable true
            minifyEnabled false
            applicationIdSuffix ".debug"
            versionNameSuffix " Debug"
            testCoverageEnabled true
        }
    }

    flavorDimensions "mode"

    productFlavors {
        demo {
            dimension "mode"
            applicationIdSuffix ".demo"
            versionNameSuffix " Demo"
            buildConfigField "boolean", "DEMO", "true"
        }

        standard {
            dimension "mode"
            buildConfigField "boolean", "DEMO", "false"
        }
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = false
        }

        unitTests.all {
            jvmArgs '-noverify'
            // https://stackoverflow.com/questions/32315978/jvm-options-in-android-when-run-gradlew-test/37593189#37593189
        }
    }

    lintOptions {
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    // External libraries
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    // Support library
    implementation 'com.google.android.support:wearable:2.1.0'
    compileOnly 'com.google.android.wearable:wearable:2.1.0'

    // Google Play services
    implementation "com.google.android.gms:play-services-wearable:15.0.1" // TODO: use ${rootProject.ext.supportLibVersion}

    // Authenticator
    implementation project(':authenticator')

    // Testing
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.robolectric:robolectric:4.0-beta-1'

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "${rootProject.ext.supportLibVersion}"
            }
        }
    }
}

artifactoryPublish.skip = true

以下是虚拟应用程序。这给了我们agent not started exception。 我们比较了模块gradle文件,并使用了工作gradle文件的依赖项对此功能进行了增强。

apply plugin: 'com.android.application'

configurations {
    demoDebugCompile
    demoReleaseCompile
    standardDebugCompile
    standardReleaseCompile
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "our.nonworking.applicationid"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }

    buildTypes {
        release {
            debuggable false
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            consumerProguardFiles 'proguard-rules.pro'
        }
        debug {
            debuggable true
            minifyEnabled false
            applicationIdSuffix ".debug"
            versionNameSuffix " Debug"
            testCoverageEnabled true
        }
    }

    flavorDimensions "mode"
    productFlavors {
        demo {
            dimension "mode"
            applicationIdSuffix ".demo"
            versionNameSuffix " Demo"
            buildConfigField "boolean", "DEMO", "true"
        }
        standard {
            dimension "mode"
            buildConfigField "boolean", "DEMO", "false"
        }
    }

    testOptions {
        unitTests {
            includeAndroidResources = true
            returnDefaultValues = false
        }
        unitTests.all {
            jvmArgs '-noverify'
            // https://stackoverflow.com/questions/32315978/jvm-options-in-android-when-run-gradlew-test/37593189#37593189
        }
    }

    lintOptions {
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')

    implementation 'com.android.support.constraint:constraint-layout:1.1.3'

    // Support library
    implementation 'com.google.android.support:wearable:2.1.0'
    compileOnly 'com.google.android.wearable:wearable:2.1.0'

    // Google Play services
    implementation "com.google.android.gms:play-services-wearable:15.0.1" // TODO: use ${rootProject.ext.supportLibVersion}

    // Authenticator
    implementation project(':authenticator')

    // Testing
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.robolectric:robolectric:4.0-beta-1'

    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion "${rootProject.ext.supportLibVersion}"
            }
        }
    }
}

artifactoryPublish.skip = true

我们现在没主意了。对此有什么影响?我们在项目中的某些库上看到了相同的行为。 经过测试的仿真器是Pixel XL API 28。 您有什么建议,建议或提示吗?

谢谢。

2 个答案:

答案 0 :(得分:0)

从插件3.0.0开始,现在您不能使用android块DSL配置Jacoco,而必须在classpath依赖项中与Android插件一起定义jacoco版本。 因此,添加以下内容:

buildscript {
  repositories {
    google()
    jcenter()
  }
  dependencies {
    // Android plugin 
    classpath 'com.android.tools.build:gradle:3.0.1'
    //Jacoco version
    classpath 'org.jacoco:org.jacoco.core:0.8.1'
  }
}

希望这会有所帮助

答案 1 :(得分:0)

我找到了一个解决方案,但不幸的是我完全不明白:

我通过将测试配置提取到一个额外的gradle文件中来统一测试配置,因此关于测试的模块gradle配置之间应该没有区别。

那没有帮助,但是改善了我的体系结构。

如果我将检测到的测试添加到有这些问题的模块中,它将正常工作,并且将为所有模块生成覆盖率报告。 我不明白的是,所有这些模块都有一个ExampleInstrumentedTest通过。 但是,两个模块确实只有ExampleInstrumentedTest,并且它会生成覆盖率报告。它们都是 android应用程序。但是,我有一个问题,就是也无法用另一个(非常简单的)应用程序生成一份覆盖率报告。 这就是为什么我没有尝试首先将其他已测试的测试添加到失败的模块中的原因。

所以,它现在对我有用。 但是,如果有人可以提示它为什么起作用并且使用ExampleInstrumentedTest无效,将不胜感激。