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 {
Related
I have a multi-flavored, multi-build-typed android project and I want to integrate the NewRelic plugin. But I have to apply it only for one of the customers, thus only for one product flavor.
NewRelic uses instrumentation and the plugin would generate code in other flavors if I applied the plugin there, and that is not permitted for us.
So my question is: How can I use the apply plugin: something command in the gradle file to be applied to only one of my flavors?
Use this code:
if (!getGradle().getStartParameter().getTaskRequests()
.toString().contains("Develop")){
apply plugin: 'com.google.gms.google-services'
}
getGradle().getStartParameter().getTaskRequests().toString() returns something like [DefaultTaskExecutionRequest{args=[:app:generateDevelopDebugSources],projectPath='null'}] so as stated in the comments Develop must start with an uppercase.
Tried different solutions, but none of them worked for me. This is what I came up with and seems to work as far as I tested:
build.gradle
productFlavors {
someFlavorWithGoogleGcm {
dimension "type"
applicationId "com.example.withGcm"
ext.useGoogleGcm = true
}
someFlavorWithoutGoogleGcm {
dimension "type"
applicationId "com.example.withoutGcm"
}
}
And outside the configuration, inside the build.gradle file:
android.productFlavors.each { flavor ->
if (getGradle().getStartParameter().getTaskRequests().toString().toLowerCase().contains(flavor.name) && flavor.ext.useGoogleGcm) {
println("Building flavor with Google GCM [${flavor.name}] - applying plugin")
apply plugin: 'com.google.gms.google-services'
}
}
I simply used apply plugin: 'com.google.gms.google-services' inside the flavor in app level build.gradle and it works just fine.
productFlavors {
..... your other flavors ....
yourFlv {
....
....
apply plugin: 'com.google.gms.google-services'
}
}
No extra step needed.
Define variable - def fl
Initialize variable in you Flavours (and/or builds)
productFlavors {
freeFlavour {
(...)
fl = "free"
}
paidFlavour {
(...)
fl = "paid"
}
}
Use if statement -
if (fl == "free") {
apply plugin: something
}
I found a solution, but it is not the best so far.
So I'm not sure anymore, that what I wanted to do initially is possible.
The gradle file evaluation and the choosing of the right flavor and build type is in different phases of the gradle build, so what I've done is:
I use a build parameter from the command line. When that paramerer is true, I apply the plugin, when it is not even there, I also apply it (for IDE build).
I use Jenkins, so I could write that parameter in the build job.
build.gradle file:
// First we have to attach a task to the project, which will be running first
gradle.projectsEvaluated {
preBuild.dependsOn(applyNewRelicByProperty)
}
// Then check on the parameter, which comes from the command line
task applyNewRelicByProperty {
if(!project.hasProperty('compileNewRelic')) {
// NewRelic on: 'compileNewRelic' property not set, so running from IDE.
apply plugin: 'newrelic'
} else if(project.hasProperty('compileNewRelic') && project.getProperties().get('compileNewRelic').equals('true')) {
// NewRelic on: 'compileNewRelic' property is set and it is 'true'.
apply plugin: 'newrelic'
} else {
// NewRelic off
println("No NewRelic")
}
}
And you have to run the gradle build by this:
assembleYourApp -PcompileNewRelic=true
After upgrading to Gradle 4.2, the approach by Tarps stopped working, so I ended up combining it with Erik's answer.
Set ext.useGoogleGcm for each flavour in your build.gradle:
productFlavors {
a {
...
ext.useGoogleGcm = true
}
b {
...
ext.useGoogleGcm = false
}
}
Then at the bottom of the file:
apply plugin: 'com.google.gms.google-services'
afterEvaluate {
android.productFlavors.each { flavor ->
tasks.matching {
it.name.contains('GoogleServices') && it.name.contains(flavor.name.capitalize())
}*.enabled = flavor.ext.useGoogleGcm
}
}
In the output for your assembleRelease task, you should see tasks with "GoogleServices" in the name have run for those that are true and skipped for those that are false. If your build complains about toCapitalize, you could use toLowerCase on both it.name and flavor.name.
In the case of the Google Services Gradle plugin, the following works.
apply plugin: 'com.google.gms.google-services'
afterEvaluate {
tasks.matching { it.name.contains("GoogleServices") && !it.name.contains(yourFlavorName) }*.enabled = false
}
where yourFlavorName is a capitalised string containing the name of your flavor that must apply the plugin; all other flavors don't use the plugin.
Note that the plugin is still applied to other flavors; this solution just disables the *GoogleServices* task(s) for them. That assumes that the Google Services Gradle plugin only applies changes by adding a task containing substring "GoogleServices", which might not work in the future.
To check that this works, you can check the dependencies, e.g. like so:
./gradlew app:dependencyInsight --configuration yourFlavorNameDebugCompileClasspath --dependency google | grep -i services
That should show some dependencies for yourFlavorName but not for other flavor names:
./gradlew app:dependencyInsight --configuration otherFlavorNameDebugCompileClasspath --dependency google | grep -i services
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.
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
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...
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?