Zurück zur Übersicht

Tobias Knell

18.09.2013

Building Android projects with maven – part 2: Releases with maven

In my previous post, I showed you the basic setup for android with maven using the android-maven-plugin. Now I’ll show you how to configure it to make releases with maven, and how to configure the plugins to save you some work.

Configuring the keystore data

If you have used the release archetype like in the previous post, most of the work is already done. The necessary plugins are configured and only need some additional data, like the data for your release keystore.

Looking into the poms, you’ll find the property keys, you have to provide:


     <keystore>${sign.keystore}</keystore>
     <alias>${sign.alias}</alias>
     <storepass>${sign.storepass}</storepass>
     <keypass>${sign.keypass}</keypass>

Simply configure them into your maven settings.xml in the release profile (which is also configured in the parent pom):

(If you don’t have a release key yet, check this out http://developer.android.com/tools/publishing/app-signing.html#cert)


   <profile>
       <id>release</id>
       <properties>
           <sign.keystore>/path/to/your/keystore/keystore.keystore</sign.keystore>
           <sign.alias>your-key-alias</sign.alias>
           <sign.storepass>your-keystore-password</sign.storepass>
           <sign.keypass>your-key-password</sign.keypass>
       </properties>
    </profile>

For our case, we removed everything related to proguard from the configs, beacuase we don’t use it for this project. Instead of removing everything, you could also just disable it with the android-maven-plugin configuration

<proguardskip>true</proguardskip>

Adding scm connection and distribution management

Like you would normally, using maven releases, you have to add an scm connection to your parent pom, e.g.:

<scm>
  <developerconnection
    >scm:git:ssh://asd@some.git.server/my-android-project.git</developerconnection
  >
  <tag>HEAD</tag>
</scm>

If you have a server to deploy your maven artifacts to, now is the time to add it to your config (if you only build and deploy them from your local machine, you don’t need this):


    <distributionmanagement>
        <repository>
            <name>releases-repository</name>
            <id>releases.some.address</id>
            <url>https://some.address/content/repositories/releases/</url>
        </repository>
        <snapshotrepository>
            <name>snapshots-repository</name>
            <id>snapshots.some.address</id>
            <url>https://some.address/content/repositories/snapshots/</url>
        </snapshotrepository>
    </distributionmanagement>

Configure the Android Manifest from Maven

To save us some nasty work, we also want to configure the Android manifest with our maven profiles (e.g. setting Debug to false for release, automatically update the version on releases, etc.)

For this, we’ll need to filter the manifest in maven:

add following plugins to the parent pom:


                <plugin>
                    <groupid>org.apache.maven.plugins</groupid>
                    <artifactid>maven-resources-plugin</artifactid>
                    <version>2.6</version>
                    <configuration>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupid>org.codehaus.mojo</groupid>
                    <artifactid>build-helper-maven-plugin</artifactid>
                    <version>1.8</version>
                </plugin>

… and the config for them in the app module (and IT module) pom:


   <build>
       <resources>
            <resource>
                <targetpath>${project.basedir}/target/filtered-manifest</targetpath>
                <filtering>true</filtering>
                <directory>${basedir}</directory>
                <includes>
                    <include>AndroidManifest.xml</include>
                </includes>
            </resource>
        </resources>
   ...
        <plugins>
           ...
           <plugin>
                <groupid>org.codehaus.mojo</groupid>
                <artifactid>build-helper-maven-plugin</artifactid>
                <executions>
                    <execution>
                        <id>parse-version</id>
                        <goals>
                            <goal>parse-version</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactid>maven-resources-plugin</artifactid>
                <executions>
                    <execution>
                        <phase>initialize</phase>
                        <goals>
                            <goal>resources</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
           ...
        </plugins>
    </build>

Don’t forget to update the Android manifest path in the android-maven-plugin to the filtered manifest location!


<androidmanifestfile>${project.build.directory}/filtered-manifest/AndroidManifest.xml</androidmanifestfile>

In the Android Manifest files of the modules, you can now insert the maven property placeholders for the versionCode and versionName


<manifest ....="" android:versioncode="${parsedVersion.majorVersion}${parsedVersion.minorVersion}${parsedVersion.incrementalVersion}" android:versionname="${project.version}" xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>

If you now build your project (clean install), the generated Android manifest should include the versionCode “100” and the version “1.0-Snapshot” (If you didn’t change the version in the pom, yet).

Other than that, you have plenty of configuration possibilities of your AndroidManifest within the android-maven-plugin config, and with this setup you can easily alter the values for the different build profiles.

Building the release

To build the release, use

mvn release:prepare -Prelease

release:perform is not necessary here!

After the release is done, run

mvn release:clean

to clean up.

Note: you might want to let your scm ignore some files (e.g. release.properties and the target folders) so that you don’t have to further clean up your directories after you run the release.

That’s it, now you are able to build your android app releases with maven!

Here are the sources to check against: my-android-project sources

My next blog will be about Android & Continuous integration with Jenkins, so check our blog from time to time 🙂