如何在android gradle项目中使用Jacoco从代码覆盖中跳过包?

时间:2014-07-03 13:46:12

标签: android gradle jacoco

以下是build.gradle文件

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

任何人都知道我错过了什么。

1 个答案:

答案 0 :(得分:2)

我正在使用此gradle任务进行JaCoCo次报告。

classDirectories - >排除 - 在任务中查看此行。

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