Skip to content

Commit

Permalink
Fix Jacoco coverage and JacocoFullCoverage tasks
Browse files Browse the repository at this point in the history
- A bug as reported on a relative repository vanniktech/gradle-android-junit-jacoco-plugin#183
  prevents jacoco exec coverage file from being generated when android.buildTypes.debug.testCoverageEnabled is true
- Disabled the createDebugCoverageReport from the jacocoTestReport therefore disabling running of instrumentation tests
  • Loading branch information
ekigamba committed Aug 4, 2022
1 parent ab02d0e commit 0406944
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 30 deletions.
36 changes: 19 additions & 17 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ext {
compileSdkVersion = 28
volleyVersion = "1.1.0"
targetSdkVersion = 27
jacocoVersion = "0.8.7"
jacocoVersion = "0.8.8"
mapboxSdkVersion = "8.3.3"
mapboxAnnotationPluginVersion = "0.6.0"
}
Expand All @@ -71,40 +71,34 @@ coveralls {
, "wrapper/src/main/java"]
}

task jacocoFullReport(type: JacocoReport, group: 'Coverage reports') {
task jacocoFullReport(type: JacocoReport, group: 'Coverage reports', dependsOn: ':utils:jacocoTestReport') {
description = 'Generates an aggregate report from all subprojects'

additionalSourceDirs.from = files(subprojects.sourceSets.main.allSource.srcDirs)
sourceDirectories.from = files(subprojects.sourceSets.main.allSource.srcDirs)
classDirectories.from = files(subprojects.sourceSets.main.output)

def index = 0
for (dir in additionalSourceDirs) {
System.out.println( (index + 1) + ". " + dir.path)
}

index = 0
for (dir in sourceDirectories) {
System.out.println( (index + 1) + ". " + dir.path)
}

index = 0
for (dir in classDirectories) {
System.out.println( (index + 1) + ". " + dir.path)
}

List<File> executionDataPaths = new ArrayList<>()
List<File> classPaths = new ArrayList<>()
List<File> sourcePaths = new ArrayList<>()
List<File> jacocoClasspathPaths = new ArrayList<>()

for (subModule in subprojects) {
def jacocoTestReportTask = subModule.getTasksByName("jacocoTestReport", false)
jacocoTestReportTask.forEach({task ->
if (task instanceof JacocoReport) {
System.out.println("This is a JacocoReport task: " + task.name)
executionDataPaths.addAll((task as JacocoReport).executionData.getFiles())
classPaths.addAll((task as JacocoReport).classDirectories)
sourcePaths.addAll((task as JacocoReport).sourceDirectories)
jacocoClasspathPaths.addAll((task as JacocoReport).jacocoClasspath)
}
})
}

executionData.from = executionDataPaths
sourceDirectories.from = sourcePaths
classDirectories.from = classPaths

getReports().getXml().setDestination(file("${buildDir}/reports/jacoco/jacocoFullReport/merged.xml"))
getReports().getHtml().setDestination(file("${buildDir}/reports/jacoco/jacocoFullReport/html"))
Expand All @@ -113,5 +107,13 @@ task jacocoFullReport(type: JacocoReport, group: 'Coverage reports') {
html.enabled true
xml.enabled true
}

jacocoClasspath = files(jacocoClasspathPaths)
}


task printClasspath {
doLast {
this.printClasspath
}
}
8 changes: 5 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ android {
}

debug {
testCoverageEnabled true
// See bug https://github.com/vanniktech/gradle-android-junit-jacoco-plugin/issues/183
testCoverageEnabled false

if (project.rootProject.file("local.properties").exists()) {
Properties properties = new Properties()
Expand Down Expand Up @@ -178,7 +179,8 @@ tasks.withType(Test) {
jacoco.excludes = ['jdk.internal.*']
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
// createDebugCoverageReport is not available when android.buildTypes.debug.testCoverageEnabled is false
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) { //, 'createDebugCoverageReport']) {

reports {
xml.enabled = true
Expand All @@ -190,7 +192,7 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'crea

def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*'
, 'android/**/*.*', "io/realm/*.*"]
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: fileFilter)
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/classes", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"

sourceDirectories.setFrom([mainSrc])
Expand Down
8 changes: 5 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ android {
}

debug {
testCoverageEnabled true
// See bug https://github.com/vanniktech/gradle-android-junit-jacoco-plugin/issues/183
testCoverageEnabled false
buildConfigField "String", "MAPBOX_SDK_ACCESS_TOKEN", properties["mapbox.sdk.token"]
buildConfigField "String", "CGR_USERNAME", properties["cgr.username"]
buildConfigField "String", "CGR_PASSWORD", properties["cgr.password"]
Expand Down Expand Up @@ -242,7 +243,8 @@ tasks.withType(Test) {
jacoco.excludes = ['jdk.internal.*']
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
// createDebugCoverageReport is not available when android.buildTypes.debug.testCoverageEnabled is false
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) { //, 'createDebugCoverageReport']) {

reports {
xml.enabled = true
Expand All @@ -254,7 +256,7 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'crea

def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*'
, 'android/**/*.*', "io/realm/*.*", "io/ona/kujaku/sample/activities/*.*"]
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: fileFilter)
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/classes", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"

sourceDirectories.setFrom(files([mainSrc]))
Expand Down
13 changes: 10 additions & 3 deletions utils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ android {
}

debug {
testCoverageEnabled true
// See bug https://github.com/vanniktech/gradle-android-junit-jacoco-plugin/issues/183
testCoverageEnabled false
}
}

Expand Down Expand Up @@ -68,9 +69,11 @@ dependencies {

tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
// createDebugCoverageReport is not available when android.buildTypes.debug.testCoverageEnabled is false
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) { //, 'createDebugCoverageReport']) {

reports {
xml.enabled = true
Expand All @@ -82,7 +85,7 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'crea

def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*'
, 'android/**/*.*', "io/realm/*.*"]
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: fileFilter)
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/classes", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"

sourceDirectories.setFrom(files([mainSrc]))
Expand All @@ -92,5 +95,9 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'crea
]))
}

task printTasks {
System.out.println("Jacoco version ${jacoco.toolVersion}")
}

// Add github packages, maven-central and sonatype publishing
apply from: '../publish.gradle'
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
*/

@RunWith(RobolectricTestRunner.class)
@Config(manifest = Config.NONE)
public class MapBoxStyleHelperTest {

/**
Expand Down
8 changes: 5 additions & 3 deletions wrapper/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ android {
}

debug {
testCoverageEnabled true
// See bug https://github.com/vanniktech/gradle-android-junit-jacoco-plugin/issues/183
testCoverageEnabled false
}
}

Expand Down Expand Up @@ -69,7 +70,8 @@ tasks.withType(Test) {
jacoco.excludes = ['jdk.internal.*']
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {
// createDebugCoverageReport is not available when android.buildTypes.debug.testCoverageEnabled is false
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest']) { //, 'createDebugCoverageReport']) {

reports {
xml.enabled = true
Expand All @@ -81,7 +83,7 @@ task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'crea

def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*'
, 'android/**/*.*', "io/realm/*.*"]
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/compileDebugJavaWithJavac/classes", excludes: fileFilter)
def debugTree = fileTree(dir: "${buildDir}/intermediates/javac/debug/classes", excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"

sourceDirectories.setFrom(files([mainSrc]))
Expand Down

0 comments on commit 0406944

Please sign in to comment.