Using Jenkins for mobile projects – Part 2 (Android)

In the last blog post we spoke about how to install an iOS Jenkins-Node and configure a project for building and running all the necessary code metrics to ensure good code quality.

Based on that  (clean install of a OS X system, installation all the development and metric tools, knowing how to create a Jenkins project) we will cover what we need in addition to do the same for Android projects.

First step – Android SDK and build tools

Since Android Studio is the default IDE for doing Android projects for quite a while now and Gradle is the default build automation system of Android Studio, we will only cover Android projects that use Gradle.

The most important part for the beginning is installing Java, the Android SDK and Gradle.

brew cask install java
brew install android-sdk
brew install gradle

Now install all Android SDK versions and platform/build tools you need (the API levels starting at 15 are the important ones). Open the Android SDK Manager by just typing this:

android

Create the ANDROID_HOME variable and add it to the PATH variable (maybe your path to the Android SDK is different than mine):

export ANDROID_HOME=//android-sdk-macosx
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-to

Second step – Project / Gradle configuration

On Android it’s more project configuration than on iOS, e. g. for the unit tests you should use Robolectric, and for the code coverage there is a tool call JaCoCo that needs its own Gradle task.

Robolectric

Robolectric is used because the Android emulator is very slow, and for unit tests that require a context you usually need the emulator. With Robolectric you can just run the tests without needing an emulator.

Just add this to your build.gradle file under “dependencies”:

testCompile "org.robolectric:robolectric:3.0"

And to tell the Test that it should be run with Robolectric you just Annotate it with the Gradle test runner:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
    public class someTest {
}

JaCoCo

Enabling code coverage is a little bit more tricky because we need to add a lot of stuff to the build.gradle file (you can outsource this to an extra *.gradle-file if you want).

First thing is to enable test coverage. Do this by adding this in the guild.gradle file in the “buildTypes” section:

buildTypes {
    debug {
        testCoverageEnabled = true
    }
}

To enable JaCoCo you need these lines:

apply plugin: 'jacoco'

jacoco {
    toolVersion "0.7.5.201505241946"
}

Now the actual JaCoCo task:

task jacocoTestReport(type:JacocoReport, dependsOn: "testDebug") {  
    group = "Reporting"
    description = "Generate Jacoco coverage reports after running tests."
    reports {
        xml.enabled = true
        html.enabled = true
    }
    classDirectories = fileTree(
        dir: "./build/intermediates/classes/debug",
        excludes: ['**/R*.class',
                   '**/*$InjectAdapter.class',
                   '**/*$ModuleAdapter.class',
                   '**/*$ViewInjector*.class']
    )
    def coverageSourceDirs = [
        "src/main/java"
    ]
    sourceDirectories = files(coverageSourceDirs)
    executionData = files("${project.buildDir}/jacoco/${testTaskName}.exec")
}

Remember that this is just an example of how the task could look like, you find a user guide on JaCoCo here: https://docs.gradle.org/current/userguide/jacoco_plugin.html

When the task is properly configured you will get several files as output, where the most useful for us is the HTML report that will by default be located at [TARGET]/build/reports/jacoco/jacocoTestReport/html.

Third step – Jenkins project configuration

The only things we need to change compared to the iOS project is to remove the last two shell execution lines for executing Unit Tests and measuring Code Coverage, replace the Xcode build step with the Gradle build step and replace the Cobertura results with the JaCoCo HTML results (and of course we don’t need any of the Apple developer profile steps).

1. Invoke Gradle script

Add build step, select „Invoke Gradle script“ and enter the Gradle tasks to perform and the path to the build.gradle file.

2. Show JaCoCo result HTML

For this you need to have the Jenkins plugin „HTML Publisher plugin“ installed.
Add a post-build step „Publish HTML reports“ and enter the needed input fields.

So that’s it, hopefully we could support you on creating your CI environment or at least help you with a certain step you had problems with.