Android automatic and manual testing using Jacoco

Md Ali Hossain
2 min readSep 18, 2018


Code Coverage:

Code Coverage is a method of finding out the measure of the developed source code while performing unit and functional testing.


Jacoco is a very effective and promising open-source Code Coverage library developed by EclEmma team for Java and Android projects.

Code Coverage can be easily achieved for unit and functional tests for either of the testing ways mentioned below:

a. Automation Testing

b. Manual Testing

Automation Testing Steps in Android:

  1. First install Android Studio and import an working Android project
  2. Configure build gradle

Go to app/build.gradle path in the project and then add this:

apply plugin: jacoco
toolVersion = “0.8.1” // try a newer version if you can

also add this under buildtypes:

debug {
testCoverageEnabled = true

3. Execute and generate Code Coverage report using gradle command

To generate the code coverage report you need to build a gradle task in build.gradle file like below:

task jacocoTestReport(type:JacocoReport) {
group = "Reporting"
description = "Generate Jacoco coverage reports"
// exclude auto-generated classes and tests
def fileFilter = ['**/R.class', '**/R$*.class',
'**/BuildConfig.*', '**/Manifest*.*',
def debugTree = fileTree(dir:
excludes: fileFilter)
def mainSrc = "${project.projectDir}/src/main/java"
sourceDirectories = files([mainSrc])
classDirectories = files([debugTree])
additionalSourceDirs = files([
executionData = fileTree(dir: project.projectDir, includes:
['**/*.exec', '**/*.ec'])
reports {
xml.enabled = true
xml.destination = "${buildDir}/jacocoTestReport.xml"
csv.enabled = false
html.enabled = true
html.destination = "${buildDir}/reports/jacoco"

Now execute below command to generate the report:

./gradlew jacocoTestReport

Open the index.html file in app/build/reports/jacoco/jacocoTestReport/html/ folder and have a look.

Manual testing Steps in Android:

To test it out, put below code in onPause() or some other lifecycle method of an Activity.

public class JacocoReportGenerator {
static void generateCoverageReport() {
String TAG = "jacoco";
// use reflection to call emma dump coverage method, to avoid
// always statically compiling against emma jar
Log.d("StorageSt", Environment.getExternalStorageState());
String coverageFilePath = Environment.getExternalStorageDirectory() + File.separator + "coverage.exec";
File coverageFile = new File(coverageFilePath);
try {
Class<?> emmaRTClass = Class.forName("com.vladium.emma.rt.RT");
Method dumpCoverageMethod = emmaRTClass.getMethod("dumpCoverageData",
coverageFile.getClass(), boolean.class, boolean.class);

dumpCoverageMethod.invoke(null, coverageFile, false, false);
Log.e(TAG, "generateCoverageReport: ok");
} catch (Exception e) {
throw new RuntimeException("Is emma jar on classpath?", e)

Run the app and do your testing. Once you are done execute the following adb command to pull the coverage file from sdcard:

adb pull /sdcard/coverage.exec app/build/outputs/coverage.exec

‘app/build/outputs/coverage.exec’ denotes the path where the coverage file needs to be copied in the project workspace. Delete if already exist any .exec file this directory.

Now you can either generate the report using the gradle task been shown earlier or you can import the .exec file in android studio by following:

Analyze -> Show coverage data -> import the .exec file

It will show the code coverage report.



Md Ali Hossain

Senior Android Engineer @Delivery Hero | Android developer | Kotlin lover | Flutter explorer | Problem Solver