How Can We Help?
< All Topics
Print

Code Coverage for Java/Kotlin Apps in Android Studio IDE

Integrate RKTracer with Android Studio IDE and generate code coverage for Java/Kotlin Apps

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, 

Windows: “C:\rktracer\share\rktracer-plugins”

Linux: “/home/username/rktracer/share/rktracer-plugins”

Select “RKTracer-Android-Plugin.jar” and select ok, to install the RKTracer plugin for Android Studio IDE.

 

Step 4: Now, we need to close the android studio IDE and open it. 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.

Generate Code coverage for Java-based 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 target device.

Step 1: Enable RKTracer Tool.

Goto → Tools→ RKTracer and select Switch Instrumentation ON.



Step 2: Rebuild the application

The RKTracer tool will instrumentation takes place at a source code level.

 

Step 3: Install the APK in the device

Don’t disconnect the device from the host machine and execute the command by opening the command prompt or terminal from the project directory, i.e., parallel to the rktracer folder(rktracer folder will get generate once we instrument the project with the rktracer tool). So that coverage data from logcat will get collected from the device. Then start testing the app on the device.

adb -e logcat > rk-coverage.txt 2>&1

This command will write code coverage data from logcat to the rk-coverage.txt file. You should not change the file name, i.e., rk-coverage.txt. RKTracer tool will consider the rk-coverage.txt file as a coverage data file.RKTracer will also, by default, write to the device. The RKTRacer tool will try to write at multiple places, and you check this in logcat output. Here is the sample logcat output when I search for the keyword “rk-coverage.”

2021-05-19 20:17:39.947 1203-4189/com.google.android.gms I/SQLiteCastStore: [CastNetworkInfo] saved: 0, skipped: 0, [CastDeviceInfo]: saved 0, skipped 1, [Paired Guest Mode DeviceInfo] saved: 0, skipped: 0. [ProbedNetworks]: saved 0, skipped: 0, [ProbedSocketAddress]: saved 0, skipped: 0, [Network-Device pairs]: saved: 0

2021-05-19 20:17:58.173 4718-4753/? I/rktracer: ‘/sdcard/rk-coverage.txt’ Operation not permitted

2021-05-19 20:17:58.174 4718-4753/? I/rktracer: writing to ‘/data/data/com.example.widecolor/rk-coverage.txt’

2021-05-19 20:26:39.240 5053-5084/? I/rktracer: ‘/sdcard/rk-coverage.txt’ Operation not permitted

2021-05-19 20:26:39.241 5053-5084/? I/rktracer: writing to ‘/data/data/com.example.widecolor/rk-coverage.txt’

2021-05-19 20:27:39.428 5181-5207/? I/rktracer: ‘/sdcard/rk-coverage.txt’ Operation not permitted

2021-05-19 20:27:39.428 5181-5207/? I/rktracer: writing to ‘/data/data/com.example.widecolor/rk-coverage.txt’

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

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

We need to copy the pulled rk-coverage.txt file to the project folder where we have the rktracer folder in the project directory.

 

Step 4: Generate Code Coverage Reports

We can either use the Tools menu → rktracer → report option to generate a report or open a terminal or command prompt in the project directory. We 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.

Coverage data file not found after testing.

RKTracer tool uses the following approaches to save coverage data.

  • Run unit/functional testing, and at the end RKTracer tool will automatically save coverage data on the host/embedded target/server machine, that is, write coverage data to the rk-coverage.txt file.
  • Send a signal to the application running in an infinite loop to trigger and save coverage data. 
  • The application under test has crashed in the middle of testing on the host or target device. RKTracer saves coverage data to rk-coverage.txt and generate reports to check the point of a crash in reports.
  • Embedded devices without a file system or not connected to host while testing. In this case, rktracer RAW mode comes in to picture. You need to save the coverage data from the RKTracer variable. 

Suppose you cannot find the rk-coverage.txt file in the project working folder or target server machine. An application under test has not exited within the proper shutdown hook, i.e., Runtime. get runtime().addShutdownHook(new Thread()), or your application might be running in an infinite loop or live application

Save coverage data when a particular function is executed at runtime testing.

Identify a function/method name in your application (ensure this function/method is not excluded from RKTracer instrumentation or file with this function). Then copy the function name and edit the rktracer.config in the RKTracer installation folder. 

For Java programming language:  Add a new line at the end of rktracer.config file with “java-function-writeout” and function name. Here the function name is case sensitive, and you should also add empty braces, i.e., () at the end of a function name. In case if you have a function with an input parameter, it should still be empty braces. You can add multiple function names with one space to the list, as shown below.

  • java-function-writeout main()
  • java-function-writeout main()  OnCreate() fun()

For Kotlin programming language: Add a new line at the end of rktracer.config file with “kotlin-function-writeout” and function name. Here the function name is case sensitive, and you should also add empty braces, i.e., () at the end of a function name. In case if you have a function with an input parameter, it should still be empty braces. You can add multiple function names with one space to the list, as shown below.

  • kotlin-function-writeout main()
  • kotlin-function-writeout main()  OnCreate() fun()

After adding function name to rktracer.config file. You need to rebuild the application to re-instrument using rktracer. Coverage data will be saved to the rk-coverage.txt file when this function executes at runtime testing. 

Send a signal to save coverage data.

We can send a signal to the application running in an infinite loop to save coverage data to rk-coverage.txt.

  • Windows: send a kill signal using keys in the keyboard, i.e., Control+Pause/break. Contact support if you would like to add additional signal command to save coverage data at runtime testing.
  • Linux/Unix: Send a SIGWINCH signal: killall -SIGWINCH a.out or kill -SIGWINCH Process ID number.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

Generate code coverage for selected functions/files/folder

Generate coverage reports  for the selected folders

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\

 

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 *source-file-X.java *source-file-Y.java *source-file-Z.java

function-ignore *

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

 

ignore *.java = Ignore all Java programming source files

instrument *file-X.java  *file-Y.java  *file-Z.java  = 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() = But instrument or don’t ignore these three functions from these three files.

Previous Code Coverage for Gradle Java Projects
Next Code Coverage for Selenium java web application Eclipse IDE
Table of Contents