Continuous Integration for Android

Learn how to use Continuous Integration for Android to be sure you have fully-verified and battle-tested code on the master branch. By Prashant Barahi.

4.8 (11) · 1 Review

Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Making the Build Fail

To make the build fail due to inadequate test cases, you need to create rules that define a threshold that JaCoCo verifies. If your test cases don’t meet the criteria, the build will fail.

In the app-level build.gradle file, add the following code:

// Task declaration
task jacocoTestCoverageVerification(type: JacocoCoverageVerification) {
    // Run only after the test reports are generated
    dependsOn = ['jacocoTestReport']
    enabled = true
    sourceDirectories.from = files([mainSrc])
    classDirectories.from = files([debugTree])
    executionData.from = fileTree(dir: project.buildDir, includes: [
            'jacoco/testDebugUnitTest.exec',
            'outputs/code_coverage/debugAndroidTest/connected/*.ec'
    ])

    violationRules {
        failOnViolation = true 
        // 1
        rule {
            enabled = true
            element = 'PACKAGE'
            includes = ['com.raywenderlich.android.simplecalculator.operators']
            limit {
                counter = 'CLASS'
                value = 'MISSEDCOUNT'
                maximum = 0
            }
        }
        // 2
        rule {
            element = 'PACKAGE'
            includes = ['com.raywenderlich.android.simplecalculator']
            limit {
                value = 'COVEREDRATIO'
                counter = 'INSTRUCTION'
                minimum = 0.8
            }
        }

    }
}
// Make the check gradle task depend on the above task so that failure of above task will fail the check task
check.dependsOn jacocoTestCoverageVerification

Similarly to the jacocoTestReport task, you’ve created and configured another task named jacocoTestCoverageVerification that gets executed after jacocoTestReport completes. Inside the violationRules block, there are two rules that the JaCoCo checks the test results against.

  1. Since adding features means adding a class in operators package in current case, make sure no class is left uncovered. Set the maximum missed count value to 0, which will fail the task if you add a class inside operators but don’t cover it with test cases.
  2. This defines rule that fails the build if the covered ratio of the Java byte-code instructions is less than 80%.

Currently, this is a successful task because the test cases fulfill the criteria set in the JaCoCo verification rules.

Passing test results

Now, comment out some test cases to see the task fail with corresponding reasons.

Failed task

Note: To run the verification task, use ./gradlew clean jacocoTestCoverageVerification.

Finally, add, commit and push the changes — the workflow executes successfully.

Great! You’ve integrated JaCoCo in the workflow. For more configuration options, check JaCoCo’s documentation.

Where to Go From Here?

Congrats! You’ve learned how to implement continuous integration for Android. You can download the complete project using the Download Materials button at the top or bottom of the tutorial.

But CI is only half of a whole. CD, short for Continuous Delivery is the practice of automating the delivery of the app to a location like the Google Play Store.

As a whole, the practice is called CI/CD, and it’s commonly used in Agile teams to increase software quality, shorten delivery cycles and optimize the feedback loop.

Leverage third-party actions to ease the workflow configuration. Actions like r0adkll/upload-google-play help you with CD.

Another helpful action lets you post the link to the artifacts in your Slack channel to inform your team about the new build.

Check out GitHub’s Marketplace to discover more.

Also, remember to check out our Continuous Integration course here.

If you have any comments or questions, feel free to join in the forum discussion below.