1*8975f5c5SAndroid Build Coastguard Worker# Android code coverage instructions 2*8975f5c5SAndroid Build Coastguard Worker 3*8975f5c5SAndroid Build Coastguard WorkerThese are instructions for collecting code coverage data for android 4*8975f5c5SAndroid Build Coastguard Workerinstrumentation and JUnit tests. For Clang(C++) code coverage refer to [clang coverage]. 5*8975f5c5SAndroid Build Coastguard Worker 6*8975f5c5SAndroid Build Coastguard Worker[TOC] 7*8975f5c5SAndroid Build Coastguard Worker 8*8975f5c5SAndroid Build Coastguard Worker## How JaCoCo coverage works 9*8975f5c5SAndroid Build Coastguard Worker 10*8975f5c5SAndroid Build Coastguard WorkerIn order to use JaCoCo code coverage, we need to create build time pre-instrumented 11*8975f5c5SAndroid Build Coastguard Workerclass files and runtime **.exec** files. Then we need to process them using the 12*8975f5c5SAndroid Build Coastguard Worker[build/android/generate_jacoco_report.py](https://source.chromium.org/chromium/chromium/src/+/main:build/android/generate_jacoco_report.py) script. 13*8975f5c5SAndroid Build Coastguard Worker 14*8975f5c5SAndroid Build Coastguard Worker## How to collect coverage data 15*8975f5c5SAndroid Build Coastguard Worker 16*8975f5c5SAndroid Build Coastguard Worker1. Use the following GN build arguments: 17*8975f5c5SAndroid Build Coastguard Worker 18*8975f5c5SAndroid Build Coastguard Worker ```gn 19*8975f5c5SAndroid Build Coastguard Worker target_os = "android" 20*8975f5c5SAndroid Build Coastguard Worker use_jacoco_coverage = true 21*8975f5c5SAndroid Build Coastguard Worker ``` 22*8975f5c5SAndroid Build Coastguard Worker 23*8975f5c5SAndroid Build Coastguard Worker Now when building, pre-instrumented files will be created in the build directory. 24*8975f5c5SAndroid Build Coastguard Worker 25*8975f5c5SAndroid Build Coastguard Worker2. Run tests, with option `--coverage-dir <directory>`, to specify where to save 26*8975f5c5SAndroid Build Coastguard Worker the .exec file. For example, you can run chrome JUnit tests: 27*8975f5c5SAndroid Build Coastguard Worker `out/Debug/bin/run_chrome_junit_tests --coverage-dir /tmp/coverage`. 28*8975f5c5SAndroid Build Coastguard Worker 29*8975f5c5SAndroid Build Coastguard Worker3. The coverage results of JUnit and instrumentation tests will be merged 30*8975f5c5SAndroid Build Coastguard Worker automatically if they are in the same directory. 31*8975f5c5SAndroid Build Coastguard Worker 32*8975f5c5SAndroid Build Coastguard Worker## How to generate coverage report 33*8975f5c5SAndroid Build Coastguard Worker 34*8975f5c5SAndroid Build Coastguard Worker1. Now we have generated .exec files already. We can create a JaCoCo HTML/XML/CSV 35*8975f5c5SAndroid Build Coastguard Worker report using `generate_jacoco_report.py`, for example: 36*8975f5c5SAndroid Build Coastguard Worker 37*8975f5c5SAndroid Build Coastguard Worker ```shell 38*8975f5c5SAndroid Build Coastguard Worker build/android/generate_jacoco_report.py \ 39*8975f5c5SAndroid Build Coastguard Worker --format html \ 40*8975f5c5SAndroid Build Coastguard Worker --output-dir /tmp/coverage_report/ \ 41*8975f5c5SAndroid Build Coastguard Worker --coverage-dir /tmp/coverage/ \ 42*8975f5c5SAndroid Build Coastguard Worker --sources-json-dir out/Debug/ \ 43*8975f5c5SAndroid Build Coastguard Worker ``` 44*8975f5c5SAndroid Build Coastguard Worker Then an index.html containing coverage info will be created in output directory: 45*8975f5c5SAndroid Build Coastguard Worker 46*8975f5c5SAndroid Build Coastguard Worker ``` 47*8975f5c5SAndroid Build Coastguard Worker [INFO] Loading execution data file /tmp/coverage/testTitle.exec. 48*8975f5c5SAndroid Build Coastguard Worker [INFO] Loading execution data file /tmp/coverage/testSelected.exec. 49*8975f5c5SAndroid Build Coastguard Worker [INFO] Loading execution data file /tmp/coverage/testClickToSelect.exec. 50*8975f5c5SAndroid Build Coastguard Worker [INFO] Loading execution data file /tmp/coverage/testClickToClose.exec. 51*8975f5c5SAndroid Build Coastguard Worker [INFO] Loading execution data file /tmp/coverage/testThumbnail.exec. 52*8975f5c5SAndroid Build Coastguard Worker [INFO] Analyzing 58 classes. 53*8975f5c5SAndroid Build Coastguard Worker ``` 54*8975f5c5SAndroid Build Coastguard Worker 55*8975f5c5SAndroid Build Coastguard Worker2. For XML and CSV reports, we need to specify `--output-file` instead of `--output-dir` since 56*8975f5c5SAndroid Build Coastguard Worker only one file will be generated as XML or CSV report. 57*8975f5c5SAndroid Build Coastguard Worker ```shell 58*8975f5c5SAndroid Build Coastguard Worker build/android/generate_jacoco_report.py \ 59*8975f5c5SAndroid Build Coastguard Worker --format xml \ 60*8975f5c5SAndroid Build Coastguard Worker --output-file /tmp/coverage_report/report.xml \ 61*8975f5c5SAndroid Build Coastguard Worker --coverage-dir /tmp/coverage/ \ 62*8975f5c5SAndroid Build Coastguard Worker --sources-json-dir out/Debug/ \ 63*8975f5c5SAndroid Build Coastguard Worker ``` 64*8975f5c5SAndroid Build Coastguard Worker 65*8975f5c5SAndroid Build Coastguard Worker or 66*8975f5c5SAndroid Build Coastguard Worker 67*8975f5c5SAndroid Build Coastguard Worker ```shell 68*8975f5c5SAndroid Build Coastguard Worker build/android/generate_jacoco_report.py \ 69*8975f5c5SAndroid Build Coastguard Worker --format csv \ 70*8975f5c5SAndroid Build Coastguard Worker --output-file /tmp/coverage_report/report.csv \ 71*8975f5c5SAndroid Build Coastguard Worker --coverage-dir /tmp/coverage/ \ 72*8975f5c5SAndroid Build Coastguard Worker --sources-json-dir out/Debug/ \ 73*8975f5c5SAndroid Build Coastguard Worker ``` 74*8975f5c5SAndroid Build Coastguard Worker3. If generating coverage and there are duplicate class files, as can happen 75*8975f5c5SAndroid Build Coastguard Worker when generating coverage for downstream targets, use the 76*8975f5c5SAndroid Build Coastguard Worker `--include-substr-filter` option to choose jars in the desired directory. Eg. 77*8975f5c5SAndroid Build Coastguard Worker for generating coverage report for Clank internal repo 78*8975f5c5SAndroid Build Coastguard Worker ```shell 79*8975f5c5SAndroid Build Coastguard Worker build/android/generate_jacoco_report.py --format html \ 80*8975f5c5SAndroid Build Coastguard Worker --output-dir /tmp/coverage_report/ --coverage-dir /tmp/coverage/ \ 81*8975f5c5SAndroid Build Coastguard Worker --sources-json-dir out/java_coverage/ \ 82*8975f5c5SAndroid Build Coastguard Worker --include-substr-filter obj/clank 83*8975f5c5SAndroid Build Coastguard Worker ``` 84*8975f5c5SAndroid Build Coastguard Worker 85*8975f5c5SAndroid Build Coastguard Worker[clang coverage]: https://chromium.googlesource.com/chromium/src/+/HEAD/docs/testing/code_coverage.md