---
title: Android
description: >-
  Enable Emma instrumentation in an Expo or React Native CLI test app, pull the
  `.ec` file, and turn it into a JaCoCo XML report with rn-coverage.
---
> ## Documentation Index
> Fetch the complete documentation index at: https://docs.page/invertase/react-native-coverage/llms.txt
> Use this file to discover all available pages before exploring further.

Pull the Emma `.ec` file from your [dedicated test app](/coverage#dedicated-test-app) and turn it into a JaCoCo XML report. Expo needs fewer steps.

## Expo

Add the config plugin. It applies both Gradle helpers and turns on `testCoverageEnabled` for the `debug` build type:

```json
{
  "expo": {
    "plugins": [
      [
        "react-native-coverage",
        {
          "libraryProjectMatchers": ["my-lib"],
          "enableAndroidCoverage": true
        }
      ]
    ]
  }
}
```

Then generate the native Android project:

```sh
npx expo prebuild
```

After prebuild, the `android/build.gradle` file applies the root helper and the `android/app/build.gradle` file applies the JaCoCo report helper and sets `testCoverageEnabled`.

## React Native CLI

In the root `android/build.gradle` file, add this block:

```gradle
ext.coverageLibraryProjectMatchers = ['my-lib']
def rnCoverageRoot = new File(
  ["node", "--print", "require.resolve('react-native-coverage/package.json')"]
    .execute(null, rootDir).text.trim()
).parentFile
apply from: new File(rnCoverageRoot, "android/rn-coverage.gradle")
```

In the `android/app/build.gradle` file, add this block:

```gradle
android {
  buildTypes {
    debug {
      testCoverageEnabled true
    }
  }
}

def rnCoverageRoot = new File(
  ["node", "--print", "require.resolve('react-native-coverage/package.json')"]
    .execute(null, rootDir).text.trim()
).parentFile
apply from: new File(rnCoverageRoot, "android/rn-coverage-jacoco.gradle")
```

These two helpers wire JaCoCo into the library and app projects.

| File | Role |
|------|------|
| `android/rn-coverage.gradle` | Enables JaCoCo and `enableAndroidTestCoverage` on every library project that matches `coverageLibraryProjectMatchers` |
| `android/rn-coverage-jacoco.gradle` | Adds the `jacocoTestReport` task and the unit and end-to-end report variants |

JaCoCo class directories and execution-data trees resolve from each project's `layout.buildDirectory` (not a hard-coded `projectDir/build`). Class trees walk every AGP variant under `intermediates/javac/<variant>/…/classes` and `tmp/kotlin-classes/<variant>` (debug, release, and flavored variants when present). If you redirect a library's Gradle build directory so two Android builds in one checkout do not collide, the report helper still finds the classes and `.ec` / `.exec` files under that redirected root.

## Pull and report

When the suite ends, `Coverage.flush()` calls Emma's `RT.dumpCoverageData` and writes `filesDir/coverage.ec`. On the host, stage the `.ec` file, write the JaCoCo XML, and assert matched hits:

```sh
rn-coverage android pull     # stages the .ec under android/app/build/ so JaCoCo's fileTree includes it
rn-coverage android report   # runs jacocoTestReport and writes the XML
rn-coverage assert           # exits 2 when the XML has no matched LINE hits
```

`rn-coverage android pull` creates the staging directories and lands `emulator_coverage.ec` under the app build directory (`<layout.buildDirectory>/outputs/code_coverage` by default). When `--output` is omitted, the CLI queries `:app:rnCoveragePrintAppBuildDir` so a redirected app build directory still stages inside JaCoCo's `fileTree`; set `android.coveragePullDir` to override. A file pulled only into `artifacts/` leaves `:app:jacocoTestReport` skipped with empty data.

JaCoCo XML package names use slashes, `com/foo`. The `assert` matchers accept dots, `com.foo`, or slashes.

For JavaScript coverage from the same run, add `rn-coverage js pull` and `rn-coverage js report` from [JavaScript and TypeScript](/integration/js). For more information about emulator and Metro failures in CI, see [CI with Appium](/integration/ci-appium).
