Published in


Have code quality analysis in your Android Project

Developers should be proud of doing good code. But how can a developer recognises if it’s good enough?

And if a tool can help you with:

  1. Percentage/Number of duplication of code
  2. Size and Complexity of your code
  3. Possible issues/code smell in your code
  4. Test coverage of your Unit Tests

Most of you already know that SonarQube Server shows you all of these helpful analytic results, therefore this article has the goal to show you the importance of having a code quality reports and how you can configure it.

(example of a SonarQube Server dashboard, showing code qualities report)

What is SonarQube?

SonarQube is a tool that “provides the capability to not only show health of an application but also to highlight issues newly introduced. With a Quality Gate in place, you can fix the leak and therefore improve code quality systematically”

Important SonarQube measures


SonarQube issues can be classified in these types:


  • High probability to impact the behaviour of the application like memory leaks, null pointers, etc
  • Should be immediately solved
  • Should make your build failing in your Continuous Integration system


  • High probability to impact the behaviour of the application but less than Blocker issues, like wrong visibility of a setter, wrong handling caught exception, etc
  • Should be immediately reviewed
  • Should notify the team that is number of critical issues is increasing

Quality flaw which can highly impact the developer productivity: duplicated blocks, unused parameters, etc”

“Quality flaw which can slightly impact the developer productivity: lines should not be too long, “switch” statements should have at least 3 cases,etc”

Neither a bug nor a quality flaw, just a finding, like a missing TODO in code.

SonarQube always gives you an explanation about each issue, a “Noncompliant Code Example” and most of the times a proper solution.

More info about issues here.

You can also define your own rules with what you think that it’s acceptable number of issues in your project, going to SonarQube > Quality Gates > “Select your project “.

How to configure it?

1. SonarQube server installation

  1. Download SonarQube here.
  2. Extract your sonarqube-{version}.zip
  3. Inside $SONAR_DOWNLOAD_DIR/sonarqube-{version}/bin you can see all the SO distributions:
> cd /$SONAR_DOWNLOAD_DIR/sonarqube-{version}/bin
> ls -l
drwxr-xr-x@ 3 x x 170 Oct 24 16:10 linux-x86–32
drwxr-xr-x@ 3 x x 170 Oct 24 16:10 linux-x86–64
drwxr-xr-x@ 3 x x 204 Nov 8 12:27 macosx-universal-64
drwxr-xr-x@ 3 x x 306 Oct 24 16:10 windows-x86–32
drwxr-xr-x@ 3 x x 306 Oct 24 16:10 windows-x86–64

Please, select your distribution and follow the installation for Linux or Windows. If you are confortable with Linux, just create a symbolic link from /$SONAR_DOWNLOAD_DIR/sonarqube-{version}/bin/<linux-distribution>/

OR just make a quick test running directly:

> cd $SONAR_DOWNLOAD_DIR/sonarqube-{version}/bin/<linux-distribution>
> ./ start
Starting SonarQube...
Started SonarQube.

Now you should see an empty dashboard in your http://localhost:9000

2. Add Sonar Qube Scanner Gradle Plugin

  1. Add the plugin in your global build.gradle
apply plugin: 'org.sonarqube'

2. Add in your project build.gradle

buildscript {
repositories {
maven {
url ""
dependencies {
classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6'

You can check other latest release version here.

4. Sonar Project Configuration

4.1 Base configuration

  1. Go to your Android Project and add in your app/ file, a project configuration like this example:
properties {
property "sonar.projectKey", "projectName"
<--more keys-->

Example of more keys

sonar.projectVersion=<you can read your branch here with gradle>#credential by default
#your base project directory
#define a key for your project. Everytime that you push a new version of code, SonarQube will compare it according yout projectKey (usually this key doesn't change along of the time)
#name that will appear in the list of your projects in sonarqube
##project sources
#directory of project's binaries. Look inside your build/intermediates/class dir
#(optional),directories if you want to exclude something

Be aware that depending of your buildVariants and flavors configuration, those directories change for each project. Please run ./gradlew build and check your directories to confirm your project directories.

2. Testing configuration

If you have Unit Tests in your project and you want to see your test coverage/number of your unit tests, these are the keys to add in

##testing area
#if you have Jacoco configured in your project (this will be necessary to show the code coverage)

5. Push your reports to SonarQube

  • Apply sonarqube-gradle-plugin in your gradle config.
  • Run your tests ./gradlew clean testUnitTest
  • Push your reports in your SonarQube server
> ./gradlew sonarqube

This last step can easily be added to your Continuos Integration process, to avoid repeating it all the time. More information here.

If you are using Kotlin in your project, please add sonar-kotlin plugin to your SonarQube.


“Quality is an investment in the future”



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store