Moving Android build from Ant to Maven

Submitted by olaf on 2017-07-01

Recently, my five year old Android smartphone broke and so I had to “upgrade” it to a newer one. Unfortunately, the new phone (Android 6) was missing a menu button.

This forced me to upgrade my development environment for my home grown apps too. As a consequence, Ant isn’t supported anymore, but I also don’t want to use Android Studio as recommended on Android’s web site. Using Gradle alone was no option either, because it was an order of magnitude slower than Ant at least, if not more.

Which took me to Maven as the lesser evil.

Using Maven requires the Android Maven Plugin. This plugin takes care of the Android specific parts and simplifies the whole process.


All things considered, going from Ant to Maven is pretty straightforward. It consists of removing the old files

rm ant.properties build.xml local.properties proguard-project.txt project.properties

moving a few files around

mkdir -p src/main/java/
mv AndroidManifest.xml src/main/
mv src/de/ src/main/java/
mv res/ src/main/

and finally creating an appropriate build file, called pom.xml. There’s a sample at the plugin’s web site Usage, which didn’t quite fit my environment:

<project>
  <groupId>com.simpligility.android</groupId>
  <artifactId>helloflashlight</artifactId>
  <version>1.0.0</version>
  <packaging>apk</packaging>
  <build>
    <plugins>
      <plugin>
        <groupId>com.simpligility.maven.plugins</groupId>
        <artifactId>android-maven-plugin</artifactId>
        <version>4.3.0</version> <!-- use latest release -->
        <extensions>true</extensions>
      </plugin>
    </plugins>
  </build>
</project>

First, Maven complained about a missing modelVersion. Next, it was missing ANDROID_HOME, and finally it was missing the default Android SDK.

Searching around the internet, adding pieces, gave the following pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>de.olafdietsche.android.samples</groupId>
    <artifactId>hello</artifactId>
    <version>0.1.0</version>
    <packaging>apk</packaging>

    <properties>
        <!-- use UTF-8 for everything -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <sdk.dir>/usr/local/android-sdk-linux</sdk.dir>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>4.1.1.4</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>com.simpligility.maven.plugins</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>4.3.0</version> <!-- use latest release -->
                <configuration>
                    <sdk>
                        <path>${sdk.dir}</path>
                        <platform>23</platform>
                    </sdk>
                </configuration>
                <extensions>true</extensions>
            </plugin>
                
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

If you want to build a simple Jar file or a library containing other Android resources, used by other modules, packaging would be either

<packaging>jar</packaging>

or

<packaging>aar</packaging>

respectively.


As a last step, I added Google’s appcompat support library, which needs an additional dependency

<dependency>
    <groupId>com.android.support</groupId>
    <artifactId>appcompat-v7</artifactId>
    <version>23.4.0</version>
    <type>aar</type>
</dependency>

and an entry for the SDK repository

<repositories>
    <repository>
        <id>android-support</id>
        <url>file://${sdk.dir}/extras/android/m2repository</url>
    </repository>
</repositories>

Post a comment

All comments are held for moderation; Markdown and basic HTML formatting accepted. If you want to stay anonymous, leave name, e-mail and website empty.