Use this guide to generate code coverage for a project that uses the NDK build system. The workflow is the same idea RKTracer uses everywhere: you prefix the rktracer command to your existing ndk-build command, and RKTracer instruments the code during compilation. The runtime libraries are added automatically, so you can run the instrumented application on a native host or on an embedded system, and then generate the coverage report. RKTracer checks the compiler variables, instruments the source files, and produces Function Coverage, Line Coverage, Statement Coverage, and Multi-Condition Code Coverage in the HTML reports.
Prefix rktracer to the ndk-build command, run your tests, pull the rk-coverage.txt file from the device, then run rkresults to generate the HTML report.
Step 1: Enable the RKTracer tool
Prefix the rktracer command to your ndk-build command. For example, if your original command is:
ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=.\app\Android.mk v=1
Prefix rktracer to enable the coverage tool:
rktracer ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=.\app\Android.mk v=1
RKTracer checks the compiler variables and instruments the source files to build the project.
What happens during instrumentation
RKTracer preprocesses each source file, then instruments the preprocessed file (with the bundled librklic compiler) and stores the metadata in JSON format. It adds the runtime automatically during linking.
Step 2: Run your tests on the instrumented application
After RKTracer generates the shared library, push it to the target device or add it to the application APK. Run your tests against the RKTracer-instrumented application. RKTracer autosaves the coverage data from the device to the application working folder on the build machine. If the device is disconnected, the coverage data is saved in logcat. Pull the coverage data file (rk-coverage.txt) with the adb pull command:
adb pull /data/data/com.example.widecolor/rk-coverage.txt
Step 3: Generate the HTML coverage report
Once you have the rk-coverage.txt file, run the rkresults command in the application root folder to generate the HTML reports.
rkresults
The rkresults command maps the rk-coverage.txt coverage data file with the JSON metadata files generated by RKTracer during instrumentation, and generates the HTML reports.
Enable coverage for selected files and functions
You can scope coverage to specific folders, files, or functions by editing rktracer.config.
Coverage for selected folders
For example, to instrument source files from some folders while ignoring others, given folders such as:
C:\project\sound\drivers\base\power C:\project\sound\drivers\base\power\firmware_loader C:\project\sound\drivers\core C:\project\sound\security\keys C:\project\sound\security\lockdown
Add the following at the end of rktracer.config:
ignore *.c instrument */power/* */core/* */keys/* never */firmware_loader/* */lockdown/*
ignore *.cignores all C source files from instrumentation.instrumentinstruments source files from the given folders.neverignores the selected folders.
Coverage for selected functions
To generate coverage only for specific functions in specific files, edit rktracer.config:
ignore *.c instrument *source-file-X.c *source-file-Y.c *source-file-Z.c function-ignore * function-instrument fun_X() fun_Y() fun_Z()
ignore *.cignores all C source files.instrument *source-file-X.c *source-file-Y.c *source-file-Z.cinstruments only these three source files.function-ignore *then ignores all functions in the above three files.function-instrument fun_X() fun_Y() fun_Z()does not ignore these three functions from instrumentation.
Next steps
Once your NDK project reports coverage, the same RKTracer workflow applies across other build systems and IDEs. Browse the rest of the documentation, or read more about how RKTracer measures coverage through MC/DC.