Running SonarQube on Android

On gradle.properties:

systemProp.sonar.host.url=http://localhost:9000

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 = "0.7.2.201409121644"
}

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 = project.android.sdkDirectory.getPath() + "/platforms/android-23/android.jar," +
                "build/intermediates/exploded-aar/**/classes.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 "sonar.java.binaries", "build/intermediates/classes/debug"
        property "sonar.java.libraries", libraries

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

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

        property "sonar.jacoco.reportPath", "build/jacoco/testDebugUnitTest.exec" // path to coverage reports
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.junit.reportsPath", "build/test-results/debug" // path to junit reports
        property "sonar.android.lint.report", "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 'com.android.support:support-annotations:23.4.0'
androidTestCompile 'com.android.support.test:runner:0.4.1'
androidTestCompile 'com.android.support.test:rules:0.4.1'

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