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



我正试图从Jacobo生成的代码覆盖率报告中排除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('$$', '$'))
            }
        }
    }
}

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

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

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

您可以将要排除的任何内容放在excludes属性中。它使用一个字符串/正则表达式数组,正如你所看到的,例如,InjectAdapter类被排除在外,我认为这些类是由dagger生成的,ViewInjector类是由butter knife生成的。因此,如果你用Dao(如somethingDao.class)作为所有GreenDao类的后缀,那么你就可以排除所有以Dao(如)结尾的类

'**/*Dao.class'

解释

**匹配零个或多个目录

*Dao.class匹配所有以Dao.class 结尾的字符串

您可以在此处获得有关语法的更多信息

如果需要排除某些包,可以将这些包包括在excludes数组中,如下所示。

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

";道;最后添加了包排除。您可以使用相同的方式添加任何包排除。

相关内容

  • 没有找到相关文章

最新更新