en
de

Configure your Android project on Jenkins

9 October 2013
| |

Developing an Android application you won’t do without continuous integration. But setting up a Jenkins job for your Android project is not quite the same as for your Java projects. Furthermore, Android just introduced the new gradle build system. Here is a brief tutorial how to set up your Jenkins server so it can run builds for your Android project.

Gradle

In order to use the new build system you need to install the Gradle Plugin. After installing the plugin in you can create a new job and add a new build step “Invoke gradle script”. Here you can select “Use Gradle Wrapper” and configure the tasks which should be executed within the build (e.g. clean assemble).

configure_gradle

The next time the build runs, gradle will be installed automatically on your Jenkins server. Unfortunately, your build will fail because Jenkins has not found an Android SDK.

Installing the Android SDK

At this point, you should install the Android Emulator Plugin. By starting an emulator the plugin will install the Android SDK automatically within the build. In order to start an emulator you need to select “Run an Android emulator during build” within the “Build Environment” options of your Jenkis job. The fields will help you by providing a list of all possible values as soon as you start typing. Unfortunately, the plugin does not provide the latest Android version, but with a simple workaround you can select it, as well. The field “Android OS version” supports all Android versions until 4.2. If you are typing 4.3 into the field your build fails. But you can type the value “android-18” (being the official target ID) to use the latest version. Additionally, make sure that you deselect “Show emulator window”.

configure_android_emulator_marks

As soon as you are now running your job again, the Android SDK should be installed. Nevertheless, the build will fail again because you need to set the ANDROID_HOME manually. Therefore you need to add an environment variable inside your global Jenkins configuration.

set_android_home

If you are running your Jenkins on a 64-bit system, you need to install the ia32-libs, as well. Otherwise the emulator will not start.

sudo apt-get install ia32-libs

By now your build should work, starting an emulator and compiling your project.

Run Unit-Tests

If you have integrated unit-tests into your gradle build, just run the gradle task in your Jenkins job. To add a unit-test configuration, simply add the following code [1] into your projects build.gradle file.

sourceSets {
    testLocal {
        java.srcDir file('src/test/java')
        resources.srcDir file('src/test/res')
    }
}

dependencies {
    ...
    testLocalCompile 'junit:junit:4.10'
    testLocalCompile 'org.mockito:mockito-core:1.9.5'
}

task localTest(type: Test, dependsOn: assemble) {
    testClassesDir = sourceSets.testLocal.output.classesDir
    android.sourceSets.main.java.srcDirs.each { dir ->
        def buildDir = dir.getAbsolutePath().split('/')
        buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
        sourceSets.testLocal.compileClasspath += files(buildDir)
        sourceSets.testLocal.runtimeClasspath += files(buildDir)
    }
    classpath = sourceSets.testLocal.runtimeClasspath
}

check.dependsOn localTest

Now you can add the localTest target to you job configuration. You may not need an emulator here, hence, disable this option to make your build faster.

In order to display the test results in jenkins add the “Publish JUnit test result report” post-build action to your job. Generally the test-results can be found by using the pattern “**/build/test-results/*.xml”.

configure_unit_test_results

Run Instrument Tests

However, you need the emulator for your instrument test. Just run these tests by adding the gradle task connectedInstrumentTest to your job configuration.

You can show the test results by adding a “Publish Junit test result report” as explained above and the pattern **/build/instrumentTest-results/connected/*.xml.

Lint

Moreover, you can run Lint via Jenkins. But you need to run it via shell executions because there is no gradle integration, yet. Add an “Execute shell” build step and call

$ANDROID_HOME/tools/lint --xml MyAndroidProject/build/lint-results.xml MyAndroidProject

In order to showing the results in Jenkins, you need to install the Android Lint Plugin. Afterwards, you can add a “Publish Android Lint results” post-build action reffering to your lint-result.xml.

**/build/lint-results.xml

If you are adding both steps at once an error message may appear, telling you the file lint-result.xml was not found. Ignore this message because the file will be created within your next build.

Conclusion

