Guest User

Untitled

a guest
Apr 20th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. apply plugin: "jacoco"
  2.  
  3. jacoco {
  4. toolVersion = deps.test.jacocoVersion
  5. }
  6.  
  7. tasks.withType(Test) {
  8. jacoco.includeNoLocationClasses = true
  9. }
  10.  
  11. task('jacocoReports') {
  12. group = "Reporting"
  13. description = "Generate Jacoco coverage reports for all variants"
  14. }
  15.  
  16. task('jacocoVerifications') {
  17. group = "Verification"
  18. description = "Generate Jacoco coverage reports for all variants"
  19. }
  20.  
  21. variants().all { variant ->
  22. def params = prepareJacocoParams(variant)
  23. def reportTask = createReportTask(params)
  24. def verificationTask = createVerificationTask(params)
  25. verificationTask.dependsOn reportTask
  26. jacocoReports.dependsOn reportTask
  27. }
  28.  
  29. def prepareJacocoParams(variant) {
  30. def params = [:]
  31. params.variantName = variant.name
  32. params.variantCapName = variant.name.capitalize()
  33. params.fileFilter = [ '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*',
  34. // '**/App*.*',
  35. // '**/*Activity*.*',
  36. ]
  37. params.classDirectories = files([fileTree(
  38. dir: variant.javaCompiler.destinationDir,
  39. excludes: params.fileFilter
  40. ) + fileTree(
  41. dir: "$buildDir/tmp/kotlin-classes/$params.variantName",
  42. excludes: params.fileFilter
  43. )])
  44.  
  45. params.sourceDirectories = files([
  46. "src/main/java", "src/main/kotlin",
  47. "src/$params.variantName/java", "src/$params.variantName/kotlin"
  48. ])
  49. params.executionData = files("${buildDir}/jacoco/junitPlatformTest${params.variantCapName}.exec")
  50. return params
  51. }
  52.  
  53. def createReportTask(params) {
  54. return task("jacoco${params.variantCapName}Report", type: JacocoReport, dependsOn: "test${params.variantCapName}UnitTest") {
  55. group = "Reporting"
  56. description = "Generate Jacoco coverage reports for $params.variantCapName"
  57. reports {
  58. xml.enabled = true
  59. html.enabled = true
  60. csv.enabled = false
  61. }
  62. classDirectories = params.classDirectories
  63. sourceDirectories = params.sourceDirectories
  64. executionData = params.executionData
  65. }
  66. }
  67.  
  68. def createVerificationTask(params) {
  69. return task("jacoco${params.variantCapName}Verification", type: JacocoCoverageVerification) {
  70. sourceDirectories = params.sourceDirectories
  71. classDirectories = params.classDirectories
  72. executionData = params.executionData
  73. violationRules {
  74. failOnViolation true
  75. rule {
  76. element = 'PACKAGE'
  77. includes = ['com.myapp.domain.*']
  78. excludes = ['com.myapp.domain.entity']
  79. limit {
  80. counter = 'CLASS'
  81. value = 'MISSEDCOUNT'
  82. maximum = 0
  83. }
  84. }
  85. rule {
  86. element = 'CLASS'
  87. includes = ['com.myapp.domain.*']
  88. excludes = ['com.myapp.domain.entity.*']
  89. limit {
  90. counter = 'INSTRUCTION'
  91. value = 'COVEREDRATIO'
  92. minimum = 0.8
  93. }
  94. }
  95. }
  96. }
  97. }
  98.  
  99. def variants() {
  100. if (project.android.hasProperty('libraryVariants')) {
  101. return project.android.libraryVariants
  102. } else {
  103. return project.android.applicationVariants
  104. }
  105. }
Add Comment
Please, Sign In to add comment