Analisa Code Android dengan Jacoco & Sonarqube

1. Download sonarqube dari https://www.sonarqube.org/downloads/
2. Extract / unzip
3. Buka folder sonarqube-blablabla/bin
4. Buka folder berikutnya sesuai platform (di tutorial ini saya menggunakan linux)
5. Jalankan sonarqube, linux terminal: sh sonar.sh start


6. Buka http://localhost:9000 di browser untuk melihat admin panel
7. Masuk kedalam admin panel dengan username 'admin' dan password 'admin' (tanpa kutip)


8. Buat project baru. Set up project key, display name & token.



9. Setting gradle project

buildscript {
    repositories {
        google()
        jcenter()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.1.1"
        classpath "com.hiya:jacoco-android:0.2"
    }
}


apply plugin: 'com.android.application'
apply plugin: "org.sonarqube"
apply plugin: "com.hiya.jacoco-android"


sonarqube {
    properties {
        property "sonar.projectKey", "com.fd.analyze" // TODO: - Setup your project key
        property "sonar.projectName", "Android Code Analyze" // TODO: - Setup your project name
        property "sonar.host.url", "http://localhost:9000/" // TODO: - Setup your server address
        property "sonar.login", "bdda7079b9f487809961c88f21b3acf3b2d4d0ec" // TODO: - Setup token

        property "sonar.projectVersion", "1.0.0"
        property "sonar.exclusions", "robospice/**/*"

        property "sonar.sources", "src/main/java"
        property "sonar.binaries", "build/intermediates/app_classes/debug"
        property "sonar.java.binaries", "build/intermediates/app_classes/debug"
        property "sonar.tests", "src/test/java, src/androidTest/java"
        property "sonar.java.test.binaries", "build/intermediates/app_classes/debug"

        def unit = fileTree(dir: project.projectDir, includes: ['**/*.exec']).files;
        def ui = fileTree(dir: project.projectDir, includes: ['**/*.ec']).files;
        unit.addAll(ui);
        def files = unit.join(", ");

        property "sonar.jacoco.reportPaths", files
        property "sonar.java.coveragePlugin", "jacoco"
        property "sonar.junit.reportsPath", "build/test-results/testDebugUnitTest"
        property "sonar.android.lint.report", "build/reports/lint-results.xml"
    }
}


10. Execute Task
./gradlew clean connectedAndroidTest test createDebugCoverageReport jacocoTestReport sonarqube


11. Lihat hasil analisa di dashboard

Komentar