- Notifications
You must be signed in to change notification settings - Fork27
A Gradle Plugin for Android developers that automatically configures Jacoco code coverage tasks for both combined and per module coverage reports, easier than ever.
License
NeoTech-Software/Android-Root-Coverage-Plugin
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Automatically configures Jacoco code coverage tasks for both combined and per module coverage reports.
Configuring Jacoco for Android projects is unfortunately not always easy. It is very common to find multi-module Androidprojects where one module has tests for code found in another module, for example integration/UI tests that cover codein multiple other modules. Configuring Jacoco for such a case is not always straight forward, you need to point Jacocoto the right sources, execution data and class files, not to mention how error prone manual Jacoco setups can be. Thisplugin automatically configures Jacoco for you, so you don't have to.
Feature highlights:
- Include unit-tests, instrumented unit-tests or both in the final reports
- Support for combined coverage reports(code in module X is covered when touched by tests from any other module)
- Support for coverage reports per module(code in module X is only covered when touched by tests from module X)
- Custom package/class filters
- Support for mixed build-types
Apply the plugin to your top-level (root project)build.gradle
file using one of thefollowing methods:
Plugin block (Kotlin):
plugins {// Add the plugin to the plugin block id("nl.neotech.plugin.rootcoverage") version"1.10.0"}
Plugin block (Groovy):
plugins {// Add the plugin to the plugin block id"nl.neotech.plugin.rootcoverage" version"1.10.0"}
Still using classpath & apply?
Groovy
applyplugin:'nl.neotech.plugin.rootcoverage'buildscript { dependencies { classpath'nl.neotech.plugin:android-root-coverage-plugin:1.10.0' }}
- Enable running tests with code coverageThis is required so that code will be instrumented, but also to tell this plugin to includemodules that have at least one of these properties enabled in the final report (or individualreports)
Kotlin (.gradle.kts)
android { buildTypes { debug {// AGP 7.3+ (at least one should be true for this module to be included in the reporting) enableUnitTestCoverage=true enableAndroidTestCoverage=true// AGP before 7.3 testCoverageEnabled=true } }}
Groovy (.gradle)
android { buildTypes { debug {// AGP 7.3+ (at least one should be true for this module to be included in the reporting) enableUnitTestCoveragetrue enableAndroidTestCoveragetrue// AGP before 7.3 testCoverageEnabledtrue } }}
Only Android modules (
com.android.application
orcom.android.library
) are supported, this plugin will not executetests and generate coverage reports for non-android modules. Also keep in mind that by defaultthis plugin is configured to create reports for thedebug
variant, so coverage must beenabled for thedebug
variant, unless you change this (Configuration).
Run one of the automatically configured Gradle tasks to generate a Jacoco report:
- For combined coverage:
./gradlew :rootCoverageReport
- For module specific coverage (all modules):
./gradlew coverageReport
- For module specific coverage (single module):
./gradlew :yourModule:coverageReport
Resulting reports can be found in
/build/reports/
andyourModule/build/reports/
- For combined coverage:
Optionally configure the plugin to change the output types, test variants and more, seeConfiguration.
By default the plugin generates code coverage reports using the build variantdebug
for everymodule. However in some cases different build variants per module might be required, especially ifthere is nodebug
build variant available. In those cases you can configure custom build variantsfor specific modules:
Place this in the project root build.gradle file!
Kotlin (.gradle.kts)
rootCoverage {// The default build variant for every module buildVariant="debug"// Overrides the default build variant for specific modules. buildVariantOverrides=mapOf(":moduleA" to"debugFlavourA",":moduleB" to"debugFlavourA")// Class & package exclude patterns excludes= ["**/some.package/**"]// Since 1.1 generateHtml is by default true generateCsv=false generateHtml=true generateXml=false// Since 1.2: Same as executeTests except that this only affects the instrumented Android tests executeAndroidTests=true// Since 1.2: Same as executeTests except that this only affects the unit tests executeUnitTests=true// Since 1.2: When true include results from instrumented Android tests into the coverage report includeAndroidTestResults=true// Since 1.2: When true include results from unit tests into the coverage report includeUnitTestResults=true// Since 1.4: Sets jacoco.includeNoLocationClasses, so you don't have to. Helpful when using Robolectric// which usually requires this attribute to be true includeNoLocationClasses=false// Since 1.7 (experimental): If set to true instrumented tests will be attempt to run on// Gradle Managed Devices before trying devices connected through other means (ADB). runOnGradleManagedDevices=false// Since 1.7 (experimental): The name of the Gradle Managed device to run instrumented tests on.// This is only used if `runOnGradleManagedDevices` is set to true. If not given tests will be// run on all available Gradle Managed Devices gradleManagedDeviceName="nexusoneapi30"}
Groovy (.gradle)
rootCoverage {// The default build variant for every module buildVariant"debug"// Overrides the default build variant for specific modules. buildVariantOverrides":moduleA" :"debugFlavourA",":moduleB":"debugFlavourA"// Class & package exclude patterns excludes= ["**/some.package/**"]// Since 1.1 generateHtml is by default true generateCsvfalse generateHtmltrue generateXmlfalse// Since 1.2: Same as executeTests except that this only affects the instrumented Android tests executeAndroidTeststrue// Since 1.2: Same as executeTests except that this only affects the unit tests executeUnitTeststrue// Since 1.2: When true include results from instrumented Android tests into the coverage report includeAndroidTestResultstrue// Since 1.2: When true include results from unit tests into the coverage report includeUnitTestResultstrue// Since 1.4: Sets jacoco.includeNoLocationClasses, so you don't have to. Helpful when using Robolectric// which usually requires this attribute to be true includeNoLocationClassesfalse// Since 1.7 (experimental): If set to true instrumented tests will be attempt to run on// Gradle Managed Devices before trying devices connected through other means (ADB). runOnGradleManagedDevicesfalse// Since 1.7 (experimental): The name of the Gradle Managed device to run instrumented tests on.// This is only used if `runOnGradleManagedDevices` is set to true. If not given tests will be// run on all available Gradle Managed Devices gradleManagedDeviceName"nexusoneapi30"}
Version | Android Gradle plugin version | Gradle version |
---|---|---|
1.10.0 | 8.8+ | 8.10.2+ |
1.9.0 | 8.6+ | 8.7+ |
1.8.0 | 8.5.2 8.4.2 8.3.0-alpha05 - 8.3.2 | 8.6+ 8.5+ 8.4+ |
Note 1 | 8.0 - 8.3.0-alpha04 | n.a. |
1.7.1 | 7.4 | 7.5+ |
1.6.0 | 7.3 | 7.4+ |
1.5.3 | 7.2 | 7.3+ |
Note 2 | 7.0 - 7.2.0-alpha05 | n.a. |
1.4.0 | 4.2 4.1 | 6.7.1+ 6.5+ |
1.3.1 | 4.0 3.6 | 6.1.1+ 5.6.4+ |
1.2.1 | 3.5 | 5.4.1+ |
1.1.2 | 3.4 | 5.1.1+ |
1.1.1 | 3.3 | 4.10.1+ |
1.0.2 | 3.2 | 4.6+ |
Note 1: AGP 8.0-8.3.0-alpha04
Android Gradle Plugin version 8.0 till 8.3.0-alpha04 suffered from abug that made it impossible (by normal means) to get access to non-instrumented class files, this bug lasted for a long time and was only fixed in 8.3.0-alpha05. This means there is no stable working plugin version available for these AGP versions.
Note 2: AGP 7.0-7.2.0-alpha05
Android Gradle Plugin version 7 till 7.2.0-alpha05 suffered from abug that caused instrumented coverage in Android library modules to fail, this has only beenfixed in Android Gradle Plugin 7.2.0-alpha06. This means there is no stable working plugin version available for these AGP versions.
Note 3: Versions 1.0.2 to 1.3.0
Plugin versions below 1.3.1, such as 1.3.0, are only available on the Gradle Plugin Portal and not on Maven Central. These versions use the group IDorg.neotech.plugin
and plugin IDorg.neotech.plugin.rootcoverage
! For more info see:Bintray/JCenter shutdown.
Note 4: AGP versions before 3.4.0-alpha05
Android Gradle Plugin versions before3.4.0-alpha05
are affected by a bug that in certain conditions can cause Jacoco instrumentation to fail in combination with inline kotlin methods shared across modules. For more information see:issue #109771903 andissue #110763361. If your project is affected by this upgrade to an Android Gradle Plugin version of at least3.4.0-alpha05
.
Due to theshutdown of Bintray/JCenterthe Android-Root-Coverage-Plugin has been migrated to Sonatype's Maven Central repository. Unfortunately this alsomeant that the group ID used by the Android-Root-Coverage-Plugin had to be changed fromorg.neotech.plugin
tonl.neotech.plugin
. The plugin ID has also changed fromorg.neotech.plugin.rootcoverage
tonl.neotech.plugin.rootcoverage
.
More info
JCenter is supposed to stay available as read-only repository, however it is probably better tomigrate to the Gradle Plugin Portal, as it is the official Gradle repository for plugins, and allversions of this plugin are available there:
pluginManagement { repositories {// Add this repository to your build script for plugin resolution, above mavenCentral() or jcenter() gradlePluginPortal() }}
Version 1.3.0 has been re-released (as 1.3.1) with the new group ID and plugin ID to Maven Central and theGradle Plugin Portal. Upcoming versions will also be released to Maven Central and the Gradle Plugin Portal.Check thesetup section on how to use this plugin with the updated group ID and plugin ID.
Want to contribute? Great! Just clone the repo, code away and create a pull-request. Try to keep changes small and makesure to follow the code-style as found in the rest of the project.
How to test your changes/additions?The plugin comes with an integration test and unit tests. You can run these tests either by executing./gradlew clean test
or run the test directly from Android Studio (or IntelliJ IDEA).
About
A Gradle Plugin for Android developers that automatically configures Jacoco code coverage tasks for both combined and per module coverage reports, easier than ever.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors5
Uh oh!
There was an error while loading.Please reload this page.