Android Studio Integration with Sonar Cloud - android

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.

Related

How to create a gradle task to run sonar locally

I have configured Sonar for my project and it is working fine, but I would like to create a custom task in the Gradle file to generate reports and run sonar locally directly.
For that, I have tried to create this custom task:
task sonarLocalReport(dependsOn: ['jacocoTestReport', 'lint']) {
group = "Reporting"
description = "Generate Sonar reports for local check"
doLast {
project.extensions.getByName("sonar").ext.extraProperties = [
"sonar.host.url": "http://localhost:9000",
"sonar.login": "KEY"
]
}}
I tried to add extra properties to my sonar task:
sonar {
properties {
property "sonar.projectKey", "App-Android"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectName", "android"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.sources", "${project.projectDir}/src/main/java"
property "sonar.tests", "${project.projectDir}/src/test/java"
property "sonar.junit.reportPaths", "${project.buildDir}/test-results/testDebugUnitTest"
property "sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/jacoco/jacocoTestReport/jacocoTestReport.xml"
property "sonar.android.lint.reportPaths", "${project.buildDir}/reports/lint-results-debug.xml"
}
}
When I run my custom task sonarLocalReport, I get nothing new locally.
I donĀ“t know if this is not the way to add new properties to the sonar task or if I need to run sonar task after adding these new properties.
I found the solution and it will be something like this:
sonar {
properties {
property "sonar.projectKey", "App-Android"
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectName", "android"
...
}
task sonarLocal(type: GradleBuild) {
tasks = ['lint', 'jacocoTestReport']
sonarLocal.finalizedBy('sonar')
sonar {
properties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.login", "YOUR_KEY_GENERATED"
}
}
}
Then you can run your task like ./gradlew app:sonarLocal and it will run after lint, jacocoTestReport and sonar, adding some properties to be able to send data to your local sonar server.

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

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...

Gradle Coverage with Cobertura and Sonar

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?

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