5
votes

I have configured a Jenkins item that, after gradle building and testing, calls the "Execute SonarQube Scanner" with the following specifications:

sonar.projectKey=my_project_key
sonar.projectName=my_Android
sonar.projectVersion=1.0
sonar.sources=app/src
sonar.java.binaries=app/build/intermediates/classes/debug
sonar.coverage.exclusions=app/src/test/**,app/src/androidTest/**,app/src/main/res/**

This successfully runs SonarQube and generates a report to IPaddress:9000 that measures: Bugs, Vulnerabilities, Debt, Code Smells, and Duplications just like it is supposed to. However there is no place in this report that defines the code covered by tests. SonarQube is running the Kotlin plugin successfully and sees the codebase itself, but is not determining coverage. Is this possible to gather this information with SonarQube when using Kotlin Android?

I did try installing Jacoco, but did not see any new data on the SonarQube results.

4
did you find a solution? I'm stuck in the exact problem, I got the other reports, but not the coveragegiroxiii
@giroxiii I abandoned attempting to get Kotlin code coverage with Sonar shortly after posting the question. HOWEVER, I have gotten many projects to work with Java and JaCoCo. Retrospectively looking back at my sonar.properties file I may have been able to fix this by referencing the output of the JaCoCo task by adding the following line to the sonar.properties file: ``` sonar.jacoco.reportPath=build/jacoco/test.exec ``` I am not sure that this will work with Kotlin, but it helped with Java coverage. I hope this helps, and good luck!Don
Newer versions of Sonarqube have a Kotlin plugin that integrates with the Jacoco plugin and detects Kotlin projects correctly. I've modified my answer to reflect thisMikezx6r

4 Answers

1
votes

You can install a 3rd-party Sonar plugin here that will measure code quality for Kotlin projects. Unfortunately, as of this posting, SonarQube does not have its own plugin for this.

Once that is set up, take a look here for setting up gradle and sonar-project.properties. The post is a little dated, but contains a link to an updated version and is still a good starting point.

TL;DR for #2: The information SonarQube needs for Kotlin is located in 2 places. One is already identified by the OP. The other is located in app/build/tmp/kotlin-classes.

0
votes

If you are using gradle and Jacoco. You can use Jacoco for the code coverage with the xmlReportPaths. With that Sonar will trust what Jacoco output.

sonarqube {
    properties {
        property("sonar.java.coveragePlugin", "jacoco")
        property("sonar.coverage.jacoco.xmlReportPaths", "./build/reports/jacoco/test/jacocoTestReport.xml")
    }
}

Documentation from Sonar + Jacoco Plugin. It also works with mixed source code (Java, Kotlin).

0
votes

I abandoned attempting to get Kotlin code coverage with Sonar shortly after posting the question.

However, I have gotten many projects to work with Java and JaCoCo.

Retrospectively looking back at my sonar.properties file I may have been able to fix this by referencing the output of the JaCoCo task by adding the following line to the sonar.properties file:

sonar.jacoco.reportPath=build/jacoco/test.exec

I am not sure that this will work with Kotlin, but it helped with Java coverage and solved my use case.

0
votes

Newer versions of Sonarqube (6+) now support Kotlin natively, and do not require any custom configuration in your project.

Just ensure the Kotlin and the Jacoco plugins are installed, and it should just work for you.


My project is using Jacoco for generating coverage reports. To have SonarQube pick up the results, try setting the following:

sonar.java.binaries={$buildDir}/classes/kotlin 

In other words let SonarQube know where the Kotlin class files are.

This configuration assumes a 100% Kotlin project. If your project is a mix of Java and Kotlin, then you'll have to determine how to configure both directories.