Configure your Android project on Jenkins

from on 09.10.2013
10
Android Robot
(Quelle istock)

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

Write a comment

Your e-mail address will not be published or shared with third parties. Fields marked with * are required.

10 comments for “Configure your Android project on Jenkins

  1. Francisco

    Hello,

    I have a Jenkins configuration running Firefox, Chrome and IE by Continuous Integration. It is possible to combine them with this Android plugin? Or it will create conflicts?

    Thanks!
    Francisco.

  2. Margene Varney

    I blog οften and I really appreciate your cоntеnt. This article hаs really peaked my іnterest. I'm going to book mark your site and keep checking fοr new details about once per week. I subsсribed to your RSS feed as well.

  3. Benjamin

    Really helpful, it works like a charm.
    Since your blog post, Gradle can run Lint, you just have to add the task "lint". More, it's now useless to specify the lint-result.xml file, it will search by default with "**/lint-results.xml".

    Thank you !

  4. Pingback: The curious case of the Gradle wrapper and source control | MbcDev

  5. Alejandro

    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.

  6. Divorcio e Separação

    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.