Android Sonarqube Sample

1. Introduction

First at all, You need to configure the grade variables of Test project,
then the test unit cases had worked with SonarQube Framework.

All Testing are working using Mockito 1.10.* dependency

Add dependencies on gradle config project file

  • Permission library (for request permissions at Android 5)
  • SharePreferenceLib library (save internal memory)
  • Glide library for structure data
  • Junit, mockito, hamcrest & powermock Libraries

You need to add the following 'Dependency Libraries' version:

glideVersion = '3.6.1'
junitVersion = '4.12'
mockitoVersion = '1.10.19'
powerMockito = '1.6.2'
hamcrestVersion = '1.3'

2. Install SonarQube

Add maven repository of sonarqube and the classpath into gradle-project, like that:

// build.gradle
maven {
        url ""
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.3"

On App module, apply jacoco plugin to generate a test report, wich is going to be upload in Sonar services:

// app_build.gradle
apply plugin: "org.sonarqube"

apply plugin: 'jacoco'
jacoco {
    toolVersion = ""

3. Configure Android Project

Create a task for generate a jacoco report format with gradle. Worked with flavors into Android projects:

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport'])

Also You need to create a task from connection with the sonar, these properties:

sonarqube {
    properties {
        property "sonar.projectKey", "sonar_qube_sample"
        property "sonar.projectName", "SonarQube Sample Ver. 1"
        property "sonar.projectVersion", "1.0.f"
        property "", "http://localhost:9000"

Now you need to config the tests: Enabling the coverage testing, you could disable abort on error check of code with critics issues, because these errors you would analyze it from SonarQube Dashboard, and don't forget add your jacoco reports.

android {
    buildTypes {
        debug {
            testCoverageEnabled = true
    lintOptions {
        abortOnError false
    testOptions {
        unitTests.all {
            jacoco {
                includeNoLocationClasses = true

Make a new gradle task to run it quickly and efficiency:

task exportTestRultToSonarqube(dependsOn: ['jacocoTestReport','sonarqube']){

In your terminal:

>gradlew exportTestRultToSonarqube

4. Start SonarQube Server

Sonarqube service version 7.2. depends on you SO, you need to run sh or exe bash:

sh start

On Web browsers, open localhost:9000

Wait for a bit moment while it will be finishing the tasks.

5. Export Metrics

Run Gradle task

On sonar dashboard you could go over your projects, issues, rules, quality metrics, and more engineering features
If all components works well, execute a test task, and send it with exportTestResultToSonarqube task:

./gradlew exportTestResultToSonarqube

Confirm message:

6. View Your Metrics

On dashboard, you have a analyzed new project

Into the project, Look four metric: count of bug, security vulnerabilities, percentage of testing coverage, and percentage of duplicate codes:

You could verificate the id project in url on web browser:

On Sonarqube project dashboard, thats look like:

7. Stop SonarQube Server

Finally, if you need to stop sonar service, run the sentence:

sh stop

8. License

by Romell Domínguez

Copyright 2018 Romell D.Z.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,


See the License for the specific language governing permissions and limitations under the License.