Not able to update Code Coverage on SonarCloud for my android project

  • ALM used : GitHub
  • CI system used : Circle CI
  • Languages of the repository : Java, Kotlin
  • Error observed : No errors.
  • Steps to reproduce : Generate JaCoCo coverage report on system locally, then upload this report to SonarCloud.
  • Potential workaround : None identified

Hello Team,

I am trying to update the coverage report for my android project on SonarCloud, but I am not able to do so. Below are the steps for updating the report :

1. Integrate JaCoCo plugin with android project.

apply plugin: 'jacoco'

jacoco {
    toolVersion = "0.8.8"
    reportsDirectory.set(layout.buildDirectory.dir("jacoco"))
}

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

task jacocoTestReport(type: JacocoReport, dependsOn: "testDebugUnitTest") {
    group = "Reporting"
    description = "Generate Jacoco coverage reports"

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

    def fileFilter = []
    def mainSrc = "${project.projectDir}/src/main/java"
//    def javaTree = fileTree(dir: "$project.buildDir/intermediates/javac/debug/classes", excludes: fileFilter)
    def kotlinTree = fileTree(dir: "$project.buildDir/tmp/kotlin-classes/debugUnitTest", excludes: fileFilter)

    sourceDirectories.setFrom(files([mainSrc]))
    classDirectories.setFrom(files([kotlinTree]))
    executionData.setFrom(fileTree(dir: "$project.projectDir",
            includes: ['**/*.exec', '**/*.ec']
    ))
}

2. Integrate SonarQube with Android project.

sonarqube {
    properties {
        property "sonar.organization", "<org_key>"
        property "sonar.projectKey", "<prj_key>"
        property "sonar.host.url", "https://sonarcloud.io"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.sources", "src/main/java"
        property "sonar.binaries", "build"
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.coverage.jacoco.xmlReportPaths", "app/build/jacoco/jacocoTestReport/jacocoTestReport.xml"
        property "sonar.tests", ["src/test/java"]
        property "sonar.test.inclusions", "**/*Test*/**"
        property "sonar.exclusions", '**/com/skydoves/powerspinner/*'
        property "sonar.login", "<sonar_token>"
    }
}

3. Generate local JaCoCo coverage report using Terminal.
gradlew build jacocoTestReport sonarqube

4. Upload JaCoCo report to SonarCloud using Terminal.
.\gradlew sonarqube -Dsonar.host.url=https://sonarcloud.io -Dsonar.login=<SONAR_TOKEN>

The coverage report still shows 0.0%, but the JaCoCo coverage report is 86% successful.

Please help me fix this and update the coverage report to SonarCloud.

PS: This is not for the MAIN BRANCH, but for a long-lived branch.

Thank you.

Hi,

Can you share your analysis log? It’s what’s output from the analysis command. Hopefully, the log you provide will include that command as well.

