一、环境安装
a. 安装JDK最好JDK1.5或更高版本
b. 安装ANT, 最好使用版本高一点的, Ant的contrib的任务需要的Ant1.5或更高版本才能正常工作。
c. 设置java和ant环境变量,ant build.xml会读取系统设置的环境变量.
d. 下载 ant-contrib.jar 支持build.xml文件中循环处理,下载路径:http://ant-contrib.sourceforge.net/,
将下好的包放到ant的lib目录下
二、android项目环境设置
a. 在命令行下,打开到\android-sdk-windows\tools下执行 androidupdate project --name 项目名称 -t 3 -p 项目路径
执行命令后生成build.xml和local.properties文件.
b. 项目路径下创建ant.properties文件 ,内容编辑:
key.store=keystore存放路径
key.store.password=*******
key.alias.password=*******
c. 编辑local.properties
sdk.dir=E:\\android-sdk-windows //sdk路径
apk.path=F://apk //生成的apk存放路径
version=3.0 //项目版本
marketchannels.file.path=F:/marketchannels.txt //渠道文件路径
temp.str.path = F:/newstrings.xml //temp文件路径
marketchannels.txt 存放的是 渠道信息表 如:360手机@www.360.cn#安智网@http://www.anzhi.com/
newstrings.xml 里存放的是 <string name="channelname">%channelname%</string>
<string name="downloadurl">%downloadurl%</string>
将newstrings.xml复制一份到android项目的values,用于读取渠道属性
d. 编辑bulid.xml ,内容编辑:
<?xml version="1.0" encoding="UTF-8"?>
<project
name="项目名称" default="help" >
<loadproperties srcFile="local.properties" />
<!-- 使用第三方的ant包,使ant支持for循环-->
<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${ANT_HOME}/lib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<property name="out.dir" value="bin" />
<property name="out.absolute.dir" location="${out.dir}" />
<!--定义初始变量-->
<property name="channelname" value="" />
<property name="channelkey" value="" />
<!-- 读取存放渠道信息的text -->
<target name="getdata" >
<loadfile property="market_channels" srcFile="${marketchannels.file.path}"encoding="GBK"/>
<echo message="${market_channels}"/>
</target>
<!--循环打包 -->
<target name="deploy" depends="getdata">
<foreach target="modify_manifest" list="${market_channels}" param="nameandchannel" delimiter=" #"/>
</target>
<target name="modify_manifest">
<!-- 获取渠道号码 -->
<propertyregex override="true" property="channelkey" input="${nameandchannel}" regexp="@(.*)" select="\1"/>
<!-- 获取渠道名字 -->
<propertyregex override="true" property="channelname" input="${nameandchannel}" regexp="(.*)@" select="\1"/>
<echo> ${channelname} ---------- ${channelkey} </echo>
<delete file="./res/values/newstrings.xml"/>
<copy file="${temp.str.path}" todir="./res/values/" />
<!-- 正则匹配替换渠道路径 -->
<replaceregexp flags="g" byline="true" encoding="UTF-8">
<regexp pattern="%downloadurl%" />
<substitution expression="${channelkey}"/>
<fileset dir="./res/values/" includes="newstrings.xml" />
</replaceregexp>
<!-- 正则匹配替换渠道名称 -->
<replaceregexp flags="g" byline="true" encoding="UTF-8">
<regexp pattern="%channel%" />
<substitution expression="${channelname}"/>
<fileset dir="./res/values/" includes="newstrings.xml" />
</replaceregexp>
<!-- 编译打包 -->
<antcall target="release" />
<!--将编译签名后的包,复制指定目录并按渠道名重命名 -->
<copy file="${out.absolute.dir}/${ant.project.name}-release.apk" tofile="${apk.path}/${channelname}-${version}.apk" />
</target>
<property file="ant.properties" />
<loadproperties srcFile="project.properties" />
<fail message="sdk.dir is missing. Make sure to generate local.properties using @apos;android update project'" unless="sdk.dir" />
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
E. 项目路径下创建antexec.bat文件 编辑
call ant deploy
D. 双击antexec.bat,执行打包,在apk存放目录可以生成不同渠道的apk.