简体   繁体   English

从Android的jacoco代码覆盖率中排除软件包

[英]Exclude package from jacoco code coverage for Android

I'm trying to exclude generated files by GreenDao framework, which placed in a package named dao, from my code coverage report generated by Jacoco, but creating a custom task like following doesn't work. 我正在尝试从Jacoco生成的代码覆盖率报告中排除GreenDao框架生成的文件,该文件放置在名为dao的程序包中,但是无法创建如下自定义任务。

def coverageSourceDirs = [
        '../app/src/main/java'
]

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {

    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }
    classDirectories = fileTree(
            dir: './build/intermediates/classes/debug',
            excludes: ['**/R*.class',
                       '**/*$InjectAdapter.class',
                       '**/*$ModuleAdapter.class',
                       '**/Dao*.class',
                       '**/*$ViewInjector*.class'
            ])
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("$buildDir/jacoco/testDebug.exec")
    // Bit hacky but fixes https://code.google.com/p/android/issues/detail?id=69174.
    // We iterate through the compiled .class tree and rename $$ to $.
    doFirst {
        new File("$buildDir/intermediates/classes/").eachFileRecurse { file ->
            if (file.name.contains('$$')) {
                file.renameTo(file.path.replace('$$', '$'))
            }
        }
    }
}

Here is my build.gradle: 这是我的build.gradle:

apply plugin: 'com.android.application'
    android {
        jacoco {
            version = '0.7.3.201502191951'
        }

        testOptions {
            unitTests.returnDefaultValues = true
        }

        compileSdkVersion 22
        buildToolsVersion '22.0.1'

        defaultConfig {
            applicationId "com.nitralabs.m1_mm"
            minSdkVersion 12
            targetSdkVersion 18
        }

        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
            debug {
                testCoverageEnabled = true
            }
        }
        packagingOptions {
            exclude 'META-INF/DEPENDENCIES.txt'
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
            exclude 'META-INF/NOTICE'
            exclude 'META-INF/LICENSE'
            exclude 'META-INF/DEPENDENCIES'
            exclude 'META-INF/notice.txt'
            exclude 'META-INF/license.txt'
            exclude 'META-INF/dependencies.txt'
            exclude 'META-INF/LGPL2.1'
        }
    }

    dependencies {
        compile 'com.google.code.gson:gson:2.1'
        compile files('libs/greendao-1.3.1.jar')
        compile 'com.android.support:appcompat-v7:22.2.0'
        compile 'com.android.support:recyclerview-v7:22.2.0'
        testCompile 'org.mockito:mockito-core:1.10.19'
        testCompile 'org.hamcrest:hamcrest-library:1.1'
        compile 'junit:junit:4.12'
        compile 'org.apache.commons:commons-io:1.3.2'
        testCompile 'com.android.support.test:testing-support-lib:0.1'
        testCompile 'junit:junit:4.12'
        androidTestCompile 'com.jayway.android.robotium:robotium-solo:5.5.2'
    }

How can I exclude some classes or package from code coverage without creating custom task? 如何在不创建自定义任务的情况下从代码覆盖范围中排除某些类或程序包? Thank you in advance. 先感谢您。

classDirectories = fileTree(
        dir: './build/intermediates/classes/debug',
        excludes: ['**/R*.class',
                   '**/*$InjectAdapter.class',
                   '**/*$ModuleAdapter.class',
                   '**/Dao*.class',
                   '**/*$ViewInjector*.class'
        ])

You can put whatever you want to exclude here, in the excludes property. 您可以在此处将要排除的任何内容放在excludes属性中。 It takes an array of strings/regex as you can see that for example InjectAdapter classes are excluded, these classes are generated by dagger I think, and ViewInjector classes are generated by butter knife, so if you suffixed all your GreenDao classes with Dao like somethingDao.class then you can exclude all classes that end with Dao like so 它需要一个字符串/正则表达式数组,如您所见,例如,InjectAdapter类被排除在外,这些类是由我认为的匕首生成的,而ViewInjector类是由黄油刀生成的,因此,如果您将所有GreenDao类添加到Dao后面,例如somethingDao.class那么您可以像这样排除所有以Dao结尾的类

'**/*Dao.class' '** / * Dao.class'

Explanation 说明

** Match zero or more directories **匹配零个或多个目录

*Dao.class matches all strings ending with Dao.class *Dao.class匹配所有以Dao.class结尾的字符串

You can get more info on the syntax here 您可以在此处获取有关语法的更多信息

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

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