Android with Maven

Ever since making the switch from Ant to Maven 2 back in 2005, I’ve never looked back. So one of the first things I wanted to know was if I could use maven to build my Android apps. And of course, you can.

Here’s an example pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.androidlearning</groupId>
    <artifactId>androidlearning</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>androidlearning</name>
    <description>Learning Android!</description>
 
    <dependencies>
    ...
    </dependencies>
 
    <build>
        <finalName>${project.artifactId}</finalName>
        <sourceDirectory>src</sourceDirectory>
        <defaultGoal>install</defaultGoal>
 
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <version>3.7.0</version>
                    <extensions>true</extensions>
                </plugin>
            </plugins>
        </pluginManagement>
        <plugins>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <configuration>
                    <sdk>
                        <path>/opt/adt-bundle-linux-x86_64-20130917/sdk</path>
                        <!-- platform or api level (api level 4 = platform 1.6) -->
                        <platform>18</platform>
                    </sdk>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

There are certainly plenty of maven detractors, but the reasons why I prefer maven to ant (or any other tool that’s available at the moment):

  • XML is a reasonably good format for configuration. It is a terrible format for programming. With ant, you are essentially programming a build with XML. Maven configures builds with XML.
  • Programming a build leads to developers doing…whatever they want. But builds should not be particularly complex things, and with a few conventions can be very standardized. This is what maven does.
  • A maven expert is immediately a build expert for any product that builds with maven. Not so much with Ant (or any other build tool that gives the build developer freedom to program the build however they want).
  • Maven’s dependency management system is the right way to handle dependencies. There’s no checking jar files into source control, just some configuration specifying what your dependencies are.

Caveats:

  1. Android appears to moving towards using Gradle as it’s official build system, although it may not be ready for primetime yet. Gradle certainly has a lot of momentum in the industry.
  2. Maven creates .apklib files for android dependencies, which allows android dependencies to have xml resource files in them. This is great, except that the apklib concept is specific to maven. IntelliJ (and presumably the extremely beta Android Studio) will support these types of dependencies, but Eclipse knows nothing of them.