Next, we can see how to configure SonarQube in Android Studio
Step 1: In app’s build.gradle, if we add SonarQube Plugin and other details like this
allprojects {
apply plugin: 'org.sonarqube'
sonar {
properties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.test.inclusions", "src/test/**"
property 'sonar.profile', 'Android Lint'
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectName", "SonarTestApp"
property "sonar.projectKey", "SonarTestKey"
property "sonar.projectVersion", 1.0.0
property "sonar.login", "sqp_123123kj123k123j123kj123j1k23k123jk132j"
}
}
}
we may get the following error Plugin with id ‘org.sonarqube’ not found
So, we can try this instead (applying plugin outside allprojects and mentioning other details inside allprojects)
plugins {
id "org.sonarqube" version "4.0.0.2929"
}
allprojects {
sonar {
properties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.test.inclusions", "src/test/**"
property 'sonar.profile', 'Android Lint'
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectName", "SonarTestApp"
property "sonar.projectKey", "SonarTestKey"
property "sonar.projectVersion", 1.0.0
property "sonar.login", "sqp_123123kj123k123j123kj123j1k23k123jk132j"
}
}
}
In the above code, property “sonar.projectVersion” should match your project’s versionName.
Step 2: In gradle.properties
systemProp.sonar.host.url=http://localhost:9000
# (Optional, if not using credentials in build.gradle)
systemProp.sonar.login=your_sonar_username
systemProp.sonar.password=your_sonar_password
Step 3: Finally run your sonar analysis for your project using this command in Android Studio Terminal:
./gradlew sonarqube
You may get the following error if JDK is not installed.
The operation couldn’t be completed. Unable to locate a Java Runtime.
I downloaded suitable JDK Version 17 as my Android Gradle plugin requires Java 17 to run. After installing JDK and typing the same command ./gradlew sonarqube again
Build was getting failed due to an error.
Execution failed for task ‘:app:sonarqube’. > Cannot get property ‘0.0’ on null object
Solution? From Step 2, we should replace property “sonar.projectVersion”, 1.0.0 to “sonar.projectVersion”, 1.0 (as my project’s versionName is 1.0) like this
allprojects {
sonar {
properties {
property "sonar.host.url", "http://localhost:9000"
property "sonar.test.inclusions", "src/test/**"
property 'sonar.profile', 'Android Lint'
property "sonar.sourceEncoding", "UTF-8"
property "sonar.projectName", "SonarTestApp"
property "sonar.projectKey", "SonarTestKey"
property "sonar.projectVersion", 1.0
property "sonar.login", "sqp_123123kj123k123j123kj123j1k23k123jk132j"
}
}
}
After all steps are successful, we will get BUILD SUCCESSFUL message in terminal