Gradle Coverage with Cobertura and Sonar - android

I am trying to configure our Android Gradle build to publish code coverage results (generated by Cobertura) to Sonar.
We have a modular project structure where each module is built as an apk-lib or apk. The project is structured like so:
build.gradle (top-level gradle build script)
settings.gradle (top-level gradle settings)
android_lib.gradle (used by module's build.gradle to tell it to build module as apk-lib)
android_app.gradle (as above but apk)
|
|__module1
|____build.gradle
|
|
|__module2
|____build.gradle
|
|
|__module3
|____build.gradle
|
|__etc...
I have had some luck generating the coverage using Cobertura. I can see the html and xml coverage reports in build/reports/cobertura/coverage.xml. This is how I've configured it:
android_lib.gradle:
apply plugin: 'com.android.library'
apply plugin: 'net.saliman.cobertura'
cobertura {
coverageFormats = ['html', 'xml']
coverageIgnoreTrivial = true
coverageIgnores = ['org.slf4j.Logger.*']
coverageReportDir = new File("$buildDir/reports/cobertura")
}
android {
... //android specific config goes here
}
After I run gradlew cobertura the coverage reports are generated and exist in build/reports/cobertura/coverage.xml
Now I would like to be able to publish these results to a local Sonar (v4.3) server I have running.
I have tried applying the sonar-runner plugin to my android_lib.gradle file but I receive lots and lots of errors and do not see any coverage results in Sonar.
android_lib.gradle:
apply plugin: 'sonar-runner'
sonarRunner {
sonarProperties {
//property "sonar.projectKey", "coverage-example"
//property "sonar.projectName", "Coverage Example"
//property "sonar.projectVersion", "1.0"
property "sonar.sources", "src/main/java"
property "sonar.binaries", "build"
//property "sonar.test", "src/androidTest/java"
property "sonar.profile", "Sonar way"
property "sonar.language", "java"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.dynamicAnalysis", "reuseReports"
property "sonar.junit.reportsPath", "build/test-results/debug"
property "sonar.cobertura.reportPath", "build/reports/cobertura/coverage.xml"
property "sonar.java.coveragePlugin", "cobertura"
property "sonar.host.url", "http://localhost:9000"
}
}
Is there something I am missing, has anyone had luck with this setup?

Related

Android Studio Integration with Sonar Cloud

I'm getting an Authorisation Error while executing "gradlew sonarqube"
I've added these properties in app level gradle
sonarqube {
properties {
property 'sonar.sourceEncoding', 'UTF-8'
property 'sonar.projectName', 'ToDoTest'
property 'sonar.projectKey','967dcfc8faf76abdbb560889d5087245faa69a5b'
property "sonar.language", "kotlin"
property "sonar.sources", "app/src/main/java"
property "sonar.binaries", "app/build"
property "sonar.java.binaries", "target/classes,app/build/tmp/kotlin-classes"
property "sonar.tests", "app/src/test/java, app/src/androidTest/java"
property "sonar.java.test.binaries", "app/build/intermediates/classes/debug"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.junit.reportsPath", "app/build/test-results/testDebugUnitTest"
property "sonar.android.lint.report", "app/build/reports/lint-results.xml"
property "sonar.jacoco.reportPath","app/build/jacoco/testDevDebugUnitTest.exec"
}
}
These I kept in gradle.properties
systemProp.sonar.host.url=https://sonarcloud.io
systemProp.sonar.login=967dcfc8faf76abdbb560889d5087245faa69a5b
Getting this exception in terminal;
* What went wrong:
Execution failed for task ':app:sonarqube'.
Not authorized. Please check the properties sonar.login and sonar.password.
The configuration you posted is missing the sonar.organization required property.
Btw, all the other properties in sonarqube { properties { ... } } in your posted example should not be necessary, the SonarQube plugin for Gradle should detect and include them automatically in the analysis.

How to add SonarQube Plugin in Build.Gradle and take the reports of current project

I tried downloading SonarQube and followed each steps based on this link, SonarQube Setup and add SonarQube plugins in build.gradle
Was able to execute SonarQube from Command but while I am adding the plugins of SonarCube in Build.Gradle it is showing an error like this when I tried to sync a project,
Error#25: all buildscript {} blocks must appear before any plugins {} blocks in the script
But I have added a plugin and properties of SonarQube in build.gradle,
plugins {
id "org.sonarqube" version "7.2.1"
}
and adding properties of SonarQube,
sonarqube {
properties {
property "sonar.projectName", "MyApplication2"
property "sonar.projectKey", "SQKey"
property "sonar.sources","src/main/java"
property "sonar.language","java"
property "sonar.sourceEncoding", "UTF-8"
}}
Android Studio Version is, 3.1.3 and
Gradle Version is, 4.4
and, downloaded SonarQube Version is, 7.2.1
My Question here is, Is SonarQube 7.2.1 not compatible for Gradle Version 4.4?
Please see the Image below (SonarQube is Up) in command Prompt,
Any idea regarding how to generate the reports of my currrent project using sonarqube and executing a command in cmd prompt?
Error # 33: only buildscript {} and other plugins {} script blocks are allowed before plugins {} blocks, no other statements are allowed
Please find the below build.gradle file,
buildscript {
repositories {
jcenter()
maven { url 'https://maven.fabric.io/public' }
maven {
url 'https://maven.google.com/'
name 'Google'
}
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:2.1.0'
classpath 'io.fabric.tools:gradle:1.+'
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6'
}
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: "org.sonarqube"
repositories {
maven { url 'https://maven.fabric.io/public' }
}
apply plugin: 'com.google.gms.google-services'
plugins {
id "org.sonarqube" version "7.2.1"
}
sonarqube{
properties{
property "sonar.projectName", "Example 16-8"
property "sonar.projectKey", "example 16-6-key"
property "sonar.sources","src/com.example.project"
property "sonar.language","java"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.jacoco.reportPath",
"build/jacoco/testDebugUnitTest.exec"
// property "sonar.exclusions", "src/main/java/com/foo/Foo.java"
}}
allprojects {
repositories {
jcenter()
google()
}
}
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
}
What mistake I am doing here in above build.gradle file and how to run sonarQube and generate the reports of my current project?
And, please find the below Image - When I add http://localhost:9000/ in my browser, it does not show any of my Android Projects. Please refer this Image for reference how SonarQube islooking in my browser,
Tried to create Gradle.properties file to add this lines,
systemProp.sonar.host.url=http://localhost:9000
systemProp.sonar.login=admin
systemProp.sonar.password=admin
Opened Android Studio Terminal and executed this,
.\gradlew sonarqube
Please see below Image, I am getting Sonar Qube Exception when I tried to execute the command on Android Studio Terminal (Bottom).
The error #33 you get comes from the strict syntax that must be followed when using plugins{} block, which is described in Gradle documentation here : https://docs.gradle.org/current/dsl/org.gradle.plugin.use.PluginDependenciesSpec.html (see "Strict syntax" section)
In your build.gradle script, you have added the "plugins{}" block after some other directives, which is not allowed.
I guess the other error #25 comes from the same reason.
You should try to rewrite your script and move the 'buildscript' block first, then the 'plugins' block, and then other parts of your scripts
hope this helps.
sonarqube {
properties {
property "sonar.host.url", "https://sonar-url"
property "sonar.login", "login - token "
property "sonar.projectName", "hello-world-service"
property "sonar.projectKey", "hello-world-service"
property "sonar.coverage.jacoco.xmlReportPaths", "$buildDir/reports/jacoco/test/jacocoTestReport.xml"
property "sonar.junit.reportPaths", "$buildDir/test-results/test/"
property "sonar.exclusions", "**/xmldefs/**,**/spec/api.json"
property "sonar.cpd.exclusions", ", **/persistence/entity/**"
property "sonar.dynamicAnalysis", "reuseReports"
property "sonar.clover.reportPaths", "**/build/clover/clover.xml"
property "sonar.java.coveragePlugin", "clover"
property "sonar.sources", "src/main/kotlin"
property "sonar.tests", "."
property "sonar.test.inclusions", "**/*Test*/**"
}
}
// Build should fail if coverage less than minimum.
jacocoTestCoverageVerification.dependsOn jacocoTestReport
build.finalizedBy jacocoTestCoverageVerification
// Add this in your build.gradle

Sonar Jacoco for Kotlin Setup not generating code coverage

I'm trying to do Sonar Setup with Jacoco for Kotlin to generate Code Coverage report but it's not showing any code coverage. While checking Sonar Console it showing following error. Anyone has faced this issue before, any suggestion what could be miss.
Meta info
plugin using sonarqube version "2.6.1"
gradleVersion = '3.0.1'
kotlinVersion = '1.2.21'
Sonarqube version = Version 6.7.1 (build 35068) - LGPL v3
Frustrating part is, my setup project generating blank code coverage report :(. PFA.
Edit : Please find project structure snap.
I'm adding sonar & Jacoco gradle file setup I'm using to generate sonar-matrix report.
Here is sonar.gradle file:
sonarqube {
properties {
property "sonar.projectKey", "jacoco.sonar.test"
property "sonar.projectName", "Sonar Jacoco Test"
property "sonar.projectVersion", "1.1"
property "sonar.java.source", "7"
property "sonar.android.lint.report", "build/outputs/lint-results.xml"
property "sonar.java.binaries", "build/tmp/kotlin-classes"
property "sonar.java.test.binaries", "build/intermediates/classes/test/,build/tmp/kotlin-classes/devDebugUnitTest"
property "sonar.tests","src/test/java"
property "sonar.sources","src/main/java"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.jacoco.reportPaths","build/jacoco/testDevDebugUnitTest.exec"
property "sonar.junit.reportsPath","build/test-results/testDevDebugUnitTest"
}
}
and here is jacoco.gradle file
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.9"
reportsDir = file("${project.projectDir}/app/build/reports")
}
task jacocoTestReport(type: JacocoReport, dependsOn: "app:testDevDebugUnitTest") {
group = "Reporting"
reports {
xml.enabled = true
html.enabled = true
}
def fileFilter = ['**/R.class',
'**/R$*.class',
'**/BuildConfig.*',
'**/*$ViewInjector*.*',
'**/*$ViewBinder*.*',
'**/*$MembersInjector*.*',
'**/Manifest*.*',
'**/*Test*.*',
'android/**/*.*']
classDirectories = fileTree(
dir: "${project.projectDir}/app/build/intermediates/classes/dev",
excludes: fileFilter
) + fileTree(
dir: "${project.projectDir}/app/build/tmp/kotlin-classes/devDebug",
excludes: fileFilter
)
// sources
sourceDirectories = files(["${project.projectDir}/app/src/main/java"])
executionData = files("${project.projectDir}/app/build/jacoco/testDevDebugUnitTest.exec")
}
Following gradle commands I'm using to generate Jacobo report & then soar report.
./gradlew clean jacocoTestReport sonarqube
I observed following I'm getting, must be issue some path.
Coverage information was not collected. Perhaps you forget to include
debug information into compiled classes?
I'm sorry, if this is looking bit length; but this is best I found to summaries in one place. Please also note I tried similar setup with Java class instead Kotlin it's generating report with code coverage.
If you're using the android test orchestrator this is likely the problem.
I had the same issue today and after disabling the android test orchestrator the coverage is working again.
Bug report: https://issuetracker.google.com/issues/72758547
I'm not sure how Android Kotlin builds are configured, but in my Android Java build.gradle I had to comment out the test orchestrator like so:
android {
...
testOptions {
// temporarily disable the orchestrator as this breaks coverage: https://issuetracker.google.com/issues/72758547
//execution 'ANDROID_TEST_ORCHESTRATOR'
...
}
}
There are few things you need to do to make it work for kotlin.
Ensure your sonarqube > 7.5
If your sonarqube ver < 7.5, have the admin install sonar-jacoco plugin. Check the plugin compatibility version if sonarqube < 5.5.
plugins {
id "jacoco"
id "org.sonarqube" version "2.7.1"
}
Follow this url: https://community.sonarsource.com/t/coverage-test-data-importing-jacoco-coverage-report-in-xml-format/12151
Add properties in your build.gradle for it to search for jacoco results.
property 'sonar.coverage.jacoco.xmlReportPaths', "${buildDir}/reports/jacoco/test/jacocoTestReport.xml"
Follow this url for properties: https://docs.sonarqube.org/7.5/analysis/analysis-parameters/

SonarQube integration with gradle

I am integrating Sonar qube with gradle. I have mentioned plugin and required properties in app level build.gradle file.But command execution failed with exception. Please clarify what else required?
Build.gradle :
plugins {
id "org.sonarqube" version "2.6.1"
}
sonarqube {
properties {
property "sonar.host.url", "http://192.168.22.12:9000/"
property "sonar.projectName", "ourValues-Android"
property "sonar.projectKey", "1234567"
property "sonar.projectVersion","8.0"
property "sonar.sources","src/main/java"
property "sonar.exclusions","build/**,**/*.png"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.report.export.path", "sonar-report.json"
property "sonar.issuesReport.json.enable", "true"
property "sonar.import_unknown_files", true
property "sonar.android.lint.report", "./build/outputs/lint-
results.xml"
}
}
Exception is when running command gradlew sonarqube :
Execution failed for task ':app:sonarqube'.
You must define the following mandatory properties for 'Unknown': sonar.projectKey, sonar.sources
Please check the structure of project...

How to update project using gradle automatically when executing sonar-runner?

I am using:
Android Studio 1.2
SonarQube 5.1
...and I want to force the project build of Android app before passing SonarQube, maybe modifying gradle configuration file.
I have added the next lines to build.gradle file:
apply plugin: "sonar-runner"
sonarRunner {
sonarProperties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.analysis.mode", "incremental"
property 'sonar.sourceEncoding', 'UTF-8'
property 'sonar.language', 'java'
property "sonar.sources", "src/main"
property 'sonar.profile', 'Android Lint'
property 'sonar.import_unknown_files', 'true'
}
}
subprojects {
sonarRunner {
sonarProperties {
property "sonar.sources", "src/main"
}
}
}
What should I change in order to achieve the desired behaviour?
Simply add a new line to module build.gradle file, just after sonarRunner task declaration:
sonarRunner {
tasks.sonarRunner.dependsOn build // <- This new line
sonarProperties {

Categories

Resources