Maven Android Plugin with zipalign and improved verification

With the latest release of the Maven Android Plugin a bunch of changes I did went out to the rest of the users of the plugin. As of version 2.5.0 the plugin has improved verification of the sdk platform and api level, a new zipalign goal and maybe the emulator start works on windows too. And the samples have been expanded and changed to use the Android jar artifacts from Maven central. But lets look at the changes of the plugin in detail today and maybe the samples another day…

Work on the improved verification of the platform and api level started when Google changed the folder names for the different platforms in the SDK from using the platform number (e.g. 1.5, 2.1 and so on) to use the API level number (3, 7 …). Due to this change and the fact that the plugin picks up the platform number from the sdk platform configuration in the pom, depending on your configuration and age of SDK install your system either kept working or broke. This ticked me off enough to decide that this should be improved and both should work. And that is the case now. In addition the plugin actually checks the installed platform versions of your Android SDK install and will only proceed if the platform or api level specified in the pom file is actually available on the machine. With the new code you can configure the plugin like that

   
<plugin>
  <groupId>com.jayway.maven.plugins.android.generation2</groupId>
  <artifactId>maven-android-plugin</artifactId>
  <version>2.5.0</version>
  <configuration>
    <sdk>
      <platform>3</platform>
    </sdk> ...

using the API level. Or if you prefer you can use the platform version with the same effect and that would then look like this:

<plugin>
  <groupId>com.jayway.maven.plugins.android.generation2</groupId>
  <artifactId>maven-android-plugin</artifactId>
  <version>2.5.0</version>
  <configuration>
    <sdk>
      <platform>1.5</platform>
    </sdk> ...

Both configuration will work the same and will only work if your Android SDK folder contains a platforms/android-3 folder.

The next enhancement to the plugin is the new zipalign goal. Any Android package that is uploaded to the Android Market needs to be signed, which can be done with the jarsigner plugin, and zipaligned. Up to now this was only possible by doing it manually or by using the exec maven plugin. With the new goal for the plugin you could for example just add the ziaplign goal execution to your plugin definition like this

...
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<executions>
  <execution>
    <id>alignApk</id>
    <phase>install</phase>
    <goals>
      <goal>zipalign</goal>
    </goals>
  </execution>
...

This would take the standard apk created by the plugin, run the zipalign command with the required parameters on it and save the result as *-aligned.apk file. To zipalign other input apk files or create a different outputfile name you can pass in parameters in the plugin configuration like that

<configuration>
..
<zipalign>
  <verbose>true</verbose>
  <skip>false</skip>
  <inputApk>${project.build.directory}/${project.artifactId}.apk</inputApk>
  <outputApk>${project.build.directory}/${project.artifactId}-aligned.apk</outputApk>
</zipalign>
...

This configuration actually shows the default values so that would not change anything. But you could pass in the apk file name after the jarsign process in as inputApk parameter and you would be ready for release on the market. Similar to the surefire plugin you can skip the zipalign goal.

The third change on the plugin is hopefully working now too. I say that because since I do not have access to a Windows machine I can not test the emulator start/stop feature. I have relied on feedback on the issue to get it to hopefully work fully this time. If not make sure you let me know and comment on the issue.

So those are the new changes in the 2.5 release of the Maven Android Plugin. I hope you enjoy them. If you want to see them changes fully configured and usable you can have a look at the samples of the plugin. I did a bunch of changes there too, but like I said that will be a blog post another night.