Thursday, 11 April 2019

sonarQube Android | How to use sonarQube in android studio | End to end integration of sonarQube in android project | sonar qube code quality check in android studio | Android studio sonarQube




1. Download SonarQube from following link v 6.6.7

https://www.sonarqube.org/downloads/

2. Now Open your terminal and go to your downloaded sonarQube folder:
for example u are on root like:-

SupriyaMac:~ SupriyaBharti$ cd Downloads/sonarqube-6.7.6/bin/macosx-universal-64/
SupriyaMac:macosx-universal-64 SupriyaBharti$ ./sonar.sh start
Starting SonarQube...
SonarQube is already running.

Images:-

3. Now time to open sonar local host in your browser , please copy paste below link in your web browser:-


4. Open your android studio project and open the project build.gradle and add following :-

buildscript {
    repositories {
        google()
        jcenter()
        maven { url "https://plugins.gradle.org/m2/" } // added for sonarQube 
    }

dependencies {
    classpath 'com.android.tools.build:gradle:3.3.2'
    classpath 'org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.1' // added for sonarQube
}

Images :- 

5. Now open  app build.gradle and add following :-
apply plugin: 'org.sonarqube' // added for sonar qube
// added for sonar qube
sonarqube {
    properties {
        property "sonar.projectName", "ContentResolver"
        property "sonar.projectKey", "ContentResolver"
        property "sonar.host.url", "http://localhost:9000"
        property "sonar.language", "java"
        property "sonar.sources", "src/main/java"
        property "sonar.java.sources", "src/main/java"
        property "sonar.sourceEncoding", "UTF-8"
        property "sonar.login", "admin"
        property "sonar.password", "admin"
    }
}

Note :- Here in sonarQube property "sonar.sources""src/main/java" this source path is very important .
Images :- 
Note :- put sonar properties above dependencies. 


6. Now open your terminal inside android studio 
check you are at you current project root like for me my current project location will looks like following :-

SupriyaMac:ContentResolver SupriyaBharti$ 

7. Now run following command :

SupriyaMac:ContentResolver SupriyaBharti$ ./gradlew sonarqube -Dsonar.host.url=http://localhost:9000/


NOTE :  no 7 is very important if you will not run this command than u will not able to connect your project with sonarLocal host.

8. Finally your refresh sonarQube local host and it will look look following :-
Images:-

For End to End sonar Qube integration you can also watch my youtube video :

https://www.youtube.com/watch?v=AjoI7EvVgMQ&t=4s 

9 comments:

  1. hello!
    i have a question for you..
    can you contact me throw email?
    thank you.

    ReplyDelete
  2. What went wrong:
    Execution failed for task ':app:sonarqube'.
    > Unable to execute SonarQube

    ReplyDelete
  3. hi,
    I am getting below error while excuses ./gradlew sonarqube -Dsonar.host.url=http://localhost:9000/

    Error
    Could not initialize class org.codehaus.groovy.runtime.InvokerHelper

    ReplyDelete
    Replies
    1. Pure a me succede hai capito perchè?

      Delete
  4. HI I am facing Execution failed for task ':app:compileDebugJavaWithJavac'.
    > javax/xml/bind/JAXBException

    this exception from android studio

    ReplyDelete
  5. i m getting bash: ./gradlew: /usr/bin/env: bad interpreter: Operation not permitted

    ReplyDelete