Automate code coverage in Android development using Bitrise and Codecov

by Solomon Eseme

.

Updated Mon Apr 24 2023

Automate code coverage in Android development using Bitrise and Codecov

Code Coverage is simply the extent to which your source code is covered either by testing Unit or UI and automating the continuous sending of code coverage using Bitrise and Codecov will boost your Android development team‘s productivity. 

Testing your source code is very vital. After all, it’s an essential aspect of software development because it helps the developer build a scalable, maintainable, and bug-free app. 

Codecov is a tool that makes it easy to send your code coverage using CI platforms like Bitrise, Github Action, Bitbucket CI, Jenkins, Circle CI, etc.

What is Code coverage?

Code coverage is the measurement of the depth your test covered within your source code. It may be generated locally using a default Gradle command or by using Jacoco. 

Developers that write a test for their projects without generating a code coverage.

What is Bitrise

Bitrise is a Continuous Integration and Delivery (CI/CD) Platform as a Service (PaaS) with the main focus on mobile app development (iOS, Android, React Native, Flutter, and so on). It is a collection of tools and services for the automation and development of mobile projects.

The reason why mobile developers choose it over other CICD platforms are below:

  1. Bitrise is mobile-focused
  2. Easy to set up a CI pipeline
  3. UI has a good Ux
  4. Easy to understand
  5. Has a custom script for your manual workflows
  6. Drag and do workflow with few parameters
  7. Has an active support team/community

You can check out an easy to get started with Bitrise article on this platform.

Codecov

Codecov is a tool that makes it easy to send your code coverage using CI platforms like Bitrise, Github Action, Bitbucket CI, Jenkins, Circle CI. It helps developers automate the process of sending code coverage, beautify the code coverage on their platform.

Implement code coverage in your Android project

In this section, we will walk through how to add Jacoco to your Android Studio to enable it to generate the supported format of Code Coverage by Codecov.

Add Jacoco to your project:


plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'jacoco'
}

task jacocoTestReport(type: JacocoReport, dependsOn: ['testDebugUnitTest', 'createDebugCoverageReport']) {

    reports {
        xml.enabled = true
        html.enabled = true
    }

    def fileFilter = ['**/R.class', '**/R$*.class', '**/BuildConfig.*', '**/Manifest*.*', '**/*Test*.*', 'android/**/*.*']
    def debugTree = fileTree(dir: "${buildDir}/intermediates/classes/debug", excludes: fileFilter)
    def mainSrc = "${project.projectDir}/src/main/java"

    sourceDirectories.setFrom(files([mainSrc]))
    classDirectories.setFrom(files([debugTree]))
    executionData.setFrom(fileTree(dir: "$buildDir", includes: [
            "jacoco/testDebugUnitTest.exec",
            "outputs/code-coverage/connected/*coverage.ec"
    ]))
}
testOptions {
    unitTests.all {
        jacoco {
            includeNoLocationClasses = true
        }
    }
    unitTests.returnDefaultValues = true
}
buildTypes {
    debug {
        testCoverageEnabled true
    }
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}

At this point, you will sync your project in the Android Studio so Jacoco can generate Gradle commands for you.

Now that JaCoCo is in your Android project, you can generate your code coverage in HTML format. The following Gradle command generates a test coverage report for your project:

.gradlew connectedCheck

This command will generate a report for your unit test alone:

.gradlew testDebugUnitTest

And this will generate the report for an instrumented test:

.gradlew connectedDebugAndroidTest

Before sending your code coverage online, you can view it locally in your system by navigating to the Android project folder > app > build > reports > androidTests > connected > flavors > debugAndroidTest > Index.html_

The above is for instrumented tests, while the below is for the Unit test.

For the unit test, navigate to _…reports > tests > testDebugUnitTest > Index.html_. After clicking on _index.html_ then view it on your browser. 

Before pushing your code coverage report to Codecov, take note of the report formats Codecov supports:

  • .xml (Cobertura XML, JaCoCo XML, etc.)
  •  .json (Erlang JSON, Elm JSON, etc.)
  •  .txt (LCOV, Gcov, Golang)

You’ll need the JaCoCo XML report format and a Codecov Token. You can sign up with Codecov to retrieve your token.

Writing the workflow in Bitrise

Firstly, you will need to create an account with Bitrise and set up your Workflow.

Below is the workflow required to send code coverage to the Codecov platform.

I will explain the steps above that make up the workflow that sends code coverage to Codecov.

Activate SSH key(RSA private key)

This step makes sure Bitrise has access to your repository and can clone your code to our virtual machines. The step saves the provided private key of your SSH key pair to a file and then loads it into the user’s ssh-agent with ssh-add.

Git Clone Repository:

The checkout process depends on the checkout properties: 

The step either checks out a repository state defined by a git commit or a git tag or achieves a merged state of a pull/merge request. 

The step uses two solutions to achieve the merged state of the pull/merge request: auto merge in the case of a merged branch or diff file (provided by the Git service) and manual merge otherwise.

Bitrise.io Cache: Pull

Downloads the build cache from bitrise.io and moves the files back to their original place. The aim is to speed up your builds while not having to download every single dependency every time can be a serious timesaver.

Install missing Android SDK components

This step makes sure that required Android SDK components (platforms and build-tools) are installed. To do so, the step runs the gradlew dependencies command.

If the Android Plugin for the Gradle version is 2.2.0 or higher, the plugin will download and install the missing components during the Gradle command.

Otherwise, the command fails, and the Step parses the command’s output to determine which SDK components are missing and installs them.

Android Lint

It highlights the code line where the error is, explains the error type, and suggests corrections. The step does not make the build fail if it spots any structural errors in your code. 

If you have inserted the Deploy to Bitrise.io Step in the Workflow, the test results will be available in a viewable or downloadable Lint Report HTML or XML file, which you can access on the Build’s APPS & ARTIFACTS page.

Android Unit Test

This step runs your Android project’s unit tests.

Codecov

Upload your code coverage files to Codecov.io to configure this step, to enable it to send your test report. 

A token is required to identify which project the coverage belongs to:

Now that you know what each step does in the Bitrise platform, you also need to know the supported code coverage format in Codecov.

below are the supported format:

  • .xml (Cobertura XML, JaCoCo XML, etc.)
  • .json (Erlang JSON, Elm JSON, etc.)
  • .txt (LCOV, Gcov, Golang)

At this point, you can push changes to your repository and check your Codecov dashboard to see your report.

Conclusion

Codecov is essential to every mobile developer that wants to ship bug-free, maintainable, and awesome projects with Bitrise and Codecov. 

We have discussed how to automate the process of sending your code coverage to Codecov and how to implement it using Bitrise.

You can easily send code coverage in less than 5 minutes to your Codecov dashboard.

Written by Wisdom.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board

Backend Tips, Every week

Backend Tips, Every week