Analyzing my Android Apps with SonarQube + Docker
Tags: docker, android, kotlin
Published: 2020-10-19 00:25:44

If you are not familiar with SonarQube, let me explain to you a little bit why you should know it.

As its documentation says, SonarQube is an automatic code review tool to detect bugs, vulnerabilities, and code smells in your code.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/kgeyzxg9utp13ejejqc3.png)

Docker... why I need docker? The truth is, you don't need It to run SQ, you can download a .zip and run it... But remember, Docker was made to facilitate our lives, so if you already have It installed on your machine I recommend you to use it, it's free!. But, if you still want that zip, [HERE](https://docs.sonarqube.org/latest/setup/get-started-2-minutes/) you can download the boring zip, just go to the zip section and do what it says and skip the docker section.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/yz8m7aqp66bwnfzagip5.jpeg)

#Starting with Docker

As I said, Docker was made to facilitate our lives, and if you haven't installed docker yet, this is your time. [HERE](https://www.docker.com/get-started) you can do it alone, it's very quick and simple. 

Once Docker is running on your machine, you need to run the next command in your terminal. 

```SSH
$ docker run -d --name sonarqube -e -p 9000:9000 sonarqube:latest
```
So, basically, we are telling Docker to create (**run**) a new container and start it. In this case, our container **--name** will be **sonarqube** and we are running our container in the background of your terminal **-d**, we don't want to get anything there. Also, we are telling It that we want to run our container in the **9000** port **-p**. All this will be from the image **sonarqube:latest**, if we don't have this image locally yet, docker will download it for us.

Then we just need to start our container like this:

```SSH
$ docker start sonarqube
```
Here an example of what you are going to get:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/dqo3vpk1jowfusz72anh.png)

Once Docker is running, you can go to your localhost in the 9000 port: 

```SSH
http://localhost:9000/
```

Remember, by default user and password, is: **admin**

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/pkwt0ncgeshi5ghvg8sz.png)

That's it from docker and SonarQube... Now it's time to set up our Android project.

#Setting up my Android app

I'm going to use an app that I did a time ago named [Lucky Piñata](https://github.com/jbc7ag/lucky-pinata). Maybe you remembered from my [Retrofit + Coroutines](https://dev.to/jbc7ag/using-retrofit-coroutines-in-kotlin-15bc) Post. So, in this case, I create a new branch for this SQ changes named: [sonarqube-config](https://github.com/jbc7ag/lucky-pinata/tree/sonarqube-config). You can use your own project to set it up.


First, Open your Android Studio project, and install the SonarQube Plugin:

Go to: **Android Studio** > **Preferences** > **Plugins**

Under the Marketplace Tab, look for SonarQube Comunity Plugin

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9j52iin99xlc99y5nizi.png)

Once installed, restart your IDE.

Now, we need to add the plugin to our project. Go to **Build Gradle (Project)** and add it under dependencies:

```kotlin
  classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:$version_sonarqube"

```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tv8srtprbbk667i2mdlk.png)

Then, let's set up our SonarQube project in the **Build Gradle(app)**

Add the plugin and the properties that will help us to connect our app with the SonarQube environment.

These properties need to be outside everything. 

```kotlin
 apply plugin: "org.sonarqube"

 sonarqube {
    properties {
        property "sonar.projectName", "lucky-pinata"
        property "sonar.projectKey", "com.jbc7ag.luckypinata"
        property "sonar.host.url", "http://localhost:9000"
        property "sonar.sources", "src/main/"
        property "sonar.login", "admin"
        property "sonar.password", "admin"
    }
}

```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/51r7c16kwdr04qy5d1jk.png)

Now everything is done. We just need to run the following command in the terminal:

```kotlin
  ./gradlew sonarqube
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vsl4d54yu79zfih92yrz.png)

If your build is successful, you can now go to your localhost:9000, login, and get the report of your project.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/zah2f37nwamqf64wof3k.png)

Theres too many things that you can do in SonarQube, start researching, improving your coding, and share your knowledge.