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

时间:2015-12-15 15:21:19

标签: android gradle code-coverage jacoco greendao

我试图通过GreenDao框架排除生成的文件,GreenDao框架放在一个名为dao的软件包中,来自我的Jacoco生成的代码覆盖率报告,但是创建一个自定义任务,如下所示无效。

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('$$', '$'))
            }
        }
    }
}

这是我的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'
    }

如何在不创建自定义任务的情况下从代码覆盖中排除某些类或包? 提前谢谢。

1 个答案:

答案 0 :(得分:0)

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

您可以在excludes属性中添加要在此处排除的内容。它需要一个字符串/正则表达式数组,因为你可以看到例如InjectAdapter类被排除,这些类是由dagger生成的,我认为,ViewInjector类是由butter刀生成的,所以如果你用{{1为所有GreenDao类加上后缀像Dao那样你可以排除所有以Dao结尾的类

'** / * Dao.class'

<强>解释

somethingDao.class匹配零个或多个目录

**匹配以Dao.class

结尾的所有字符串

您可以获得有关语法here

的更多信息