我是一个蚂蚁,所以很难在网上找到我可以为我的android项目解决这个小谜题的地方。
我想做的是根据我运行的蚂蚁命令运行一些规则。基本上,当我运行ANT调试时,我不希望运行任何自定义规则。当我运行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>
好的,我解决了!
我把我的宏def和两个目标移到了主build.xml 中
在我的主构建.xml中,我添加了以下
<target name="releaseCan" depends="set-version-using-file, release, git-last-commit-hash-rev-parse"/>
将版本目标更改为以下内容,以便运行预构建
<target name="set-version-using-file" depends="-pre-build">
将git目标更改为以下内容,因此它在构建后运行。
<target name="git-last-commit-hash-rev-parse" description="Commits all changes to version git" depends="-post-build">
所以我运行ant release它可以执行以下吗
修改AndroidManifest.xml(预构建)运行正常的发布目标(在那里进行编译和签名等)运行git命令(post-build)
因此,这是相当基本的,我不确定如果发布目标失败,后期构建是否还会运行。