本文介绍了从Android的jacoco代码覆盖范围中排除软件包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

任务jacocoTestReport(类型:JacocoReport,dependsOn:testDebug){

group =报告
description =运行测试后生成Jacoco覆盖报告。
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)
//有点乱码,但修复https://code.google.com/p/android/issues/detail?id=69174。
//我们遍历已编译的.class树并将$$重命名为$。
doFirst {
new File($ buildDir / intermediates / classes /)。eachFileRecurse {file - >
if(file.name.contains('$$')){
file.renameTo(file.path.replace('$$','$'))
}





这是我的build.gradle:



$ b $ / p>

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

testOptions {
unitTests.returnDefaultValues = true
}

compileSdkVersion 22
buildToolsVersion '22 .0.1'

defaultConfig {
applicationIdcom.nitralabs.m1_mm
minSdkVersion 12
targetSdkVersion 18
}

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

依赖项{
compile'c​​om.google.code.gson:gson: 2.1'
编译文件('libs / greendao-1.3.1.jar')
编译'com.android.support:appcompat-v7:22.2.0'
编译'com.android .support:recyclerview-v7:22.2.0'
testCompile'或g.mockito:mockito-core:1.10.19'
testCompile'org.hamcrest:hamcrest-library:1.1'
compile'junit:junit:4.12'
compile'org.apache。 commons-io:1.3.2'
testCompile'c​​om.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',
不包括:['** / R * .class',
'** / * $ InjectAdapter.class',
'** / * $ ModuleAdapter.class',
'** / Dao * .class',
'** / * $ ViewInjector * .class'
])

您可以在这里放入您想要排除的任何内容,在 excludedescription 属性。它需要一个字符串/正则表达式的数组,因为你可以看到,例如InjectAdapter类被排除在外,这些类是由我认为的匕首生成的,而ViewInjector类是由黄油刀生成的,所以如果你用<$ c后缀所有的GreenDao类$ c> Dao 就像 somethingDao.class 那么你可以排除所有以Dao结尾的类,比如



'** / * Dao.class'

解释

** 匹配零个或多个目录
$ b

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



您可以获得更多关于语法的信息


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.

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:

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. 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

'**/*Dao.class'

Explanation

** Match zero or more directories

*Dao.class matches all strings ending with Dao.class

You can get more info on the syntax here

这篇关于从Android的jacoco代码覆盖范围中排除软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 01:31