Ant打包APK时条件控制分支小记

Posted by KC on December 23, 2013

由于渠道包众多,只能用ant批量打包。由于广告内置的问题(有的渠道不允许有内置广告),出现manifest文件在带广告和不带广告的版本之间有大量修改,所以考虑有一个变量isSDKEnabled控制是否生成带广告的APK安装文件。于是需要用到条件判断,做个小记。

project.properties文件增加以下配置,用来控制打包是否包含广告SDK:

project.properties:

1
isSDKEnabled=true

方法一:

使用Ant自带条件判断的方式;

build.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<project name="testCondition" default="test">
	<target name="test">
		<property file="project.properties" />
		<echo message="isSDKEnabled=${isSDKEnabled}"/>
		<condition property="cond">
			<istrue value="cond"/>
		</condition>
		<antcall target="isTrue" />
		<antcall target="isFalse" />
	</target>
	<target name="isTrue" if="${isSDKEnabled}">
		<echo>It is ture...</echo>
	</target>
	<target name="isFalse" unless="${isSDKEnabled}">
		<echo>It is false...</echo>
	</target>
</project>

这个方式也可以在condition里面使用<and>、<or>、<not>、<xor>等多条件组合判断。

方法二:

使用Ant-contrib包的条件判断:

build.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<project name="testCondition" default="condition"> 
	<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>
	
	<target name="isTrue">  
        <echo>is ture</echo>  
    </target>  
    <target name="isFalse">  
        <echo>is false</echo>  
    </target>  
	<target name="condition">
	<loadproperties srcFile="project.properties" />
		<if>
			<equals arg1="true" arg2="${isSDKEnabled}" />
			<then>
				<antcall target="isTrue"/>
			</then>
		
			<elseif>
				<equals arg1="false" arg2="${isSDKEnabled}" />
				<then>
					<antcall target="isFalse"/>
				</then>
			</elseif>
			<else>
				<echo>Error occur...</echo>
			</else>
		</if>
	</target>
</project>  

方法二看起来会更加容易让人理解。

参考:

  1. Ant Manual - Conditions
  2. Ant-contrib Tasks: If