简体   繁体   中英

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.

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)

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

Sonar Runner is outdated now and is replaced by Sonarqube Scanner.

After intalling sonarqube in your system,you need to add sonarqube plugin to app module gradle file of your project.

plugins {
    id "org.sonarqube" version "2.2.1"
}

Then Define Sonarqube properties for your project and sync your project.

sonarqube {
    properties {
        property "sonar.projectName", "SonarqubeDemo"
        property "sonar.projectKey", "SQKey"
        property "sonar.sources","src/main/java"
        property "sonar.language","java"
        property "sonar.sourceEncoding", "UTF-8"
//        property "sonar.exclusions", "src/main/java/com/foo/Foo.java"
    }
}

After building or syncing project,open Command Prompt and navigate to app module directory where your gradle file is located.

Execute gradle sonarqube and wait until the build is completed

Below is the link of post which gives complete detailed explanation of integrating sonarqube with sonarqube scanner in Android .

Integrating and Understanding SonarQube in Android

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM