简体   繁体   中英

How to skip the package from code coverage using Jacoco in android gradle project?

Below is the build.gradle file

apply plugin: 'android'
apply plugin: 'jacoco'

android {

   compileSdkVersion 'Google Inc.:Google APIs:19'
   buildToolsVersion "19.1.0"
   jacoco {
      version = '0.6.2.201302030002'
   }

   dexOptions {
      preDexLibraries = false
   }

   android.enforceUniquePackageName=false

   sourceSets {
      androidTest {
         java.srcDirs = ['\\src\\androidTest\\java', '\\src\\integTest\\
      }
      main {
         manifest.srcFile('src/main/AndroidManifest.xml')
         java.srcDir file('src/main/java')
         res.srcDirs = ['src/main/res']
      }
    }

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 17
        versionCode 35
        versionName "3.4.5"
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
    }

    buildTypes {
       debug {
          testCoverageEnabled = true
       }
    }
}

Any one have idea what I'm missing.

I'm using this gradle task for JaCoCo reports.

classDirectories -> excludes - look at this line in the task.

def coverageSourceDirs = [
        'src/test/java'
]

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"
    classDirectories = fileTree(
            dir: "$buildDir/intermediates/classes/debug",
            // Specify here files and packages which should be excluded from reports
            excludes: ['**/R.class',
                       '**/R$*.class',
                       '**/*$ViewInjector*.*',
                       '**/BuildConfig.*',
                       '**/Manifest*.*']
    )
    reports {
        xml.enabled = true
        html.enabled = true
    }
    additionalSourceDirs = files(coverageSourceDirs)
    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('$$', '$'))
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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