Search code examples
androidintellij-ideaandroid-studiosonarqubesonar-runner

SonarQube plugin with Android Studio


Has anyone succeeded in getting either the SonarQube Community IntelliJ plugin OR the 'official' SonarQube IntelliJ plugin to show results of static code analysis in Android Studio projects?

The second of these requires Maven but the first of these is supposed to be agnostic.

Somehow I managed to run a sonarRunner on my project in the past but I can't manage to do it now. But there's not much point in getting that working again if I can't see my results in the IDE.


Solution

  • Short answer: yes you can using the SonarQube Community IntelliJ plugin

    Long answer:

    assuming you have a build.gradle like:

    apply plugin: "sonar-runner"
    
    sonarRunner {
        sonarProperties {
            // can be also set on command line like -Dsonar.analysis.mode=incremental
            property "sonar.host.url", "http://your.sonar.server:9000"
            property "sonar.analysis.mode", "incremental" 
            property 'sonar.sourceEncoding', 'UTF-8'
            property 'sonar.language', 'java'
            property 'sonar.profile', 'my_profile'
        }
    }
    
    subprojects {
        sonarRunner {
            sonarProperties {
                properties["sonar.sources"] += "src/main/java"
            }
        }
    }
    
    ....
    

    then you can run local sonar analysis with gradle:

    $ ./gradlew sonarRunner
    

    this will produce a sonar-report.json file:

    $ cat build/sonar/sonar-report.json
    

    Now you have everything is needed by the plugin:

    • SonarQube server
    • Local analysis script
      • Name: gradle script
      • Script: /path/to/android-studio-example-project/gradlew sonarRunner
      • Path to sonar-report.json: /path/to/android-studio-example-project/build/sonar/sonar-report.json

    After the configuration is done you can see new issues by running the SonarQube (new issues) inspection inside Intellij (Android Studio)

    I have used this project for an example:

    https://github.com/sonar-intellij-plugin/android-studio-example-project

    and sonarqube server 4.0 with a squid only based rule set (4.4 failed to analyse a gradle project)