Code Coverage Gradle build Java Applications

You need to build and test the application with gradle build.RKTracer tool will automatically add a runtime library to the application when you integrate and build the application with the RKTracer tool.

To instrument with the RKTracer tool, you need to enable RKTracer before running the gradle build command to build the application. You should always rebuild the project to instrument application with the RKTracer tool.

gradle build

Enable RKTracer tool “rktracer gradle -on build.gradle”

gradle rebuild

RKTracer tool makes internal adjustments to gradle build to instrument all the source files compiled at build time. By default, rktracer instruments for multiple-condition code coverage, and you see the following code coverage information in the HTML report.

  • Function Coverage
  • Line Coverage 
  • Statement Coverage
  • Multi-Condition Code Coverage

Code Coverage Gradle build example

Here is an example application to demonstrate how code coverage can be generated with Gradle build system.

Enable RKTracer Tool

gradle build

Enable RKTracer tool “rktracer gradle -on build.gradle”

gradle rebuild

code coverage gradle build java apps step 1
Code coverage Gradle java apps step 1
  • RKTracer instruments the preprocessed file with “c:\rktracer/lib/librklij-x86_64-w64-mingw32.exe ” and writes out the instrumented file the “” file in the preprocessed directory. Also, it stores the metadata, structure of instrumentation, and copy of source file content in JSON file. The tool will refer rktracer.config for internal configuration at the time of instrumentation.
  • RKTracer automatically adds runtime at linking time.

Run tests on instrumented application

When you run unit tests or functional testing the RKTracer tool will save coverage data to the rk-coverage.txt file on the host or phone device. If your running unit tests on the host and see the rk-coverage.txt file you can jump to the third step to generate code coverage HTML reports. 

If your doing unit or functional testing on a phone device, then you need to Install the rktracer instrumented APK in the phone device.

By default, the RKTracer tool will autosave coverage data from the emulator or device to the app working folder in the build machine provided the device or emulator is connected to the build machine. If the device/phone is not connected to the build machine at the time of testing then RKTracer coverage data will be saved in the logcat buffer and in the testing device. You can later save coverage data from logcat log or pull coverage data from device to app folder in the build machine

To pull the coverage data, you can execute the command. 

adb pull  /data/data/com.example.widecolor/rk-coverage.txt 

Generate Code Coverage HTML reports

Once you have the rk-coverage.txt file, you need to run the command “rkresults” to generate HTML reports. Ensure that you run the “rkresults” command in the application root folder or where you have the “rktracer” folder generated at the time of build/Instrumentation.

Open a terminal or command prompt in the application directory where have the rktracer folder and execute the following command to generate reports.

 Linux: “rkresults app/build.gradle” to generate reports. 

Windows: “rkresults app\build.gradle” to generate reports. 

If the RKTracer Html reports are not opening in the browser, we can navigate the project folder/rktracer/reports/ and open index.html to check the reports.

code coverage gradle build java apps step 4
Code coverage Gradle Java apps step 3

You can manually open the HTML report using index.html.

code coverage gradle build java apps step 3
Code Coverage reports

NOTE: Once the reports are generated, you need to disable the RKTracer tool with “rktracer gradle -off  build.gradle”

Enable coverage for selected files

Generate coverage reports  for the selected folders






Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and ignore coverage for folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder and go to the end of the file add the following information. 

ignore *.java

instrument */power/* */core/* */keys/*

never */firmware_loader/* */lockdown/*

ignore *.java = Ignores all Java source files from instrumentation

Instrument = Instrument source files from given folders

never = ignore selected folder  

Generate coverage reports only for selected functions from three different files.

fun_X() in source-file-X 

fun_Y() in source-file-Y

fun_Z() in source-file-Z

Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder and go to the end of the file and set the following variables as shown below.  

ignore *.java

instrument * * *

function-ignore *

function-instrument fun_X() fun_Y() fun_Z()

ignore *.java = Ignore all Java programming source files

instrument *  *  *  = Instrument only these three source files.

function-ignore * = Then ignore all functions in the above three files.function-instrument fun_X() fun_Y() fun_Z() = Don’t ignore these three functions from these three files from instrumentation

Similar Posts