如何使用 Android gradle 插件 0.10.0 或更高版本获取 jacoco 覆盖率报告? [英] How do I get a jacoco coverage report using Android gradle plugin 0.10.0 or higher?

查看:28
本文介绍了如何使用 Android gradle 插件 0.10.0 或更高版本获取 jacoco 覆盖率报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Gradle Android 插件 0.10.2 获取测试覆盖率报告.但是在运行一些测试后我仍然无法获得覆盖率报告.(连接AndroidTest).

I'm trying to get a test coverage report using Gradle Android plugin 0.10.2. But I still can't get a coverage report after running some tests. (connectedAndroidTest).

我的主模块的 build.gradle 是:

my main module's build.gradle is :

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug {
            testCoverageEnabled true
        }

        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
}

和项目构建 gradle 的 buildscript 部分是:

and the buildscript section of project's build gradle is :

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.10.+'
    }
}

从终端运行 gradlew connectedAndroidTest 后,我可以在 coverage-instrumented-classescode-coverage 文件夹中找到 >build 文件夹.但是我在 reports 文件夹中找不到 coverage 文件夹.(只有我能看到 androidTests 文件夹)

Once I run a gradlew connectedAndroidTest from terminal, I can find coverage-instrumented-classes and code-coverage folder inside the build folder. But I can't find coverage folder in the reports folder. (Only I can see is androidTests folder)

获得 jacoco 覆盖率报告有什么遗漏吗?

Is there anything missing for getting a jacoco coverage report?

推荐答案

经过数百次搜索获得覆盖率报告的答案,我终于找到了我想要的确切答案.

Over the hundreds of times searching the answer to getting a coverage report, I finally found an exact answer what I want.

这篇博文,我发现gradlew createDebugCoverageReport 创建 jacoco 覆盖率报告.

From the this blog post, I found that gradlew createDebugCoverageReport creates the jacoco coverage report.

此外,来自 gradle 插件 源代码,插件默认使用jacoco 0.6.2.201302030002.(因此,如果您要使用默认版本,jacoco 版本定义不是必需的)

Also, from the gradle plugin source code, the plugin uses jacoco 0.6.2.201302030002 by default. (therefore, jacoco version definition is not required if you are going to use a default version)

总而言之,使用 Android gradle 插件获取 jacoco 覆盖率报告的基本步骤是:

In summary, the ESSENTIAL steps to get a jacoco coverage report with Android gradle plugin are:

  1. Android gradle 插件版本 0.10.0 或更高版本(通常在您项目的 build.gradle 中)
  2. testCoverageEnabled true 添加到您想要的构建类型(即 debug)
  3. 运行 $ gradlew createDebugCoverageReportgradlew connectedCheck 以获取 jacoco 覆盖率报告.
  1. Android gradle plugin version 0.10.0 or higher (typically in your project's build.gradle)
  2. add testCoverageEnabled true to the build type you want (i.e. debug)
  3. run $ gradlew createDebugCoverageReport or gradlew connectedCheck to get a jacoco coverage report.

您可以在 build/reports/coverage/{buildType} 中找到您的覆盖率报告.(即 build/reports/coverage/debug 用于调试构建)

You can find your coverage report at the build/reports/coverage/{buildType}. (i.e. build/reports/coverage/debug for debug build)

(从@odiggity 的评论中添加多口味案例)

如果您的项目使用多口味配置,请改用 create{flavorName}CoverageReport.覆盖率报告将在 build/reports/coverage/{flavorName}/{buildType} 生成.

If your project uses multi-flavor configuration, use create{flavorName}CoverageReport instead. The coverage report will be generated at build/reports/coverage/{flavorName}/{buildType}.

具有 debug 构建类型的 krInternal 风格示例:

Example for flavor krInternal with debug build type:

  • 命令:./gradlew createKrInternalDebugCoverageReport
  • 报告生成于:build/reports/coverage/krInternal/debug

由于您只能使用 emulator具有 root 权限的设备 获得覆盖率报告,因此在常规(非根)设备:

Since you can only get a coverage report with the emulator and device with root permission, you'll get following error after running a command on the regular(non-rooted) device:

05:48:33 E/Device: Error during Sync: Permission denied                         
java.io.IOException: com.android.ddmlib.SyncException: Permission denied
    at com.android.builder.testing.ConnectedDevice.pullFile(ConnectedDevice.java:114)
    at com.android.builder.internal.testing.SimpleTestCallable.call(SimpleTestCallable.java:158)
    at com.android.builder.internal.testing.SimpleTestCallable.call(SimpleTestCallable.java:42)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:439)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)   
Caused by: com.android.ddmlib.SyncException: Permission denied
    at com.android.ddmlib.SyncService.doPullFile(SyncService.java:511)
    at com.android.ddmlib.SyncService.pullFile(SyncService.java:320)
    at com.android.ddmlib.Device.pullFile(Device.java:849)
    at com.android.builder.testing.ConnectedDevice.pullFile(ConnectedDevice.java:107)
    ... 10 more                
:myDirections:connectedAndroidTest FAILED      

FAILURE: Build failed with an exception.

Travis-CI 构建脚本以获得代码覆盖率

将此块包含在 build.gradle 中,用于所有模块(库、示例等)

Include this block in build.gradle, for all modules (library, sample, etc)

android {
    lintOptions {
        abortOnError false
    }
}

下面是.travis-ci.yml文件

language: android
jdk: oraclejdk8
sudo: required

android:
  components:
  # Uncomment the lines below if you want to
  # use the latest revision of Android SDK Tools
  - tools
  - platform-tools
  # The BuildTools version used by your project
  - build-tools-28.0.3
  # The SDK version used to compile your project
  - android-28
  - android-22
  - add-on
  # Additional components
  - extra-google-google_play_services
  - extra-android-support
  - extra-google-m2repository
  - extra-android-m2repository
  # Specify at least one system image,
  # if you need to run emulator(s) during your tests
  - sys-img-armeabi-v7a-android-22

  licenses:
  - 'android-sdk-preview-license-52d11cd2'
  - 'android-sdk-license-.+'
  - 'google-gdk-license-.+'

before_cache:
- rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
- rm -fr $HOME/.gradle/caches/*/plugin-resolution/

cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/
    - $HOME/.android/build-cache

before_install:
- yes | sdkmanager "build-tools;28.0.3"

before_script:
- echo no | android create avd --force -n test -t android-22 --abi armeabi-v7a -c 100M
- emulator -avd test -no-audio -no-window &
- android-wait-for-emulator
- sleep 180
- adb devices
- adb shell input keyevent 82 &

script:
- ./gradlew build connectedCheck

after_success:
- bash <(curl -s https://codecov.io/bash)

这篇关于如何使用 Android gradle 插件 0.10.0 或更高版本获取 jacoco 覆盖率报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