检测ant脚本中的构建配置(调试或发布)

cop*_*lii 8 java ant configuration android build

我有一个ant脚本可以执行它需要做的事情,但我需要根据我是在运行发布还是调试来设置一些属性值.我该怎么做呢?

如果它有所不同,我的ant脚本在执行android构建之前运行一些自定义实用程序任务.


回答我自己的问题:

属性查找是" build.mode.release "和" build.mode.debug ",但有一个警告 ......如果你的清单有可调试="真",则系统将恢复到调试模式有轻微的"短缺'(IMO)

  1. build.mode.release设置,
  2. build.mode.debug也 未设置
  3. 禁用调试签名(您必须提供密钥库,别名和密码)

注意:这仅适用于Android版本

Joe*_*Joe 8

"警告"的原因实际上记录在Android main_rules.xml项目($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>
Run Code Online (Sandbox Code Playgroud)

所以你要检查的是build.mode.debug(通过执行ant debug),build.mode.release(当@debuggable=false执行时ant release),并最终满足你的警告:( build.packaging.debug何时@debuggable=true执行ant release)


这是一个自动预编译的示例:

<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" />
Run Code Online (Sandbox Code Playgroud)

  • @copolii:我详细阐述的原因是因为你没有提到第三个属性(`build.packaging.debug`) - 当应用程序被假定为处于发布模式(`ant release`)时这是真的,但是由于`@ debuggable = true`而被迫返回调试版本 - 并且没有明确地调出*为什么*发生了你的"警告".我也意识到你已经有了一个适合你的解决方案,并且你用语言描述了这个解决方案,但由于这是SO而其他人稍后会偶然发现这个问题,所以有一个拼写出来的例子对其他人有用; 因此,示例目标. (3认同)

Op *_*kel 1

ant -D<prop-name>=<value>将在 ant 中设置属性