Running SonarQube on Android


On build.gradle (project-level), within buildscript | dependencies:

classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.0.1"

Create a file named jacoco.gradle containing:

apply plugin: 'jacoco'

jacoco {
    toolVersion = ""

Create a file named sonarqube.gradle containing:

apply plugin: "org.sonarqube"

// noinspection is used to remove some "warnings" from Android Studio
sonarqube {
    //noinspection GroovyAssignabilityCheck
    properties {
        /* SonarQube needs to be informed about your libraries and the android.jar to understand that methods like
         * onResume() is called by the Android framework. Without that information SonarQube will very likely create warnings
         * that those methods are never used and they should be removed. Same applies for libraries where parent classes
         * are required to understand how a class works and is used. */
        def libraries = + "/platforms/android-23/android.jar," +

        property "sonar.projectVersion", android.defaultConfig.versionName

        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.sources", "src/main/java,src/main/res" // first defines where the java files are, the second where the xml files are
        property "sonar.binaries", "build/intermediates/classes/debug"
        property "sonar.libraries", libraries
        property "", "build/intermediates/classes/debug"
        property "", libraries

        property "sonar.tests", "src/test/java" // where the tests are located
        property "", "build/intermediates/classes/debug"
        property "", libraries

        property "sonar.scm.provider", "git"

        property "sonar.jacoco.reportPath", "build/jacoco/testDebugUnitTest.exec" // path to coverage reports
        property "", "jacoco"
        property "sonar.junit.reportsPath", "build/test-results/debug" // path to junit reports
        property "", "build/outputs/lint-results-debug.xml" // path to lint reports

import org.gradle.internal.os.OperatingSystem;

task sonarComplete(type: Exec) {
    workingDir "./"
    def command = "../gradlew"
    if (OperatingSystem.current().isWindows()) {
        command = command + ".bat"
    commandLine command, "clean", "assembleDebug", "lintDebug", "testDebugUnitTest", "sonarqube"

On build.gradle (module-specific) | android | buildTypes | debug

testCoverageEnabled true

Still on build.gradle (module-specific) | After the android block

apply from: '../jacoco.gradle'
apply from: '../sonarqube.gradle'

Still on build.gradle (module-specific), add dependencies:

androidTestCompile ''
androidTestCompile ''
androidTestCompile ''

testCompile 'org.mockito:mockito-core:2.0.57-beta'
testCompile "org.robolectric:robolectric:3.1.1"