Skip to content

Instantly share code, notes, and snippets.

@mrsasha
Last active March 21, 2024 10:36
Show Gist options
  • Save mrsasha/22544a9606f4196c629616aa25d3a075 to your computer and use it in GitHub Desktop.
Save mrsasha/22544a9606f4196c629616aa25d3a075 to your computer and use it in GitHub Desktop.
adding Sonarqube reporting for Lint and test coverage to Android Kotlin project (single module)
buildscript {
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.2"
}
}
apply plugin: 'org.sonarqube'
android {
sonarqube {
androidVariant 'stagingDebug' //PUT REAL TASK NAME HERE
properties {
//PUT REAL PATHS HERE
property "sonar.java.binaries", "build/intermediates/classes/staging/debug,build/tmp/kotlin-classes"
property "sonar.junit.reportPaths", "build/test-results/testStagingDebugUnitTest"
property "sonar.jacoco.reportPaths", "build/jacoco/testStagingDebugUnitTest.exec"
property "sonar.android.lint.report", "build/reports/lint-results-stagingDebug.xml"
}
}
}
//ADD ALL THE TASKS (coverage, lint) YOU WANT TO BE REPORTED
project.tasks["sonarqube"].dependsOn 'testStagingDebugUnitTestCoverage'
project.tasks["sonarqube"].dependsOn 'lintStagingDebug'
sonarqube {
properties {
property "sonar.host.url", "https://sonarqube.unterwegs.io"
property "sonar.login", System.getenv("SONAR_LOGIN")
property "sonar.projectKey", "PUT-YOUR-DESIRED-PROJECT-KEY-HERE"
property "sonar.projectName", "PUT YOUR DESIRED PROJECT NAME HERE"
property "sonar.projectVersion", project.versionName
property "sonar.sourceEncoding", "UTF-8"
}
}

Adding Sonarqube reporting for Lint and test coverage to Android Kotlin project (single module)

add the build.gradle code to your top-level gradle file and module.gradle code to your module gradle file.

Take care to put the projectName and projectVersion names, as well as to put the correct report paths, androidVariant name and task names. Check that you have SONAR_LOGIN set in the CI environment.

You can now test that the reports are sent correctly by executing ./gradlew sonarqube and checking the sonarqube project dashboard to make sure your project is there.

If you use fastlane, also add this to your fastlane\Fastfile:

lane :sonarqube do
  gradle(task: "sonarqube")
end

Don't forget to add the task to the CI file, e.g. .gitlab-ci.yml:

sonarqube_reporting:
  stage: report
  except:
    - master
  before_script:
    - bundle install
  script:
    - bundle exec fastlane sonarqube --verbose
  artifacts:
      paths:
        - app/build/reports/jacoco

Lastly, if you want to have a nice badge with metrics in your README.md page (or any other Markdown page) like this:

coverage-badge

you can add this: [![coverage](https://sonarqube.unterwegs.io/api/badges/measure?key=navigation-android&metric=coverage)](https://sonarqube.unterwegs.io/component_measures/metric/coverage/list?id=navigation-android) (this shows coverage for navigation-android project). To check more badge examples, visit the badge homepage.

@rafaelaguerra
Copy link

@mrsasha How can you force build failed on gitlab if sonarqube doesn't follow quality metrics?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment