|
| 1 | +# Android Native Coverage — Local Testing Guide |
| 2 | + |
| 3 | +How to test Android native (Kotlin/Java) code coverage collection with a local React Native library module. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Android SDK with an emulator image |
| 8 | +- Java 11+ (for JaCoCo CLI) |
| 9 | +- A React Native library with an `android/` module and an example/playground app |
| 10 | + |
| 11 | +## 1. Install the coverage package |
| 12 | + |
| 13 | +From your library's example/playground app directory: |
| 14 | + |
| 15 | +```bash |
| 16 | +# If using the harness monorepo locally (linked): |
| 17 | +pnpm add @react-native-harness/coverage-android --workspace |
| 18 | + |
| 19 | +# Or from npm (once published): |
| 20 | +npm install --save-dev @react-native-harness/coverage-android |
| 21 | +``` |
| 22 | + |
| 23 | +## 2. Build the app with coverage instrumentation |
| 24 | + |
| 25 | +The init script handles everything — no changes to your `build.gradle` needed. |
| 26 | + |
| 27 | +```bash |
| 28 | +cd android |
| 29 | + |
| 30 | +./gradlew assembleDebug \ |
| 31 | + --init-script ../node_modules/@react-native-harness/coverage-android/scripts/harness-coverage-init.gradle \ |
| 32 | + -PHarnessCoverageModules=:mylib |
| 33 | + |
| 34 | +cd .. |
| 35 | +``` |
| 36 | + |
| 37 | +Replace `:mylib` with your library's Gradle module path (e.g. `:react-native-my-lib`, `:android`). You can instrument multiple modules: `-PHarnessCoverageModules=:moduleA,:moduleB`. |
| 38 | + |
| 39 | +### What the init script does |
| 40 | + |
| 41 | +- Adds JaCoCo offline instrumentation to the specified modules' compiled classes |
| 42 | +- Injects `CoverageHelper` + `CoverageInitProvider` into the debug build |
| 43 | +- Saves original (uninstrumented) class files + `jacococli.jar` to `<module>/build/harness-coverage/` |
| 44 | +- Adds `BuildConfig.COVERAGE_ENABLED = true` |
| 45 | + |
| 46 | +### Verify instrumentation worked |
| 47 | + |
| 48 | +```bash |
| 49 | +javap -p android/<module>/build/tmp/kotlin-classes/debug/com/example/MyClass.class | grep jacoco |
| 50 | +``` |
| 51 | + |
| 52 | +You should see `$jacocoInit` — that means JaCoCo probes are present. |
| 53 | + |
| 54 | +## 3. Configure harness |
| 55 | + |
| 56 | +In your `rn-harness.config.mjs`: |
| 57 | + |
| 58 | +```javascript |
| 59 | +import { androidPlatform, androidEmulator } from '@react-native-harness/platform-android'; |
| 60 | + |
| 61 | +export default { |
| 62 | + entryPoint: './index.js', |
| 63 | + appRegistryComponentName: 'MyApp', |
| 64 | + runners: [ |
| 65 | + androidPlatform({ |
| 66 | + name: 'android', |
| 67 | + device: androidEmulator('Pixel_8_API_35'), |
| 68 | + bundleId: 'com.example.myapp', |
| 69 | + }), |
| 70 | + ], |
| 71 | + coverage: { |
| 72 | + native: { |
| 73 | + android: { |
| 74 | + modules: [':mylib'], |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | +}; |
| 79 | +``` |
| 80 | + |
| 81 | +The `modules` array must match the module paths you passed to the init script. |
| 82 | + |
| 83 | +## 4. Run tests with coverage |
| 84 | + |
| 85 | +```bash |
| 86 | +npx react-native-harness --coverage --harnessRunner android |
| 87 | +``` |
| 88 | + |
| 89 | +After tests complete, the harness will: |
| 90 | + |
| 91 | +1. Stop the app (triggers `am force-stop`) |
| 92 | +2. Wait 2 seconds for the JaCoCo flush timer to write final data |
| 93 | +3. Pull `.ec` files from the app's internal storage via `adb` |
| 94 | +4. Merge them using `jacococli.jar` (from the build output) |
| 95 | +5. Generate an XML report using the original (uninstrumented) class files |
| 96 | +6. Convert to lcov format |
| 97 | + |
| 98 | +Output: `native-coverage.lcov` in the project root. |
| 99 | + |
| 100 | +## 5. View the report |
| 101 | + |
| 102 | +```bash |
| 103 | +# Quick summary |
| 104 | +grep -c "^DA:" native-coverage.lcov |
| 105 | +# -> number of instrumented lines |
| 106 | + |
| 107 | +# Generate HTML (requires lcov tools) |
| 108 | +genhtml native-coverage.lcov -o coverage-html |
| 109 | +open coverage-html/index.html |
| 110 | +``` |
| 111 | + |
| 112 | +## How it works |
| 113 | + |
| 114 | +### Build time |
| 115 | + |
| 116 | +The Gradle init script hooks into `compileDebugKotlin` (and `compileDebugJavaWithJavac` if present). After compilation: |
| 117 | + |
| 118 | +1. Copies original `.class` files to `build/harness-coverage/original-classes-kotlin/` (needed for reports since instrumented classes have different bytecode) |
| 119 | +2. Runs JaCoCo's `InstrumentTask` to rewrite `.class` files with coverage probes |
| 120 | +3. Copies `jacococli.jar` to `build/harness-coverage/` so it's available at report time without needing Gradle |
| 121 | + |
| 122 | +### Runtime |
| 123 | + |
| 124 | +`CoverageInitProvider` (a `ContentProvider`) bootstraps `CoverageHelper.setup()` before any Activity starts. The helper: |
| 125 | + |
| 126 | +- Writes coverage data to `context.filesDir/coverage-{pid}.ec` every 1 second via a daemon timer |
| 127 | +- Also flushes on `onActivityStopped` |
| 128 | + |
| 129 | +Each app restart (the harness restarts per test suite) gets its own `.ec` file keyed by PID. |
| 130 | + |
| 131 | +### Collection |
| 132 | + |
| 133 | +The coverage collector pulls `.ec` files from the device by copying them to `/data/local/tmp/` via `adb shell run-as`, then using `adb pull` (which handles binary data correctly). It then uses `jacococli.jar` from the build output to merge and generate reports. |
| 134 | + |
| 135 | +## Troubleshooting |
| 136 | + |
| 137 | +### "Original class files not found" |
| 138 | + |
| 139 | +The build output wasn't found at test time. Make sure: |
| 140 | +- You built with `--init-script` and the correct `-PHarnessCoverageModules` |
| 141 | +- The `modules` in `rn-harness.config.mjs` match the Gradle module paths |
| 142 | +- If build and test run on different machines, transfer the entire `<module>/build/harness-coverage/` directory |
| 143 | + |
| 144 | +### "jacococli.jar not found" |
| 145 | + |
| 146 | +Same as above — the init script stashes `jacococli.jar` during the build. If it's missing, the build didn't use the init script. |
| 147 | + |
| 148 | +### "No .ec files found on device" |
| 149 | + |
| 150 | +The app didn't write coverage data. Check: |
| 151 | +- Was the app built with the init script? (`javap -p ... | grep jacoco`) |
| 152 | +- Did the app actually run? (check adb logcat for `HarnessCoverage` tag) |
| 153 | +- Is `BuildConfig.COVERAGE_ENABLED` true? |
| 154 | + |
| 155 | +### 0% coverage on everything |
| 156 | + |
| 157 | +The `.ec` data doesn't match the class files. This happens when you rebuild without re-running tests, or vice versa. Always use matching build + test runs. |
| 158 | + |
| 159 | +### `EROFS` crash on startup |
| 160 | + |
| 161 | +Missing `jacoco-agent.properties` with `output=none`. The init script injects this automatically — if you see this error, the init script wasn't applied correctly. |
0 commit comments