简体   繁体   English

覆盖率报告称使用gradle对android进行零覆盖

[英]Coverage report says zero coverage on android with gradle

I've been unable to get code coverage to work on any of my android projects. 我一直无法获得代码覆盖率来处理我的任何Android项目。

To simplify things I created a new project (selected empty activity). 为简化起见,我创建了一个新项目(选定的空活动)。 Added a new utility class to the the project in src/main/java. 在src / main / java中为项目添加了一个新的实用程序类。

I then created a unit test for it in src/androidTest/java. 然后我在src / androidTest / java中为它创建了一个单元测试。

Updated the gradle file to enable coverage. 更新了gradle文件以启用覆盖。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.michaelkrussel.coverageapp"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }

    jacoco {
        version "0.7.1.201405082137"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
             testCoverageEnabled true
        }
    }
}

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

I ran the tests then using ./gradlew createDebugCoverageReport. 我使用./gradlew createDebugCoverageReport运行测试。 The unit test shows that the test I created passed, but the coverage report reports zero coverage. 单元测试显示我创建的测试通过,但覆盖率报告报告零覆盖。

I assume I'm either missing something in the gradle file or not running the correct gradle task, but I cannot figure out what. 我假设我要么丢失gradle文件中的某些内容,要么没有运行正确的gradle任务,但我无法弄清楚是什么。

I'm using this configuration in my project and work fine! 我在我的项目中使用此配置并正常工作!

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.1"

    defaultConfig {
        applicationId "br.com.acs.app"
        minSdkVersion 18
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        testApplicationId "br.com.acs.app.test"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            testCoverageEnabled true
        }
    }

    packagingOptions {
        exclude 'LICENSE.txt'
    }
}

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

    androidTestCompile 'junit:junit:4.11'
    androidTestCompile('com.android.support.test:testing-support-lib:0.1') {
        exclude group: 'junit' 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM