您的位置:首页 > 移动开发 > Android开发

android ant配置以及批量打包

2016-03-09 20:36 363 查看
最近公司接触到用ant批量打渠道包,学习了下,特意写篇文章share出来~

ant是eclipse中自带的工具,新建应用后,在应用根目录运行命令:

android update project --name <project_name> --target <target_ID>--path <path_to_your_project>


工程根目录下会自动生成local.properties和build.xml两个文件,将local.properties文件改为ant.properties,参数配置都在ant.properties中,build.xml去解析参数,并进行打包。如果觉得上面的命令行麻烦,直接新建两文件放到根目录。

根据项目需求,需要批量更改包名,批量更改渠道号,批量更改百度地图的key,故在ant.properties中配置如下:

#添加ant-contrib-1.0b3.jar用于批量打包用,因为android自带ant不支持批量打包
ant.contrib.path=/Users/luoxiaohui/Develop/software/ant-contrib-1.0b3.jar

# replace androidmainfest.xml
app.base.package=包名前缀(一般在这里根据渠道设置后缀,前缀都一样)
app.versionCode=10
app.versionName=1.8

# build path
out.absolute.dir=/Users/luoxiaohui/Desktop/compile

# apk签名用
# Android singKey path & password
key.store=your.keystore
key.store.password=password
key.alias=your alias
key.alias.password=alias password

# channel  channel:ourkey@appName@baidukey,
market_channels=wandoujia:balabala@豌豆荚@jasldvjlajsdvlasdASDJ,yingyongbao:asdflksd@应用宝@ALDKSGJlasdjf


接下来就看下build.xml如何解析了:

<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default="deploy">

<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" />
#上面五句代码为自动生成,第四句代码如果没自动生成,记得补上

#下面自定义custom_ruls.xml来解析ant.properties中参数,并打包
<import file="custom_rules.xml" optional="true" />

<taskdef resource="net/sf/antcontrib/antcontrib.properties">
<classpath>
<pathelement location="${ant.contrib.path}" />
</classpath>
</taskdef>

<import file="${sdk.dir}/tools/ant/build.xml" />

# 打包入口
<target name="deploy">
<foreach delimiter="," list="${market_channels}" param="channel" target="modify_manifest" />
</target>

<target name="modify_manifest">

<propertyregex property="appuser" input="${channel}" regexp="(.*):" select="\1"/>
<propertyregex property="key1" input="${channel}" regexp=":(.*)@" select="\1"/>
<propertyregex property="key" input="${key1}" regexp="(.*)@" select="\1"/>
<propertyregex property="appname" input="${channel}" regexp="@(.*)@" select="\1"/>
<propertyregex property="baidukey1" input="${channel}" regexp="@(.*)" select="\1"/>
<propertyregex property="baidukey" input="${baidukey1}" regexp="@(.*)" select="\1"/>

<delete file="${app.drawable.path}/../apk/${appuser}.apk" />

<delete dir="bin" />

<echo>
分割字符串...
appuser: ${appuser}
key:     ${key}
appname: ${appname}
baidukey:${baidukey}
</echo>

<echo>
replace AndroidMainfest.xml versionCode VersionName

versionCode:    ${app.versionCode}
versionName:    ${app.versionName}

</echo>

<!-- replace AndroidMainfest.xml versionCode VersionName -->
<replaceregexp byline="false" file="AndroidManifest.xml" match="android:versionCode="(.*)"" replace="android:versionCode="${app.versionCode}"" encoding="${encoding}" />
<replaceregexp byline="false" file="AndroidManifest.xml" match="android:versionName="(.*)"" replace="android:versionName="${app.versionName}"" encoding="${encoding}" />
<!--        <replaceregexp byline="true" file="AndroidManifest.xml" match="android:label="(.*)"" replace="android:label="${appname}""/> -->
<replaceregexp byline="false" file="AndroidManifest.xml" match="package="(.*)"" replace="package="${app.base.package}.${appuser}"" encoding="${encoding}" />
<replaceregexp byline="false" file="AndroidManifest.xml" match="android:name="com.baidu.lbsapi.API_KEY" android:value="(.*)"" replace="android:name="com.baidu.lbsapi.API_KEY" android:value="${baidukey}"" encoding="${encoding}" />

<echo>
replace src import packagename ...
</echo>
<!-- replace src import packagename -->
<replaceregexp byline="false" flags="g" encoding="${encoding}">
<regexp pattern="import(.*)com.*.R;" />
<substitution expression="import ${app.base.package}.${appuser}.R;" />
<fileset dir="src/" includes="**/*.java" />
</replaceregexp>
<antcall target="clean" />
<antcall target="release" />

<copy tofile="./apk/${appuser}.apk">
<fileset dir="${out.absolute.dir}" includes="${ant.project.name}-release.apk" />
</copy>
<echo message="删除临时文件" />
<delete includeEmptyDirs="true">
<fileset dir="${out.absolute.dir}" includes="**/*" />
</delete>
<echo message="===========================" />
</target>
</project>


由于之前没接触过ant脚本,会对它产生恐惧感,但脚本语言学起来却是最简单的,语法也简单。这里我之前遇到的麻烦是:在channel_market中配置了参数appuser,key,appName三个参数,解析也通过正则表示式搞定,但再加上百度地图的key,我用&,*等符号都不行,ant貌似只支持@连接符,所以后面追加了@,然后通过二次正则,把百度地图的key解析出来,如果后面还有参数需要配置的,继续@就好了!

希望看帖子的童鞋多多留言哈~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: