Skip to content

Instantly share code, notes, and snippets.

@mrsasha
Last active April 14, 2022 12:24
Show Gist options
  • Save mrsasha/c7b448bbd7bc7087ed3c167e09af104a to your computer and use it in GitHub Desktop.
Save mrsasha/c7b448bbd7bc7087ed3c167e09af104a to your computer and use it in GitHub Desktop.
adding Sonarqube reporting for Lint and test coverage to Android Kotlin project (multi-module)
apply plugin: "org.sonarqube"
buildscript {
dependencies {
classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:2.6.2"
}
}
//ADD ALL THE TASKS (coverage, lint) FOR ALL THE MODULES YOU WANT TO BE REPORTED
project.tasks["sonarqube"].dependsOn ':library-core:testDebugUnitTestCoverage'
project.tasks["sonarqube"].dependsOn ':library-core:lintStagingDebug'
project.tasks["sonarqube"].dependsOn ':application:testDevDebugUnitTestCoverage'
project.tasks["sonarqube"].dependsOn ':application: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"
}
}
sonarqube {
androidVariant 'stagingDebug' //PUT REAL TASK NAME HERE
properties {
//PUT REAL PATHS HERE
property "sonar.java.binaries", "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"
}
}

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

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

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.

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