8

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

copolii
  • 14,208
  • 10
  • 51
  • 80

4 Answers4

9

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"/>
    ...
</target>

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>

<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) -->
</target>

<!-- 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" />
Joe
  • 42,036
  • 13
  • 45
  • 61
  • main_rules is where I found the info that I posted. Thanks :) – copolii Jun 04 '11 at 09:14
  • 3
    @copolii: the reason I elaborated is because you did not mention the 3rd property (`build.packaging.debug`) -- which is true when the app is *supposed* to be in release mode (`ant release`), but is being forced back to a debug build due to `@debuggable=true` -- and also did not explicitly call out *why* your "caveat" occurred. I also realize you already had a solution that worked for you, and which you described in words, but since this is SO and others will stumble upon this question later, having a spelled-out example is useful for others; hence the example targets. – Joe Jun 13 '11 at 23:44
3

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 build.is.packaging.debug to differentiate between debug and release

KevinZusy
  • 31
  • 1
  • 1
  • Took me hours of trying before stumbling on this. This can't be overstressed. Indeed, it is now `build.is.packaging.debug` – Warpspace Dec 05 '14 at 10:23
1

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

Op De Cirkel
  • 28,647
  • 6
  • 40
  • 53
  • I know that, but I don't see the conditional part of that. I want to either load one of the two files (release.properties/debug.properties) or manually set the property values from within the script based on the configuration. take a directory path for example, I want out/release and out/debug – copolii Jun 02 '11 at 18:20
  • @copolii use task: http://ant.apache.org/manual/Tasks/conditions.html – Op De Cirkel Jun 02 '11 at 18:26
  • I'm aware of the condition task, I need the actual condition ... something that evaluates to true if the ant script is being executed with "ant release" and false with "ant debug" or vice versa. There must be a property somewhere ... i.e. in VisualStudio/MSBuild it's $(configuration), which evaluates to 'release' or 'debug' – copolii Jun 02 '11 at 18:39
0

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"?>
<project>
        <property name="out.library.jar.file" location="bin/classes.jar" />
    <target name="-pre-compile">
                <exec executable="ndk-build">
                        <arg value="BUILDMODE=${build.is.packaging.debug}" />
                </exec>
    </target>
</project>
  1. jni/Android.mk, add following:

ifeq ($(BUILDMODE), true)
LOCAL_CFLAGS=-DDEBUG
endif

wuruxu
  • 36
  • 2