1

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.

Margarita Spasskaya
  • 643
  • 2
  • 10
  • 24

2 Answers2

0
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

Bhargav
  • 8,118
  • 6
  • 40
  • 63
0

If you need to exclude certain packages, you could include those inside the excludes array as shown below.

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

The "dao" package exclusion is added in last. You add any package exclusion using same way.

GAJJE82
  • 1,427
  • 1
  • 10
  • 7