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)
Note: This applies only to Android builds
Open the Ant view (Window -> Show view -> Ant). If the build file isn't in the view then you can simply add it. Once added right click on the ant target you want to run and select Debug as -> Ant build. The Debug perspective should open up and the process should stop at your breakpoint where you can step through it.
Set debug breakpoints (screenshot 1) Right click on editor to show context menu (screenshot 2) and select the "Debug" menu item to launch Ant debugger. Wait until the Ant debugger stops on breakpoint then use step or resume debugger commands, investigate Ant variables and execution stack (screenshot 3)
Ant is a Java-based build tool created as part of the Apache open-source project. You can think of it as a Java version of make. Ant scripts have a structure and are written in XML. Similar to make, Ant targets can depend on other targets.
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" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With