JaCoCo 支持已添加到 Android gradle 插件 v0.10 ( http://tools.android.com/tech-docs/new-build-system )。
Enable in the tested Build Type with testCoverageEnabled = true
android {
jacoco {
version = '0.6.2.201302030002'
}
}
通过关注http://chrisjenx.com/gradle-robolectric-jacoco-dagger/,我能够获得 JaCoCo 与 Robolectric 合作的报道。
apply plugin: 'android'
apply plugin: 'robolectric'
apply plugin: 'jacoco'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.android.support:appcompat-v7:19.1.+'
androidTestCompile fileTree(dir: 'libs/test', include: '*.jar')
androidTestCompile 'junit:junit:4.11'
androidTestCompile 'org.robolectric:robolectric:2.3'
androidTestCompile 'com.squareup:fest-android:1.0.+'
}
robolectric {
// Configure the set of classes for JUnit tests
include '**/*Test.class'
exclude '**/*AbstractRobolectricTestCase.class'
// Configure max heap size of the test JVM
maxHeapSize = "2048m"
}
jacoco {
toolVersion = "0.7.1.201405082137"
}
//Define coverage source.
//If you have rs/aidl etc... add them here.
def coverageSourceDirs = [
'src/main/java',
'src/gen'
]
...
// Add JaCoCo test reporting to the test task
// http://chrisjenx.com/gradle-robolectric-jacoco-dagger/
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports after running tests."
reports {
xml.enabled = true
html.enabled = true
}
// Class R is used, but usage will not be covered, so ignore this class from report
classDirectories = fileTree(
dir: './build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class'
])
sourceDirectories = files(coverageSourceDirs)
executionData = files('build/jacoco/testDebug.exec')
}