android10

  • Increase font size
  • Default font size
  • Decrease font size

Android Continuous Integration: Using maven

E-mail Print
( 3 Votes )
Share

Introduction

"Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible."
"Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible."
Martin Fowler

We have written some tests for our project and now we would like to take continuous integration into account, mainly if we are working in an enterprise development environment and our team has several developers.
We all are very much accustom to use Eclipse and Android ADT as our main developer environment, so it's logical that even though we are increasing our team size we don't want to left behind that experience and start using a different tool from scratch. At the same time, it's pretty clear that using maven as a build tool for the project we obtain a great level of simplification, mainly if we use hudson, cruise control or similar continuous integration tools.
That's why the approach presented here is to let both models co-exist.



Project Structure

We are also considering here that a VCS is used and a parent project containing both the main and test project is under this VCS control.
The structure of our project P1 will be:

continuous_integration_maven_01

The P1 project including AP1 application's main project and its tests in AP1Test.
To create these projects you should follow this sequence:
  1. Create a Project (File -> New... -> Project), P1 in this case
  2. Add a pom.xml file (File -> New... -> Other -> Maven -> Maven POM file). We will review its content later
  3. Create an Android Project (New... -> Android Project), AP1, using P1 folder instead of the default location, but DON'T create the test project yet or it will fail (ADT 0.9.5)
  4. Add a pom.xml file to AP1 (File -> New... -> Other -> Maven -> Maven POM file). We will review its content later
  5. Selecting AP1 project, create the corresponding test project AP1Test (Android Tools -> New Test Project) using again P1 as the location
  6. Add a pom.xml file to AP1 (File -> New... -> Other -> Maven -> Maven POM file). We will review its content later
  7. You can now import the whole project structure to your VCS


Pom Files

You need to define some things inside your pom files in order to get the Android projects correctly built.

P1's pom.xml

We are using maven-android-plugin here, visit its site for further details.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>P1</groupId>
<artifactId>P1</artifactId>
<packaging>pom</packaging>
<version>0.0.1-SNAPSHOT</version>
 
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<!--
maven-android-plugin doesn't delete files from bin,
only from target, we are deleting them here
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.2</version>
<configuration>
<filesets>
<fileset>
<directory>bin</directory>
<includes>
<include>**/*</include>
</includes>
</fileset>
<fileset>
<directory>gen</directory>
<includes>
<include>**/*</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<configuration>
<sdk>
<!--
<path>${env.ANDROID_HOME}</path>
-->
<path>/opt/android-sdk/</path>
<platform>2.1</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
</configuration>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
<modules>
<module>AP1</module>
<module>AP1Test</module>
</modules>
<dependencies>
<dependency>
<groupId>android</groupId>
<artifactId>android</artifactId>
<version>2.1</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
</dependencies>
 
<dependencyManagement>
<dependencies>
<dependency>
<groupId>android</groupId>
<artifactId>android</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>

Basically we
  • define our source directory as src to be compatible with the Eclipse Android project structure
  • configure the maven-compiler-plugin according to our needs
  • clean Eclipse Android project structure too in our clean goal
  • configure maven-android-plugin defining the location of the Android SDK. This can be defined here or read from the environment
  • define our modules, in this case the main project AP1 and its tests AP1Test
  • define the dependencies, android 2.1 in this case
AP1's pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>AP1</groupId>
<artifactId>AP1</artifactId>
<packaging>apk</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<artifactId>P1</artifactId>
<groupId>P1</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

AP1Test's pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<project xmlns="http://maven.apache.org/POM/4.0.0" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>AP1Test</groupId>
<artifactId>AP1Test</artifactId>
<packaging>apk</packaging>
<version>0.0.1-SNAPSHOT</version>
<parent>
<artifactId>P1</artifactId>
<groupId>P1</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>AP1</groupId>
<artifactId>AP1</artifactId>
<version>0.0.1-SANPSHOT</version>
</dependency>
<dependency>
<groupId>AP1</groupId>
<artifactId>AP1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>apk</type>
</dependency>
</dependencies>
</project>



Populating your Local Repository

For maven to find android dependencies you must populate your local repository. To ease the task you can use android-mvn-install script which do all the hard work for you.
Just run

1
2
$ wget -qO - http://android.codtech.com/android-tools/android-mvn-install | bash -s -- \
  --sdk-dir=/opt/android

or download from its page and run it locally.
There's an alternative for this step, maven-android-sdk-deployer, however I prefer the simplicity and flexibility of the android-mvn-install script.



Building the project

Now we have several alternatives to build our project:
  • using Eclipse as usual for other Android projects using ADT plugin (i.e.: AP1 -> Run As -> Android Application)
  • using Maven from Eclipse (i.e.: P1 -> Run As -> Maven build)
  • using Maven from the command line (i.e.: mvn install)
  • using a continuous integration tool


External Libraries

If your project uses external libraries they should be added to the Eclipse project and to Maven dependencies as well. The latter case may also imply that you have to install those libraries in the local or remote Maven repository.

This example show how java activation framework was added to dependencies.

continuous_integration_maven_02

If activation is not on the repositories you have to add it using

1
2
$ mvn install:install-file -DgroupId=activation -DartifactId=activation -Dversion=1.1.1 -Dpackaging=jar \
-Dfile=/opt/java/jaf-1.1.1/activation.jar

To be able to use the double environment setup you should also add the external library to Eclipse. In project's Properties add the external jar file as displayed here.

continuous_integration_maven_03



Using Hudson

Having followed all the steps mentioned before we will now be able to create a job in hudson to build our project using maven.

continuous_integration_maven_04



Conclusion

There are still some rough edges and some things we haven't mentioned yet like running headless android emulators to be able to run the tests on the server. We will be covering these issues in future posts but I think that we have enough already to start applying continuous integration for our android projects.
Comments are gladly welcome.

Source

 
Comments (0)
Only registered users can write comments!
 

ANDROID10 --- TIP!!!

android10 tipIf you are writing an article and want to include your source code or a file...is pretty simple: first you save your article for first time to create it, then you edit it and at bottom of the editor, you have a button "Add Attachment"...just click it, upload your file...and that's all...too easy...
contact android10!!!