Saturday, July 27, 2019

Android - Configuring SonarQube with Android Studio project.

CI/CD (Continuous Integration/Continuous Deployment) is the backbone of the organizations to build a good quality products. The CI/CD bridges the gap between development and operations teams by automating build, test, and deployment of applications. It helps the development team to find code smells in the projects.

sonar qube installation in Android Studio project
Code smells are bugs in your code that produce the performance issue of the Application. We can find this smell with the help of the various tool. The tool can help you define custom rules, in addition to the common code smell patterns, externalize these rules and have the flexibility to apply them to the code at the project level, department level and at the enterprise level.  

The SonarQube is an open-source service that can scan code in 25+ languages and generates reports of smells, vulnerabilities, and bugs. It provides a beautiful dashboard scanning data where we can analyze our code quality. SonarQube is a big step toward automating development operations as it enables continuous code inspection that will improve code quality and ensures clean code.  It can easily be integrated right into the CI/CD process, which will enable continuous inspection of code for bugs, vulnerabilities, and smells, and can be extended. SonarQube can also be extended by using plugins. For example, we can use the CodeAnalyzer plugin to measure cyclomatic complexity.



In this post, we'll show you how to configure SonarQube with Android Studio project in order to manage code quality and run Sonarqube scanner on our code project. 
  
SonarQube Installation in Android Studio
1. First of all, download the latest version of SonarQube and unzip it.

2. After that open sonar qube directory folder \sonarqube-developer-7.9.1\sonarqube-7.9.1\bin\windows-x86-64 and run server on the shell by using StartSonar.bat file.  It'll take some time to run the server. 
sonar qube server run command on window
3. Now, you can check server on the browser by using http://localhost:9000 path and you'll see following window.

sonar qube admin panel on window


You can log in the admin panel by using admin/admin credential.

4. By default, sonar qube set some scan rule for our project. But we can customize it by using the Rules tab.

sonar qube admin panel rules tab



 5. To push our Android project in CI/CD pipeline, we have to update our project gradle files:
  • Add the following line in the project level build.gradle file.
build.gradle
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.2"
  • After that open app build.gradle file and push following dependency in the bottom of the file.
app/build.gradle
apply plugin: 'org.sonarqube'
sonarqube { properties { property "sonar.projectName", "developerlibs" property "sonar.projectKey", "com.devlibs.android" property "sonar.language", "kotlin" property "sonar.sources", "src/main/java/" property "sonar.binaries", "build" property "sonar.sourceEncoding", "UTF-8" property "sonar.login", "admin" property "sonar.password", "admin" } }

At the end, we have to run gradlew sonar command in the project terminal to build project on sonar server. Once, you project successfully build project in Android Studio terminal refresh sonar admin panel and you will see following page with code smells and bugs.

sonar qube project report

If you have followed the article carefully, you can see your project visible on sonar qube panel and displaying a generated report of code smell. But if you are facing any problem or you have any quires, please feel free to ask it from the comment section.
Share:

Get it on Google Play

React Native - Start Development with Typescript

React Native is a popular framework for building mobile apps for both Android and iOS. It allows developers to write JavaScript code that ca...