The Android Gradle plugin can create code coverage reports that track the
percentage of your code that your tests cover. This page describes how to
enable coverage reporting and generate reports.

You can generate coverage reports for each test type (unit and instrumentation)
for each variant. You can also generate unified coverage reports across
different test types, modules, and build variants.

> [!NOTE]
> **Note:** Coverage reports are not generated if any of the relevant tests fail.

## Enable code coverage

Code coverage must be enabled by setting `enableAndroidTestCoverage` and
`enableUnitTestCoverage` to `true` in the module-level build files for each
variant you want to get coverage reports for:

### Kotlin

```kotlin
android {
    // ...
    buildTypes {
        debug {
            // Enable coverage for unit tests
            enableUnitTestCoverage = true
            // Enable coverage for instrumentation tests
            enableAndroidTestCoverage = true
        }
    }
}
```

### Groovy

```groovy
android {
    // ...
    buildTypes {
        debug {
            // Enable coverage for unit tests
            enableUnitTestCoverage true
            // Enable coverage for instrumentation tests
            enableAndroidTestCoverage true
        }
    }
}
```

### Change Jacoco version (optional)

AGP automatically applies Jacoco when you enable coverage on your modules.
However, if you need to use a specific version of Jacoco, you can specify it in
your module-level build file:

### Kotlin

```kotlin
android {
    testCoverage {
        jacocoVersion = "JACOCO_VERSION"
    }
}
```

### Groovy

```groovy
android {
    testCoverage {
        jacocoVersion = 'JACOCO_VERSION'
    }
}
```

## Generate variant specific coverage reports

To generate coverage reports for only unit tests or only instrumented tests for
a specific variant, run the corresponding tasks.

| Test type | Command | Report location |
|---|---|---|
| Unit tests | `./gradlew :module-name:createVariantNameUnitTestCoverageReport` | `path-to-your-project/module-name/build/reports/coverage/test/variant/index.html` |
| Instrumented tests | `./gradlew :module-name:createVariantNameAndroidTestCoverageReport` | `path-to-your-project/module-name/build/reports/coverage/androidTest/variant/connected/index.html` |

## Generate unified code coverage reports

You can generate unified code coverage reports using the `testAllSuites`
and `testAllSuitesWithDependencies` Gradle tasks. These tasks will generate a
single HTML report that consolidates coverage data from different test types
(unit and instrumentation), modules, and build variants. This provides a
comprehensive view of your project's code coverage in a single dashboard.

> [!WARNING]
> **Experimental:** This is an experimental feature. To enable this feature, set `android.experimental.reportAggregationSupport=true` in your `gradle.properties` file.

### Prerequisites

- Android Gradle Plugin 9.5.0-alpha05 or higher
- [Enable code coverage](https://developer.android.com/studio/test/coverage-report#enable-coverage)

To generate a unified report, run one of the following tasks from the command
line:

| Coverage scope | Command | Description | Report location |
|---|---|---|---|
| Current module | `./gradlew :module-name:testAllSuites --continue` | Tests all test suites, and generates a unified coverage report for the current module, merging data from all test types. | `path-to-your-project/module-name/build/reports/testAllSuites/coverage-report/` |
| Current module and dependencies | `./gradlew :module-name:testAllSuitesWithDependencies --continue` | Tests all test suites, and generates a unified coverage report for the current module and all its dependencies. This task is available for app modules and [library modules with publication enabled](https://developer.android.com/build/publish-library). | `path-to-your-project/module-name/build/reports/testAllSuitesWithDependencies/coverage-report/` |

The generated HTML report landing page shows a high-level summary of all
modules. You can drill down from module to package, from package to class, and
from class to source file.
Click any file to see code with highlighting for line and branch coverage:

- Green: Covered lines.
- Red: Uncovered lines.
- Yellow: Partial coverage (some instructions or branches missed).

## Use on-device code coverage for instrumented tests

> [!WARNING]
> **Experimental:** This is an experimental feature. To enable this feature, set `android.experimental.androidTest.coverage.useOnDeviceAgent=true` and `android.experimental.reportAggregationSupport=true` in your `gradle.properties` file.

By default, AGP measures the coverage of instrumented tests by instrumenting
your app's classes with JaCoCo when it builds your app. Starting with AGP
9.6.0-alpha01, you can use on-device instrumentation instead, which instruments
your app's classes on the test device while your tests run. For more
information, see [View code coverage reports (with on-device
agent)](https://developer.android.com/studio/test/on-device-code-coverage).