How to Integrate RKTracer with Android Studio IDE and generate code coverage
How to Integrate RKTracer plugin in Android Studio in Windows 10/Linux?
Step 1: Open Android Studio IDE.
Step 2: Go to the File menu→ Settings → Plugin on the left side of the screen and then on the right side, click the “setting” icon on the right side and select the “install plug-in from disk” option.
Step 3: Navigate to the RKTracer tool installation folder,
Select “RKTracer-Android-Plugin.jar” and select ok, to install the RKTracer plugin for Android Studio IDE.
Step 4: Now, we need to restart the android studio IDE. You should see RKTracer options under the Tools menu → The RKtracer option to turn ON or OFF or generate reports. There is also a quick turn ON/OFF option with a click mark in green color, as shown in the below screenshot.
How to generate Code coverage for application in android studio IDE?
Following Instructions will work for all the Apps with C/C++ or Java, or Kotlin, or in combination in Android Studio IDE. Ensure that you can build and test the app in android studio or on the phone device.
Step 1: Enable RKTracer Tool.
Goto → Tools→ RKTracer and select Switch Instrumentation ON.
Step 2: Rebuild the application.
You need to rebuild the app in android studio ide to the instrument with the RKTracer tool as instrumentation takes place at a source code level.
Step 3: Install the APK in the 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
Step 4:Generate Code Coverage Report
We can either use the Tools menu → rktracer → Generate Report option to generate a code coverage report or 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.
NOTE: Once the reports are generated, you need to Turn OFF the RKTracer tool. go to Tools menu -> rktracer -> Switch Instrumentation Off
Generate code coverage for selected functions/files/folder
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.
instrument */power/* */core/* */keys/*
never */firmware_loader/* */lockdown/*
ignore *.c = Ignores all C 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.
instrument *source-file-X.c *source-file-Y.c *source-file-Z.c
function-instrument fun_X() fun_Y() fun_Z()
ignore *.c = Ignore all C programming source files
instrument *file-X.c *file-Y.c *file-Z.c = 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.