Analysing, Inspecting and Generating Code Reports using SonarQube in Android Studio

By | July 1, 2016

SonarQube, formerly known as Sonar, is a platform to analyze code quality.

Installing SonarQube

  1. Go to http://www.sonarqube.org/downloads/ and download the latest release.
  2. Unzip the archive to the directory of your choice.

Starting SonarQube

  1. Go to your downloaded SonarQube.
  2. Open bin/[folder corresponding to your OS]
  3. Open up a terminal window and navigate to bin/[folder corresponding to your OS]
  4. Now you need to execute the correspoding file in the terminal.
  5. For eg: On Mac : sh sonar.sh start (or just double click StartSonar.bat on windows).
    This command will start sonar listening on localhost:9000.

  6. Open a browser and enter localhost:9000. The sonar Dashboard should open.
    It may take some time to Load.

Analyse Your Android Studio Project

Open Your Project’s build.gradle and add like this.

buildscript {
    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.5.0'
        classpath "org.sonarqube.gradle:gradle-sonarqube-plugin:1.0"
    }
}

Now open the module’s build.gradle you want to analyse and add the below script.


apply plugin: "org.sonarqube"

sonarqube {
    properties {
        property "sonar.projectName", "MySampleProject"
        property "sonar.projectKey", "MySampleProject"
        property "sonar.host.url", "http://localhost:9000"
        property "sonar.projectVersion", "1.0"
        property "sonar.language", "java"
        property "sonar.sources", "src/main/"
    }
}


Open your terminal in the Android Studio and execute the below command.


    $ : ./gradlew sonar
 

You can see a series of output in the terminal and at the end You can see where the report is generated…something like this

  
  localhost:9000/projects/MySampleProject.

All Done.

Please send your feedback to coderzheaven@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *