Thursday, 24 April 2014

Continue integration: Jenkins, git and Android

As I stated in early posts, Jenkins is an excellent tool for continue integration because it could be integrated with repositories (git, svn) and for each commit or push, it could starts compile automatically and do the tests. After it, the result could be send via email.

For Android, it could be more useful. It could be used to run 64 emulators, each one with different configurations, so for each unit tests it could run on 64 configurations. It also could be used to keep the apk generated, so for each compilation, it could send the apk file to a set of receivers by email, so they can have always the last version of project.

In this post I am going to summarize the steps needed for a simple configuration and link process with a github project.

1. Install plugins
To run the android projects, it is needed to install two more plugins, beside the one installed.
1.1 From the main windows of Jenkins, go to Manage Jenkins -> Manage plugins
1.2 Select Availables
1.3. Find the follow plugins and check the checkbox
- Android Emulator Plugin (Compulsory)
- Android Lint Plugin (Optional)
- Git Plugin
- Git client plugin (It comes with Git Plugin)
1.4 Click on the button "Download now and install after restart". Then wait until Jenkins has been restarted.

2. Set Jenkins
After the plugins have been installed, there are some configurations to do
2.1 From the main windows of Jenkins, go to Manage Jenkins -> Configure System
2.2 On Android, check "Automatically install Android components when required". This will download the sdk and the emulators needed for the compilation. It will also set the ANDROID_HOME environment for Jenkins.



3. Create a new job for Jenkins
3.1 From the main windows of Jenkins, click on New Items
3.2 Set the Job name
3.3 Select "Build a free-style software project"
3.4 Click on Ok.

4. Set the git repository
4.1 In "Source Code Management", select Git
4.2 Enter the repository url. For example, it could be:

4.3 On the branches to build, leave it to "*/master" or change to any branch you want

5. Set the Emulator
5.1 On the "Build Environment", check the "Run an Android emulator during build"
5.2 Select "Run emulator with properties"
5.3 Enter the data for the emulator. For example, here is what I have done:
Android OS version: android-19
Screen density: 160
Screen resolution: WXGA800
Device locale: en_US
SD card size: 128M
Target ABI: armeabi-v7a



5.3 Disable the "Show emulator window".
5.4 Disable "Use emulator snapshots". It seems that after reboot, the Android emulator plugin has some problem to restart the emulator from the snapshot. I hope it could be fixed soon.

6. Set the ant data
Apache ant could be used to build, install and test the projects. The bad news is you will need ant, Maven or Gradle to install and test your project in the emulator. The good news is Android SDK comes with an tool to generate the ant files automatically.
Check here to see the steps.
I have set the ant file (build.xml) for the project above, so for this example you don't have to do anything.
6.1 On Build, click on "Add build step" and select "Invoke Ant"
6.2 Click on "Advanced ..." and enter the follow data:
Ant Version: Default
Targets: debug
Build File: build.xml
Properties: sdk.dir=$ANDROID_HOME

6.3 Click on "Add build steps" and select "Invoke Ant" again.
6.4 Click on "Advanced ..." again and enter the follow data
Ant version: Default
Targets: installd
Build File: build.xml
Properties: sdk.dir=$ANDROID_HOME


Check here for more ant options for Android:
Android developers - Building and Running from the Command Line

7. Save the project
7.1 Save the project by click on "Save" button

8. Running the project
8.1 On the project view, click on "Build Now" on the left panel and Jenkins will start build the project
8.2 Click on the link of the project to see the build
8.3 Click on the "Console output" on the left panel to see the progress.
If everything went well, the result of the project will be success.

Source:
Z├╝hlke Blog - Configure your Android project on Jenkins
Vitor Baptista - Continuos Integration for Android Apps with Jenkins and Maven 3
Android developers - Managing Projects fromm the Command Line
Android developers - Building and Running from the Command Line
Jenkins Wiki - building an Android app and test project
Vogella - Continuos Integration with Jenkins - Tutorial

No comments:

Post a Comment