本文主要是介绍Android开发环境和Jenkins CI集成Jacoco,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Android开发环境集成
- 集成Jacoco Plugin
在Project的build.gradle中添加:
dependencies {xxx//新增classpath "org.jacoco:org.jacoco.core:0.8.2"}
在App模块的build.gradle apply plugin
project.plugins.apply('jacoco')
在apply完plugin后,通过执行.gradlew clean testDebugUnitTest就会在build目录生成jacoco/testDebugUnitTest.exec,涵盖了测试coverage的信息。通过第2步,创建jacocoTestReport task会根据该文件和一些configuration生成Html报告。
2. 创建jacocoTestReportTask和配置jacoco(在高版本的jacoco和apply了java plugin的project自动创建该Task)
project.jacoco.toolVersion '0.8.2'
project.android.buildTypes {//toggle for report onlydebug {testCoverageEnabled true}release {testCoverageEnabled false}
}//如果集成Jenkins Pipeline,只需在Jenkins上安装Jacoco plugin,并且使用下面的Test Stage即可,无需执行jacocoTestReport task
//stage('Test') {
// steps {
// sh './gradlew -p ${project} testDebugUnitTest'
// jacoco(
// execPattern: "${project}/build/jacoco/testDebugUnitTest.exec",
// classPattern: "${project}/build/intermediates/javac/Debug, ${project}/build/tmp/kotlin-classes/Debug",
// sourcePattern: "${project}/src/main/java, ${project}/src/main/kotlin",
// exclusionPattern: '**/R.class,**/R$*.class,**/*$ViewInjector*.*,**/*$Companion.*,**/*$ViewBinder*.*,**/BuildConfig.*,**/Manifest*.*,**/*$Lambda$*.*,**/*Module.*,**/*Dagger*.*,**/*MembersInjector*.*,**/*_Provide*Factory*.*,**/*_Factory*.*,**/*$*$*.*'
// )
// }
//}
project.tasks.create(name: 'jacocoTestReport',type: JacocoReport,dependsOn: "testDebugUnitTest"
) {group = "Reporting"description = "Generate Jacoco coverage reports for the ${project.name}."def buildDir = project.buildDirdef coverageSourceDirs = ["src/main/java","src/main/kotlin"]def fileFilter = ['**/R.class','**/R$*.class','**/*$ViewInjector*.*','**/*$ViewBinder*.*','**/*$Companion.*','**/BuildConfig.*','**/Manifest*.*','**/*$Lambda$*.*','**/*Module.*','**/*Dagger*.*','**/*MembersInjector*.*','**/*_Provide*Factory*.*','**/*_Factory*.*','**/*$*$*.*']def javaClasses = fileTree(dir: "$buildDir/intermediates/javac/debug",excludes: fileFilter)def kotlinClasses = fileTree(dir: "$buildDir/tmp/kotlin-classes/debug",excludes: fileFilter)classDirectories.from(files([javaClasses], [kotlinClasses]))additionalSourceDirs.from(files(coverageSourceDirs))sourceDirectories.from(files(coverageSourceDirs))executionData.from(files("${project.buildDir}/jacoco/testDebugUnitTest.exec"))reports {xml.enabled = truehtml.enabled = true}
}
目前为止,通过执行./gradlew jacocoTestReport就能生成想要的测试报告:
Android Jenkins CI
- Jenkins集成时,针对我们的repo源码,可以不用create jacocoTestReport这个Task,但是需要在Jenkins上安装Jacoco Plugin:
然后搜索JaCoCo Plugin,安装并重启Jenkins,
在Test Stage中添加如下配置:
stage('Test') {steps {sh './gradlew -p ${project} testDebugUnitTest'jacoco(execPattern: "${project}/build/jacoco/testDebugUnitTest.exec",classPattern: "${project}/build/intermediates/javac/Debug, ${project}/build/tmp/kotlin-classes/Debug",sourcePattern: "${project}/src/main/java, ${project}/src/main/kotlin",exclusionPattern: '**/R.class,**/R$*.class,**/*$ViewInjector*.*,**/*$Companion.*,**/*$ViewBinder*.*,**/BuildConfig.*,**/Manifest*.*,**/*$Lambda$*.*,**/*Module.*,**/*Dagger*.*,**/*MembersInjector*.*,**/*_Provide*Factory*.*,**/*_Factory*.*,**/*$*$*.*')}
}
最后的结果报告:
Sample
https://github.com/Visualrainy/AndroidJacoco
这篇关于Android开发环境和Jenkins CI集成Jacoco的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!