Skip to content
This repository


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Routines to build an Android app in Python and to get rid of Ant.

branch: master

Fetching latest commit…


Cannot retrieve the latest commit at this time

Octocat-spinner-32 .idea
Octocat-spinner-32 src
Octocat-spinner-32 .gitignore
Octocat-spinner-32 README.rst

Python routines to build Android projects

This is a simple Python module to help you build Android projects, from code to apk. It exists because I can't stand Ant.

This is not a standalone build tool, but a collection of routines with which you can build your Android project by writing Python code, or by using a Python-based tool like fabric.

Tested with Android SDK 2.3 (older versions might not work).

Android's build process, that is, what this module implements, is outlined here in detail:


Install the current development version:

$ easy_install

There is also a version in the Python cheeseshop. but it may be old:

$ easy_install py-androidbuild


The simplest case will look something like this:

from import AndroidProject

project = AndroidProject('AndroidManifest.xml', sdk_dir='/opt/android')
apk =
apk.sign('keystore', 'alias', 'name')

The AndroidProject class assumes a default Android directory layout, that is it espects to find things like a ./res and a ./src directory next to the AndroidManifest.xml.


from import AndroidProject

project = AndroidProject('AndroidManifest.xml', sdk_dir='/opt/android')
    for version in ('free', 'pay'):
        # You may want to hard-exclude certain code so it can't just be
        # re-enabled, or whatever you need to do.

        # Recompile the code.

        # For each version, build different configurations.
        # In the end, you'll have 12 apk files.
        for lang in ('de', 'en', 'fr'):
            for density in ('mdpi', 'hdpi',):
      '%s-%s-%s.apk' % (version, lang, density),
                              config='%s,%s' % (lang, density))

If you need to build multiple versions of your app, you need to use different package names:

...'', version_code=5)'', version_code=5)

Note in the previous example how you can also manage your version numbers outside of the AndroidManifest.xml file.

Rather than relying on the default project layout that AndroidProject assumes, you can also use a more low-level API:

platform = get_platform('/opt/android/sdk', target='10')
code = platform.compile('AndroidManifest.xml', 'src', 'res')
res = platform.pack_resources('AndroidManifest.xml', 'res')
apk = p.build_apk('unsigned-unaligned.apk', code, res)

Should it become necessary, you are also free to do things even more low-level than that. What follows is a quick overview of all the APIs used during a build:

platform = get_platform('/opt/android/sdk', target='10')

Here is a build script that I use in production:

Special build steps

Native code

If your project contains native code, simply pass an ndk_dir argument when creating AndroidProject. When that value is set, the class will include the native build steps as well:

from import AndroidProject
project = AndroidProject('AndroidManifest.xml', sdk_dir='/opt/android', ndk_dir="/opt/android-ndk")


Renderscript is supported. We'll do the right thing automatically.

Enabling logging

To see what the build is doing, configure the library logger:


If something goes wrong, an ProgramFailedError is raised which holds all the relevant information:

except ProgramFailedError, e:
    print e.cmdline
    print e.returncode
    print e.stderr
    print e.stdout

Stand-alone script

If you've downloaded the source to an Android application which lacks an Ant build script, and you don't want to go through the whole process of installing it into Eclipse, you can do:

$ py-androidbuild SDK_DIR

This will build the project in the current directory.

Known Issues

Some things still need to be done - mostly because I never used the functionality in question. If you do need them, consider submitting a patch: The Android build process isn't that complicated, and so those things should be easy to implement.

  • Building against extension targets like the Google Maps package hasn't been tested and might well not be possible yet.
  • ProGuard obfuscation is not implememented.
  • Some tests would sure be nice.

Also, referencing "Library projects" doesn't work yet. This is what is necessary to implement it:

  • The user specifies a list of references to library projects (reading the dependencies from the Eclipse/Ant-specific file could be a bonus). This would probably be done on the AndroidProject level.
  • In each library, libs/*.jar files are collected and a) used as a classpath with javac, b) are included in the dexing process.
  • For each library, it's src/ folder is used a) as a source during AIDL compilation, b) as a source during renderscript compilation, c) as a source folder during java compilation, d) added as a sourcefolder in apkbuider.
  • For each library, it's libs/ folder is included in the apkbuilder call as both a "jarfolder" and a "nativefolder".
  • We might have to do something with a libraries res/ folder as well (collected by the Ant tools into "project.libraries.res"). The AaptExecLoopTask seems to --auto-add-overlay and a -S option for each such path.
  • AaptExecLoopTask also generates a file for each library.

Notes on debugging the Android build process

Important files are:

  • platform/sdk: files/ant/main_rules.xml
  • platform/sdk: anttasks/src/com/android/ant/
  • platform/sdk: anttasks/src/com/android/ant/
Something went wrong with that request. Please try again.