Thanks to the Jenkins plugins, configuring Android jobs on your Jenkins server is very simple, although, you need to install the Android SDK on your server. Most certainly you can add other Jenkins actions and plugins to your job, as well.

References

[1] https://coderwall.com/p/ybds4w
*The Android robot logo has been reproduced or altered from a work created and shared by Google, and is used in accordance with the terms of the Creative Commons 3.0 attribution license.

Comments (11)

Kio Krofovitch

29 December 2014 at 22:54

Thanks for the post. I was having trouble with the syntax for adding multiple gradle tasks to the Jenkins config. Much appreciated!

Alejandro

5 January 2015 at 21:46

Finally a well written understandable guide to run Robotium with Jenkins. Just one question: does this works even if my project is under Maven instead of Gradle?

Thank you very much.

    Nina Hartmann

    Nina Hartmann

    9 January 2015 at 15:51

    Of course, it should be possible to configure this for a maven project. Using the Maven Project Plugin for Jenkins and executing the right goals should lead to the same result.

Marco Maccio

9 April 2015 at 16:19

Nice article saved my day !

Thanks Marco

Divorcio e Separação

28 March 2016 at 03:19

I blog often and I seriously thank you for your information. Your article has
truly peaked my interest. I am going to bookmark your website and keep checking for new information about once a week.
I subscribed to your Feed too.

Ashish Karpe

3 July 2017 at 13:41

I am using Jenkins on AWS linux instance !
When Show Emulator is selected :
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb start-server
* daemon not running. starting it now at tcp:5813 *
* daemon started successfully *
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb start-server
[android] Starting Android emulator
$ /var/lib/jenkins/tools/android-sdk/tools/emulator -engine classic -ports 5738,5739 -report-console tcp:5816,max=60 -prop persist.sys.language=de -prop persist.sys.country=DE -avd hudson_de-DE_160_HVGA_android-18_armeabi-v7a -no-snapshot-load -no-snapshot-save
WARNING: Force to use classic engine to support snapshot.
QXcbConnection: Could not connect to display localhost:10.0
emulator: WARNING: Classic qemu does not support SMP. The hw.cpu.ncore option from your config file is ignored.
[android] Emulator did not appear to start; giving up
[android] Stopping Android emulator
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb kill-server
Finished: NOT_BUILT

When Show Emulator is not selected :
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb start-server
[android] Starting Android emulator
$ /var/lib/jenkins/tools/android-sdk/tools/emulator -engine classic -ports 5734,5735 -report-console tcp:5842,max=60 -prop persist.sys.language=de -prop persist.sys.country=DE -avd hudson_de-DE_160_HVGA_android-18_armeabi-v7a -no-snapshot-load -no-snapshot-save -no-window
WARNING: Force to use classic engine to support snapshot.
ERROR: resizing partition e2fsck failed with exit code 8
xcb_connection_has_error() returned true
Failed to create secure directory (/run/user/1000/pulse): Permission denied
emulator: WARNING: Classic qemu does not support SMP. The hw.cpu.ncore option from your config file is ignored.
emulator: warning: opening audio output failed

emulator: WARNING: Requested adb port (5735) is outside the recommended range [5555,5586]. ADB may not function properly for the emulator. See -help-port for details.
emulator: Listening for console connections on port: 5734
emulator: Serial number of this emulator (for ADB): emulator-5734
[android] Emulator reported that the console is available on port 5,734
[android] Waiting for emulator to finish booting…
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
$ /var/lib/jenkins/tools/android-sdk/platform-tools/adb -s emulator-5734 wait-for-device shell getprop init.svc.bootanim
[android] Emulator reported that the startup process is ‘running’
ACCESS TO THIS WEBSITE IS DENIED
WHY ACCESS IS DENIED:
AS PER THE DOT COMPLIANCE THE REQUESTED PAGE IS NOT ALLOWED. APOLOGIES FOR THE INCOVENIENCE CAUSED
Contact your internet administrator if you require access to this site.

×

Sign up for our Updates

Sign up now for our biweekly updates.

This field is required
This field is required
This field is required

I'm interested in:

Select at least one category
You were signed up successfully.