2
votes

I try to display the tests coverage in SonarQube of my Android Kotlin app of the code in my app module. I can generate the jacoco coverage results and display SonarQube qualymetrics but the problem is that the tests coverage is not displayed in SonarQube :

https://imgur.com/a/xOjxLl1

in my build.gradle of my project I have :

...
dependencies {
    classpath 'org.jacoco:org.jacoco.core:0.8.3'
}
...

in my build.gradle of my app module

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'

jacoco {
    toolVersion = '0.8.3'
}

tasks.withType(Test) {
    jacoco.includeNoLocationClasses = true
}
android {
   ...
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            testCoverageEnabled true
        }
        debug {
            testCoverageEnabled true
        }
     testOptions {
        animationsDisabled true
        unitTests {
            returnDefaultValues = true
            includeAndroidResources = true
        }
    }
task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {

    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = [ '**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*' ]
    def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)
    def mainSrc = "$project.projectDir/src/main/java"

    sourceDirectories = files([mainSrc])
    classDirectories = files([debugTree])
    executionData = fileTree(dir: project.buildDir, includes: [
            'jacoco/testDebugUnitTest.exec', 'outputs/code-coverage/connected/*coverage.ec'
    ])
}

sonarqube {
    properties {
        property 'sonar.projectKey', 'mySonarKey'
        property 'sonar.projectName', 'myProjectName'
        property "sonar.host.url", "http://localhost:9000" // local sonar address

        property "sonar.java.binaries", 'target/classes,app/build/tmp/kotlin-classes'
        property "sonar.sources", 'src/main/java'
        property "sonar.tests", 'src/androidTest/java'

        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.coverage.jacoco.xmlReportPaths", "./build/reports/jacoco/jacocoTestReport/jacocoTestReport.xml" //link to xml file that generated by jacoco
    }
}
...

I generate my jacoco report with : gradlew clean jacocoTestReport

And generate my sonarqube analysis with : gradlew sonarqube

the jacoco report is generated myAppName\app\build\reports\jacoco\jacocoTestReport\jacocoTestReport.xml

I also have an html folder next to the report where I can see that my test coverage generated with the right coverage.

So the question is what can I change to display my generated jacoco coverage in SonarQube ?

(I'm not sure about the sonarqube.properties that I add in my app module build.gradle)

1

1 Answers

1
votes

I had the same issue for a long time and the solution is to change your path in the build.gradle :

def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debug", excludes: fileFilter)

To :

def debugTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/snapshot", excludes: fileFilter)

Otherwise, the .xml generated won't include the coverage for your whole source code and sonarqube will display 0% coverage as a result

Note : my target folder was called 'snapshot' but it might be different for you