我是一个 ANT 菜鸟,所以很难在网上找到可以为我的 android 项目解决这个小谜语的地方。
我想做的是根据我运行的 ant 命令运行一些规则。基本上,当我运行 ANT DEBUG 时,我不希望运行任何自定义规则。当我运行 ANT RELEASE 时,我希望我的自定义规则运行。
目前,无论在预构建阶段如何,我的自定义规则都会运行。
这是我的 build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="MainActivity" default="help">
<property file="local.properties" />
<property file="ant.properties" />
<property environment="env" />
<condition property="sdk.dir" value="${env.ANDROID_HOME}">
<isset property="env.ANDROID_HOME" />
</condition>
<loadproperties srcFile="project.properties" />
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through the ANDROID_HOME environment variable."
unless="sdk.dir"
/>
<import file="custom_rules.xml" optional="true" />
<import file="${sdk.dir}/tools/ant/build.xml" />
这是我的 custom_rules.xml
<project>
<macrodef name="git" description="run a git command">
<attribute name="command" />
<attribute name="dir" default="" />
<element name="args" optional="true" />
<element name="gitOutputRedirector" optional="true"/>
<sequential>
<echo message="git @{command}" />
<exec executable="git" dir="@{dir}">
<arg value="@{command}" />
<args/>
<gitOutputRedirector/>
</exec>
</sequential>
</macrodef>
<target name="-pre-build" depends="set-version-using-file,git-last-commit-hash-rev-parse" >
</target>
<target name="set-version-using-file">
<!-- Load properties from "version.properties" file -->
<property file="version.properties" />
<replaceregexp file="AndroidManifest.xml" match="android:versionCode(.*)"
replace='android:versionCode="${Version.Code}"'/>
<replaceregexp file="AndroidManifest.xml" match='android:versionName="\d+\.+\d+\.+\d+\.+\d"'
replace='android:versionName="${Version.Name}"'/>
<echo message="Set android:versionCode as ${Version.Code}" />
<echo message="Set android:versionName as ${Version.Name}" />
</target>
<!-- Get the last commit -->
<target name="git-last-commit-hash-rev-parse" description="Commits all changes to version git" >
<property file="version.properties" />
<git command="rev-parse" >
<args>
<arg value="HEAD" />
</args>
<gitOutputRedirector>
<redirector outputproperty="git.last.commit"/>
</gitOutputRedirector>
</git>
<echo message="Last commit found was ${git.last.commit}" />
<echo message="Will now tag ${git.last.commit} with ${Version.Name}" />
<git command="tag">
<args>
<!-- This tags the last commit with the full version name -->
<arg value="${Version.Name}" />
<!-- For some reason why it doesn't like this command through ant.-->
<!-- <arg value="${Version.Name} ${git.last.commit} -m 'Tagged for build'" /> -->
</args>
</git>
</target>
</project>