(Still) build Android apps with ant and Eclipse

Android ant support

Ant support in Android is long outdated. However, with a few tweaks it's still possible to build Android apps using ant (even with limited Eclipse integration).

There are legitimate reasons to still build Android apps using ant. First of all, you may be responsible for supporting a legacy project with very limited new development. Migrating such a project to AndroidStudio and gradle may require more effort than you can spare (especially as you’d also have to migrate the project structure).

However, there are a few quirks that you have to keep in mind. The following setup works for us:

Platform setup

Tools 25.2.5 seem to be the last version with ant support. We’re using build-tools 25.0.3, but more modern versions also should work. For installing the current Android target 31 you’ll have to use the sdkmanager from tools dir, otherwise you’ll get a target not found error.

sdkmanager --install "platform-tools"
sdkmanager --install "tools;25.2.5"       
sdkmanager --install "build-tools;25.0.3"
ANDROID_TARGET=$(grep "^target=" project.properties |cut -d "=" -f 2)
${ANDROID_HOME}/tools/bin/sdkmanager "platforms;${ANDROID_TARGET}"

To enforce the use of build-tools 25.0.3, you can put the following line in your project.properties file:

sdk.buildtools=25.0.3

Use patched build.xml

The build.xml file from the ${ANDROID_HOME}/tools/ant dir does not work anymore for modern targets. Especially, the code signing process has to be adapted. You can use our patched build-android-sdk.xml. This file has to be referenced from your build.xml file (near the end of the file) like this:

<import file="/path/to/build-android-sdk.xml" />

Call ant

ant clean debug lint     # Debug build, lint
ant clean release        # Signed release build

(Limited) Eclipse support

Support for Eclipse using Andmore from Eclipse Marketplace seems to be abandoned. Currently we can’t recommend this approach.

However, some of our developers use a configuration which allows at least editing Android apps within Eclipse. For this, you have to patch org.eclipse.andmore.base_0.5.1.201701251432.jar with some classes from an ancient Oracle Java 8 release.

jar xvf rt.jar sun/misc/BASE64Decoder.class sun/misc/BASE64Encoder.class sun/misc/CharacterDecoder.class sun/misc/CharacterEncoder.class
jar uvf <path/to/org.eclipse.andmore.base_0.5.1.201701251432.jar> sun/misc

Where org.eclipse.andmore.base_0.5.1.201701251432.jar is found, depends on your installation – usually either in Eclipse/plugins or in your home dir in .p2/pool/plugins.

You’ll still get an ugly error when compiling files (“Errors occurred during the build.”). To avoid this, you can check

Skip packaging and dexing until export or launch

in Eclipse Android preferences. Probably you can even restore packaging and dexing by including crypto classes from sun.misc, but this is beyond the scope of this snippet.