Skip to Content
Author's profile photo Vedran Lerenc

How to build an Android project with Maven and run it on Jenkins (formerly Hudson)

Being a CI guy, I wanted to build my Android project not only from within my Eclipse, but also with Maven on my Jenkins (formerly known as Hudson).

So, first thing was to look for someone out there that had already wrote a plugin for building Android projects with Maven, and yep, there was a plugin and even a large section on it in the Maven bible The plugin worked like a charm. Your have to include it into your build section of your pom.xml like this:$/android-sdk</path></pre><pre>          <platform>${sdk.version}</platform></pre><pre>        </sdk></pre><pre>        <deleteConflictingFiles>true</deleteConflictingFiles></pre><pre>        <undeployBeforeDeploy>true</undeployBeforeDeploy></pre><pre>      </configuration></pre><pre>      <extensions>true</extensions></pre><pre>    </plugin></pre><pre>  </plugins></pre><pre></build></pre><p>Now you notice up there two configuration settings. One for the SDK path and one for the SDK version. The plugin expects you to have an installed Android SDK and to have a property named ANDROID_HOME pointing to the installation path or to configure it directly within the plugin configuration. I am no friend of this as this “prerequisite” breaks a very a beautiful feature of Maven: it is meant to materialize everything you need for your build and having to install the SDK beforehand is not nice. I guess the plugin creators couldn’t package the SDK into a Maven artifact for legal reasons, but since this is just my private setup, I zipped the SDK (only the crucial parts like executables and platform(-tools), but no documentation, samples or sources) and uploaded it into my private Nexus. The only thing I needed now is to do was to extract the SDK before the plugin is called:</p><pre><profiles></pre><pre>  <profile></pre><pre>    <id>install-android-sdk</id></pre><pre>    <activation></pre><pre>      <!– Due to a nasty bug in Maven I can’t activate this profile</pre><pre>           if both conditions are met (in contrary to The Defintive Guide):</pre><pre> </pre><pre>      <os> <family>windows</family> </os> –></pre><pre>      <file></pre><pre>        <missing>$/android-sdk/platform-tools/adb.exe$/android-sdk*/</includes></pre><pre>                  </artifactItem></pre><pre>                </artifactItems></pre><pre>              </configuration></pre><pre>            </execution></pre><pre>          </executions></pre><pre>        </plugin></pre><pre>      </plugins></pre><pre>    </build></pre><pre>  </profile></pre><pre></profiles> </pre><p>That’ll do the trick. When the SDK is not present, it is extracted through the Maven dependency plugin which is just great for these purposes. As you can see I had some difficulties to do that if both conditions (platform and missing file) are met – hopefully Sonatype will fix this issue one day.</p><p>Now the version is still missing. I set it as property like this:</p><pre><properties></pre><pre>  <sdk.version>8</sdk.version></pre><pre></properties> </pre><p>You could also use Maven property filtering to change the version in the AndroidManifest.xml if you like. The last thing we need are the dependencies themselves:</p><pre><dependencies></pre><pre>  <dependency></pre><pre>    <groupId>android-sdk</groupId></pre><pre>    <artifactId>android-sdk</artifactId></pre><pre>    <version>${sdk.version}</version></pre><pre>    <type>zip</type></pre><pre>    <classifier>windows</classifier></pre><pre>    <scope>provided</scope></pre><pre>  </dependency></pre><pre>  <dependency></pre><pre>    <groupId></groupId></pre><pre>    <artifactId>android</artifactId></pre><pre>    <version>2.2.1</version></pre><pre>    <scope>provided</scope></pre><pre>    </dependency></pre><pre>  <dependency></pre><pre></dependencies></pre><p>You need the SDK and the platform JARs. The first is what you have to do if you want your Maven to truly materialize everything it needs for building your Android project (you can also live without it and the profile above, if you install the SDK manually). The latter is the platform against you will build and that’s already found on Maven Central (the pre-configured Sonatype repository).</p><p>That’s it. Now Maven builds your Android project. If you need other libraries, add them as you would normally add them to your Maven project using the dependencies section in your pom.xml.</p><p>Now on to the Jenkins (formerly known as Hudson): Well, nothing special at all, at least for this basic project. Just run your Maven build on Jenkins as you would have run any other Maven build: Create yourself a new project using the Maven project template, configure your build triggers and enter your Maven goals, e.g. clean deploy. That’s it.</p><p>I haven’t explored the testing yet, especially not the testing-on-device feature, but I am sure I will soon as this is part of the CI cycle. However, what I saw just now was very promising and I haven’t expected such a smooth run on it.</p>

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.