SonarQube plugin with Android Studio

20,924

Solution 1

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)

Solution 2

Open Android studio

File -> Settings -> Plugins -> then type sonarqube and click on Browse repositories at the bottom.

Restart Android Studio.

now right click on project -> Analyze -> Sonarlint -> Analyze all files with Sonarlint

The results will be displayed in Sonarlint view at bottom

Share:
20,924
Merk
Author by

Merk

Updated on November 15, 2020

Comments

  • Merk
    Merk over 3 years

    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.

  • Sowmia Sundararajan
    Sowmia Sundararajan over 8 years
    best document for configuring sonar in Android Studio github.com/sonar-intellij-plugin/sonar-intellij-plugin