Integrate SonarQube for Android Application Development
Hello Android developers,
We all want our code to be of good quality so that it will be easy to maintain for a longer time. How much ever we try to write the best possible code still we find the scope of improvement. So we end up using code quality check tools like SoarQube, SonarLint, AndroidLint etc.
Android Studio already come with AndroidLint which can check the code quality and allow developers to improve them. Even there is a plugin for SonarQube which can suggest possible improvement per class straight in Development environment. I was happy using this plugin until my Project Manager started asking for the report which will list critical, major and trivial issues in the code to keep track for which I thought to generate the SonarQube report by installing on our server.
Here I am trying to explain how to generate SonarQube report using SonarQube
- Download SonarQube from https://www.sonarqube.org/downloads/
- Downloaded version will come in Zip, just unzip it
- Goto /sonarcube/bin/<platform-folder>
- Hit startsonar.bat to start running SonarQube on server
- Now open http://localhost:9000 to see admin panel of SonarQube
- Login to user panel using “admin” for username and password
YAy!! SonarQube server is successfully running on your machine. Next step is to make some changes in build.gradle files on your Android Project
Open your project level build.gradle file. Add classpath dependencies just below your build path just like this
Hit “Sync Now”
Make some changes to build.gradle to module level gradle i.e the one present in /app folder
Add sonar plugins at the top of file just below android plugin
apply plugin: 'com.android.application'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco-android'
It’s Android version of jacoco, read more about jacoco here
Now Project is ready to accept SonarQube Configuration. Now just pass the configuration and sync project
def libraries = project.android.sdkDirectory.getPath() + "/platforms/android-23/android.jar"/* + ", build/intermediates/exploded-aar*//**//*classes.jar"*/
property "sonar.sources", "src/main/java"
property "sonar.binaries", "build/intermediates/classes/debug"
property "sonar.libraries", libraries
property "sonar.tests", "src/test/java, src/androidTest/java"
property "sonar.java.test.binaries", "build/intermediates/class/debug"
property "sonar.java.test.libraries", libraries
property "sonar.jacoco.reportPath", "build/jacoco/testDebugUnitTest.exec"
property "sonar.java.coveragePlugin", "jacoco"
property "sonar.junit.reportsPath", "build/test-results/debug"
property "sonar.android.lint.report", "build/outputs/lint-results"
just copy -paste above code to bottom of build.gralde(app) and sync project. Hope it sync without any problem :P :D
AND you are done..Congratulations!! Now all the best for the Sonar Report, hope you get less count :D
Now run the gradle command to generate the report. If you using gradle wrapper, hope you are using we can use gradlew
$gradlew clean test jacocoTestReport sonarqube -Dsonar.host.url=http://localhost:9000 — info -stacktrace
It will run that gradle command and take couple of minutes and generate a report for you.
Finally, you can view the report by entering
Hope URL is self explanatory..
Please comment if you have any doubts..Till then
Keep your code clean :)