A side note on your properties:

  • You shouldn’t need to re-specify sonar.host.url on the command line since it’s in your properties (and vice versa - just pick one).
  • sonar.java.coveragePlugin does nothing. You can drop it.
  • sonar.binaries can also be dropped. The actual property name is sonar.java.binaries but it should be picked up automatically from your Gradle env. (The same is actually true for sonar.sources and sonar.tests

 
Ann

Hi,

Thank you for the response. I want to confirm, that do we need to add ‘sonar.jacoco.reportPaths’ property as well ?

Hi,

Again:

 
Ann

Hello,
Please find below the analysis log.

Task :app:sonarqube
No variant name specified to be used by SonarQube. Default to 'debug'
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
Caching disabled for task ':app:sonarqube' because:
  Build cache is disabled
Task ':app:sonarqube' is not up-to-date because:
  Task has not declared any outputs despite executing actions.
No variant name specified to be used by SonarQube. Default to 'debug'
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
file or directory 'C:\Users\kanis\AndroidStudioProjects\<project_name>\app\libs', not found
User cache: C:\Users\kanis\.sonar\cache
Default locale: "en_IN", source code encoding: "UTF-8"
Load global settings
Load global settings (done) | time=1039ms
Server id: 1BD809FA-AWHW8ct9-T_TB3XqouNu
User cache: C:\Users\kanis\.sonar\cache
Load/download plugins
Load plugins index
Load plugins index (done) | time=211ms
Load/download plugins (done) | time=320ms
Loaded core extensions: developer-scanner
Load project settings for component key: '<project_key>'
Load project settings for component key: '<project_key>' (done) | time=192ms
Process project properties
Execute project builders
Execute project builders (done) | time=2ms
Project key: <project_key>
Base dir: C:\Users\kanis\AndroidStudioProjects\<project_name>\app
Working dir: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar
Load project branches
Load project branches (done) | time=189ms
Check ALM binding of project '<project_key>'
Detected project binding: BOUND
Check ALM binding of project '<project_key>' (done) | time=185ms
Load project pull requests
Load project pull requests (done) | time=216ms
Load branch configuration
Load branch configuration (done) | time=0ms
Load quality profiles
Load quality profiles (done) | time=224ms
Load active rules
Load active rules (done) | time=7475ms
Organization key: <organization_key>
Branch name: release-develop, type: long-lived
Load project repositories
Load project repositories (done) | time=431ms
Indexing files...
Project configuration:
  Excluded sources: **/build-wrapper-dump.json, **/com/skydoves/powerspinner/*, **/*Test*/**
  Included tests: **/*Test*/**

1549 files indexed

Task :app:sonarqube
150 files ignored because of inclusion/exclusion patterns
0 files ignored because of scm ignore settings
Quality profile for java: Sonar way
Quality profile for json: SonarQube Way
Quality profile for kotlin: Sonar way
Quality profile for xml: Sonar way
------------- Run sensors on module app
Load metrics repository
Load metrics repository (done) | time=182ms
Sensor JavaSensor [java]
Configured Java source version (sonar.java.source): 8
JavaClasspath initialization
JavaClasspath initialization (done) | time=55ms
JavaTestClasspath initialization
JavaTestClasspath initialization (done) | time=37ms
Server-side caching is not enabled. The Java analyzer will not try to leverage data from a previous analysis.
Using ECJ batch to parse 16 Main java source files with batch size 214 KB.
Starting batch processing.
The Java analyzer cannot skip unchanged files in this context. A full analysis is performed for all files.
100% analyzed
Batch processing: Done.
Did not optimize analysis for any files, performed a full analysis for all 16 files.
No "Test" source files to scan.
No "Generated" source files to scan.
Sensor JavaSensor [java] (done) | time=9867ms
Sensor IaC CloudFormation Sensor [iac]

0 source files to be analyzed
0/0 source files have been analyzed
415 source files to be analyzed

Task :app:sonarqube
Sensor IaC CloudFormation Sensor [iac] (done) | time=1502ms
Sensor C# Project Type Information [csharp]
Sensor C# Project Type Information [csharp] (done) | time=2ms
Sensor C# Analysis Log [csharp]
Sensor C# Analysis Log [csharp] (done) | time=13ms
Sensor C# Properties [csharp]
Sensor C# Properties [csharp] (done) | time=0ms
Sensor SurefireSensor [java]
parsing [C:\Users\kanis\AndroidStudioProjects\<project_name>\app\target\surefire-reports]
Sensor SurefireSensor [java] (done) | time=1ms
Sensor HTML [web]
Sensor HTML [web] (done) | time=6ms
Sensor XML Sensor [xml]

415/415 source files have been analyzed
1040 source files to be analyzed

Task :app:sonarqube
Sensor XML Sensor [xml] (done) | time=8908ms
Sensor Text Sensor [text]

1040/1040 source files have been analyzed

Task :app:sonarqube
Sensor Text Sensor [text] (done) | time=6992ms
Sensor VB.NET Project Type Information [vbnet]
Sensor VB.NET Project Type Information [vbnet] (done) | time=2ms
Sensor VB.NET Analysis Log [vbnet]
Sensor VB.NET Analysis Log [vbnet] (done) | time=14ms
Sensor VB.NET Properties [vbnet]
Sensor VB.NET Properties [vbnet] (done) | time=0ms
Sensor JaCoCo XML Report Importer [jacoco]
Importing 1 report(s). Turn your logs in debug mode in order to see the exhaustive list.
Sensor JaCoCo XML Report Importer [jacoco] (done) | time=10944ms
Sensor Kotlin Sensor [kotlin]
Access to the multi-values/property set property 'sonar.java.binaries' should be made using 'getStringArray' method. The SonarQube plugin using this pro
perty should be updated.
Access to the multi-values/property set property 'sonar.java.libraries' should be made using 'getStringArray' method. The SonarQube plugin using this pr
operty should be updated.

520 source files to be analyzed
224/520 files analyzed, current file: src/main/java/com/<project_path>/ui/profile/activities/ProfileActivity.kt
444/520 files analyzed, current file: src/main/java/com/<project_path>/ui/explore/findfriends/FindFriendsRepeatUserFragment.kt
520/520 source files have been analyzed

Task :app:sonarqube
Sensor Kotlin Sensor [kotlin] (done) | time=107041ms
Sensor KotlinSurefireSensor [kotlin]
parsing [C:\Users\kanis\AndroidStudioProjects\<project_name>\app\target\surefire-reports]
Sensor KotlinSurefireSensor [kotlin] (done) | time=1ms
Sensor CSS Rules [javascript]
No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
Sensor CSS Rules [javascript] (done) | time=4ms
Sensor ThymeLeaf template sensor [securityjavafrontend]
Sensor ThymeLeaf template sensor [securityjavafrontend] (done) | time=3ms
Sensor Serverless configuration file sensor [security]
0 Serverless function entries were found in the project
0 Serverless function handlers were kept as entrypoints
Sensor Serverless configuration file sensor [security] (done) | time=3ms
Sensor AWS SAM template file sensor [security]
Sensor AWS SAM template file sensor [security] (done) | time=1ms
Sensor javabugs [dbd]
Reading IR files from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ir\java
Analyzing 183 functions to detect bugs.
Sensor javabugs [dbd] (done) | time=554ms
Sensor pythonbugs [dbd]
Reading IR files from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ir\python
No IR files have been included for analysis.
Sensor pythonbugs [dbd] (done) | time=0ms
Sensor JavaSecuritySensor [security]
Reading type hierarchy from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ucfg2\java
Read 194 type definitions
Reading UCFGs from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ucfg2\java
07:28:55.1133767 Building Runtime Type propagation graph
07:28:55.1303318 Running Tarjan on 741 nodes
07:28:55.132327 Tarjan found 740 components
07:28:55.1365017 Variable type analysis: done
07:28:55.1373128 Building Runtime Type propagation graph
07:28:55.151276 Running Tarjan on 741 nodes
07:28:55.1524172 Tarjan found 740 components
07:28:55.1553036 Variable type analysis: done
Analyzing 243 ucfgs to detect vulnerabilities.
All rules entrypoints : 0
Retained UCFGs : 0
Taint analysis starting. Entrypoints: 0
Taint analysis: done.
Sensor JavaSecuritySensor [security] (done) | time=1196ms
Sensor CSharpSecuritySensor [security]
Reading type hierarchy from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\ucfg_cs2
Read 0 type definitions
Reading UCFGs from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\ucfg_cs2
No UCFGs have been included for analysis.
Sensor CSharpSecuritySensor [security] (done) | time=0ms
Sensor PhpSecuritySensor [security]
Reading type hierarchy from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ucfg2\php
Read 0 type definitions
Reading UCFGs from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ucfg2\php
No UCFGs have been included for analysis.
Sensor PhpSecuritySensor [security] (done) | time=0ms
Sensor PythonSecuritySensor [security]
Reading type hierarchy from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ucfg2\python
Read 0 type definitions
Reading UCFGs from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ucfg2\python
No UCFGs have been included for analysis.
Sensor PythonSecuritySensor [security] (done) | time=0ms
Sensor JsSecuritySensor [security]
Reading type hierarchy from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ucfg2\js
Read 0 type definitions
Reading UCFGs from: C:\Users\kanis\AndroidStudioProjects\<project_name>\app\build\sonar\ucfg2\js
No UCFGs have been included for analysis.
Sensor JsSecuritySensor [security] (done) | time=0ms
------------- Run sensors on project
Sensor Analysis Warnings import [csharp]
Sensor Analysis Warnings import [csharp] (done) | time=0ms
Sensor Zero Coverage Sensor
Sensor Zero Coverage Sensor (done) | time=1033ms
Sensor Java CPD Block Indexer
Sensor Java CPD Block Indexer (done) | time=79ms
CPD Executor 49 files had no CPD blocks
CPD Executor Calculating CPD for 487 files

CPD Executor CPD calculation finished (done) | time=849ms

Task :app:sonarqube
Analysis report generated in 10922ms, dir size=10 MB
Analysis report compressed in 13326ms, zip size=3 MB
Analysis report uploaded in 1832ms
ANALYSIS SUCCESSFUL, you can find the results at: https://sonarcloud.io/dashboard?id=<project_key>&branch=release-develop
Note that you will be able to access the updated dashboard once the server has processed the submitted analysis report
More about the report processing at https://sonarcloud.io/api/ce/task?id=AYH_lQ0cyflaG8NoJYIR
Analysis total time: 3:25.133 s
:app:sonarqube (Thread[Execution worker for ':',5,main]) completed. Took 3 mins 28.477 secs.
producer locations for task group 0 (Thread[Execution worker for ':',5,main]) started.
producer locations for task group 0 (Thread[Execution worker for ':',5,main]) completed. Took 0.0 secs.

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 3m 37s
51 actionable tasks: 1 executed, 50 up-to-date
Watched directory hierarchies: [C:\Users\kanis\AndroidStudioProjects\<project_name>]

Hi,

I suspect something’s missing here:

Typically what shows up right before “… to see the exhaustive list” is a log line saying that some files were skipped because their paths were unrecognized.

And that boils down to a difference between the paths in the report and the paths that analysis sees.

So you should look there.

 
Ann

Hi,

Could you please elaborate this:

Thank you.

Hi,

If analysis reads your test report but can’t find the files it cites, then you’ll get log messaging like what I talked about. This can be caused by, E.G. generating the reports in a different filesystem/location and then moving them to where analysis happens.

 
HTH,
Ann

Hi,

Thank you for your response. I have tried multiple solutions, but still could not find a fix to this issue. Is there any link to tutorial, which I could follow to find a solution or if you could please guide me to the direct step-by-step solution ?

Hi @ganncamp,

I was able to sync the Code Coverage report with SonarCloud portal. All I had to do was to change paths of sources in sonarqube.

        property "sonar.sources", ["src/test", "src/main/java"]
        property "sonar.tests", "src/test"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.test.inclusions", ["src/test/java"]

In the SonarCloud portal, the Coverage is reflecting as only 14%. When I change the ‘sonar.sources’ property to only ‘src/test’, then the coverage is 82%.

Is there any way, that the ‘COVERAGE’ could be recorded only for the test cases, but not for the complete codebase, whereas, the ‘RELIABILITY’, ‘MAINTAINABILITY’, ‘SECURITY’, ‘SECURITY REVIEW’ and ‘DUPLICATIONS’ could be analyzed for complete code ?

Hi there,

I was able to sync coverage report only for test cases directory, instead of whole project, by adding the below property:

property "sonar.coverage.exclusions", ["**src/main/**"]