How we can integrate sonarqube in android studio? I have come across static code analysis using sonarqube. Explain how we can achieve that. There are many link available to integrate sonar-runner and sonarqube but either outdated or not sufficient to get the job done.
-
2Install plugin for IntelliJ Idea – Alexander Goncharenko May 05 '17 at 19:23
-
2Check out [this](https://androidlearnersite.wordpress.com/2017/02/21/integrating-and-understanding-sonarqube-in-android/) for up-to-date detailed explanation on how to integrate and use sonarqube in android – Android Developer Jun 16 '17 at 05:16
-
@BhuvneshVarma great brother. – Gurvinder Singh Jun 16 '17 at 07:18
5 Answers
Sonarqube is static code analyzer tool on server side. It is very useful to write clean and quality code. You should have sonarqube running on localhost or server. There create a new project giving name and unique id, this name and unique we will use to identify us to the server along with our username and password. Few things need to be set up on server side like-
- Create a user.
- Create new project with unique id.
Now in Android studio we are going use gradle sonarqube command to analyze our project with sonarqube.
There are following steps need to be covered before running gradle sonarqube command-
- First we need to have gradle installed on our machine.
- (Optional) To install sonarqube plugin in android studio. Go to-
File -> Settings -> Plugins -> then type sonarqube and click on Browse repositories at the bottom.
Open build.gradle file, add plugin sonarqube.org and add following properties-
apply plugin: "org.sonarqube" sonarqube { properties { property "sonar.projectName", "MyProject" property "sonar.projectKey", "com.example.myproject" property "sonar.host.url", "http://192.114.1.1:9000" property "sonar.language", "java" property "sonar.sources", "src/main/" property "sonar.login", "username" property "sonar.password", "password" } }
Open project gradle file and in dependencies add-
dependencies { classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.1" }
And in repository add-
allprojects { repositories { maven { url "https://plugins.gradle.org/m2/" } } }
Now on Android studio side your setup is done, run the command- gradle sonarqube to run the analysis.
If working in team and want to create different branches for all developers, run command- gradle sonarqube -Dsonar.branch={YouName}

- 2,157
- 3
- 15
- 21
-
While I appreciate this answer, somehow, I can't get it to work and get this message `Invalid value for sonar.java.test.binaries` – theAnonymous Jan 24 '18 at 03:21
-
-
-
Execution failed for task ':compileDebugJavaWithJavac'. although the java_home is configured – Mohsin Apr 25 '18 at 12:44
-
@mohsin you need to check it on java 8, on java 10 I was also getting the same issue. – Anand Kumar Jha Dec 14 '18 at 13:38
-
when i run this -gradle sonarqube getting this error : "Could not find method google() for arguments [] on repository container" – Silambarasan Damodaran Apr 01 '20 at 10:01
-
* What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'. > javax/xml/bind/JAXBException I am gettin gthis error while running gradlew sonarqube. – Akshay Apr 09 '20 at 06:32
If you are using gradle 3.X follow this steps:
1.- Download and run on localhost Sonarqube from this: https://www.sonarqube.org/downloads/
2.- At the gradle.properties:
systemProp.sonar.host.url=http://localhost:9000
systemProp.sonar.login=XXXXXXXXXXXXXXXX (put your token)
3.- At the build.gradle(Module:app) inside repositories:
maven {
url "https://plugins.gradle.org/m2/"
}
And inside dependencies:
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.7"
And finally outside buildscript:
apply plugin: "org.sonarqube"
4.- Run the command: gradle sonarqube
5.- wait 5 minutes after build successfull to see the result report

- 171
- 2
- 2
-
You should add systemProp.sonar.projectKey=
in gradle.properties – Marco Fantasia Feb 05 '21 at 09:10
Integrating Sonarqube can be a bit hard, I wrote a Gradle plugin for Android to make it easier.
Here is an article about it: https://proandroiddev.com/android-analyzer-df0e4d80dc74
Here is the plugin: https://github.com/pinchbv/android-analyzer

- 1,043
- 1
- 8
- 20
If anyone is getting scm provider autodetection failed they can disable scm by writing:
property "sonar.scm.disabled", "True"
in there properties section in build.gradle file
FOR KMM USE setProperty(key, value)
sonarqube {
properties {
setProperty("sonar.projectName", "MyProject")
}
}

- 55
- 3