java - detecting build configuration (debug or release) within ant script



I have an ant script that does what it needs to do, but I need to set a few property values based on whether I'm running release or debug. How do I do this?

If it makes a difference, my ant script runs some custom utility tasks before performing android build.

To answer my own question:

The properties to look for are "build.mode.release" and "build.mode.debug", however there IS a caveat ... if your manifest has debuggable="true", the system REVERTS to debug mode with a slight 'short-coming' (IMO)

  1. build.mode.release is NOT set,
  2. build.mode.debug is ALSO not set
  3. Debug signing is disabled (you have to provide a keystore, alias, and password)

Note: This applies only to Android builds

4 Answers: 

The reason for the "caveat" is actually documented in the Android main_rules.xml project ($ANDROID_SDK_ROOT/tools/ant/main_rules.xml):

<target name="-set-release-mode">
    <!-- release mode is only valid if the manifest does not explicitly
         set debuggable to true. default is false.
         We actually store build.packaging.debug, not build.release -->
    <xpath input="AndroidManifest.xml" expression="/manifest/application/@android:debuggable"
            output="build.packaging.debug" default="false"/>

So what you want to check for is build.mode.debug (executed via ant debug), build.mode.release (when @debuggable=false and executed with ant release), and finally to meet your caveat: build.packaging.debug (when @debuggable=true and executed with ant release)

Here's an example that would work pre-compile automatically:

<target name="-my-debug-precompile" if="build.mode.debug">
  <!-- This is executed for any "debug" build ("ant debug") -->

<target name="-my-release-precompile" unless="build.mode.debug">
  <!-- This is executed for any non-"debug" build (e.g., "ant release",
       regardless of the @debuggable attribute in AndroidManifest.xml) -->

<!-- This is called automatically by Android's ant tasks, and delegates the
     task to one of the above two targets: -->
<target name="-pre-compile" depends="-my-debug-precompile,-my-release-precompile" />

As an update to Joe's answer, it looks like, at least with Android Tools revision 22.3, the property build.mode.debug no longer exists, but you can use to differentiate between debug and release


ant -D<prop-name>=<value> will set property in ant


final soultion for ant debug and jni module:
1. in custom_rules.xml, assign debug mode to 'BUILDMODE'

<?xml version="1.0" encoding="UTF-8"?>
        <property name="out.library.jar.file" location="bin/classes.jar" />
    <target name="-pre-compile">
                <exec executable="ndk-build">
                        <arg value="BUILDMODE=${}" />
  1. jni/, add following:

ifeq ($(BUILDMODE), true)