Exclude packages from Jacoco report using Sonarrunner and Gradle

To combine @Mikalai's answer and @pavel's comment into something that's a bit easier to copy and paste:

To exclude a package or class from all Sonar checks (coverage, code smells, bugs etc), add the following to build.gradle:

sonarqube {
    properties {
        property 'sonar.exclusions', "**/com/some/package/**"
    }
}

To exclude a package or class from only Sonar code coverage checks, add the following to build.gradle:

sonarqube {
    properties {
        property 'sonar.coverage.exclusions', "**/com/some/package/**"
    }
}

To exclude res, assets, custom packages and auto generated classes from sonar code coverage for android project.

Create Exclusion list like below

exclusionList = [
            //Res and Assets
            "src/main/res/**/*.*",
            "src/main/assets/**/*.*",

            //Auto-Generated
            '**/R.class',
            '**/R$*.class',
            '**/BuildConfig.*',
            '**/*Manifest.*',
            'android/**/*.*',
            'androidx/**/*.*',

            // excluded packages
            **/com/<your-package-path>/**/*]

Provide the exclusion list to sonar coverage property

property 'sonar.coverage.exclusions', exclusionList

NOTE:

  • Keep the full path to exclude the full directory
  • **/* includes all the files and sub directories under the parent directory Refer Here
  • *Bean includes all the class names contains "Bean".
  • **/ starting with it covers the parent directories for the particular package.

Run Jacoco command and check sonar portal's coverage section after doing above changes.


I have managed to exclude particular packages from coverage reports by using sonar.coverage.exclusions property in sonar-project.properties. Property is described in official documentation