Ant批量打包工具的使用

时间:2022-01-25 23:41:48


最近大概用了两天的时间来研究下之前一直好奇的自动批量打包功能

就是利用ant脚本来更改AndroidManifest中的meta渠道值,打包不同的市场渠道号对应下的apk。

之前一直使用的是Eclipse自带的打包方法:

Project 右键--> Android Tools --> Export Signing Application Package或者Project 右键-->Export --> Android --> Export Android Application

都能打开Export Android Application对话框,之后就是选择或者创建keystore,输入密码,选择保存位置,完成。

这种传统做法打一个包还好,如果要统计对比分析不同市场的下载量,那么就要在Androidmanifest中为不同的市场分配不同的的渠道号,然后发布到不同的市场(这里笔者采用的是友盟统计工具,然后可以在友盟管理后台中查看不同市场的统计分析报表),如果市场比较多的话,一个一个打包既费时又费力,所以批量打包就显得尤为重要



Ant批量打包具体步骤:


1,到   官网   上下载Ant绿色版zip archive: apache-ant-1.9.4-bin.zip [PGP] [SHA1] [SHA512] [MD5]约8M,配置系统环境变量,ANT_HOME、Path,如果在命令行输入ant home出现build failed就表现配置成功了

2,为了实现批量打包,需要使用扩展包--->ant-contrib-1.0b3.jar,到   官网   上下载,然后把jar包放到上一步apache-ant-1.9.4\lib目录下

3,进入命令行,输入 android update project -p 工程project的绝对路径,之后会在project下生成两个文件,build.xml以及local.properties

4,手动创建两个文件,ant.properties 用来配置keystore的相关信息和 custom_rules.xml用来配置批量打包的有关信息

以下是相关示例代码:

local.properties

sdk.dir=E:\\Android\\Eclipse\\android-sdk-windows
apk.dir=E:\\Android\\Ant
market_channels =Gfan,Mumayi,Baidu

ant.properties,建议把keystore文件放在工程目录下

key.store=xxx.keysotre
key.store.password=000000
key.alias=xxxx
key.alias.password=000000
custom_rules.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" >

<taskdef resource="net/sf/antcontrib/antcontrib.properties" >
<classpath>
<pathelement location="E:/Android/Eclipse/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>

<target name="deploy" >
<foreach
delimiter=","
list="${market_channels}"
param="channel"
target="modify_manifest" >
</foreach>
</target>

<target name="modify_manifest" >
<replaceregexp flags="g" byline="false">
<regexp pattern="android:value="(.*)" android:name="UMENG_CHANNEL"" />
<substitution expression="android:value="${channel}" android:name="UMENG_CHANNEL"" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<property
name="out.final.file"
location="${apk.dir}/Microinvitation_${channel}.apk" />
<antcall target="clean" />
<antcall target="release" />
</target>
</project>

build.xml,是用命令行生成的

<?xml version="1.0" encoding="UTF-8"?>
<project
name="MainActivity"
default="help" >

<!--
The local.properties file is created and updated by the 'android' tool.
It contains the path to the SDK. It should *NOT* be checked into
Version Control Systems.
-->

<property file="local.properties" />

<!--
The ant.properties file can be created by you. It is only edited by the
'android' tool to add properties to it.
This is the place to change some Ant specific build properties.
Here are some properties you may want to change/update:

source.dir
The name of the source directory. Default is 'src'.
out.dir
The name of the output directory. Default is 'bin'.

For other overridable properties, look at the beginning of the rules
files in the SDK, at tools/ant/build.xml

Properties related to the SDK location or the project target should
be updated using the 'android' tool with the 'update' action.

This file is an integral part of the build system for your
application and should be checked into Version Control Systems.

-->

<property file="ant.properties" />

<!--
if sdk.dir was not set from one of the property file, then
get it from the ANDROID_HOME env var.
This must be done before we load project.properties since
the proguard config can use sdk.dir
-->

<property environment="env" />


<condition
property="sdk.dir"
value="${env.ANDROID_HOME}" >

<isset property="env.ANDROID_HOME" />
</condition>

<!--
The project.properties file is created and updated by the 'android'
tool, as well as ADT.

This contains project specific properties such as project target, and library
dependencies. Lower level build properties are stored in ant.properties
(or in .classpath for Eclipse projects).

This file is an integral part of the build system for your
application and should be checked into Version Control Systems.
-->

<loadproperties srcFile="project.properties" />

<!-- quick check on sdk.dir -->

<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 per project custom build rules if present at the root of the project.
This is the place to put custom intermediary targets such as:
-pre-build
-pre-compile
-post-compile (This is typically used for code obfuscation.
Compiled code location: ${out.classes.absolute.dir}
If this is not done in place, override ${out.dex.input.absolute.dir})
-post-package
-post-build
-pre-clean

-->

<import
file="custom_rules.xml"
optional="true" />

<!--
Import the actual build file.

To customize existing targets, there are two options:
- Customize only one target:
- copy/paste the target into this file, *before* the
<import> task.
- customize it to your needs.
- Customize the whole content of build.xml
- copy/paste the content of the rules files (minus the top node)
into this file, replacing the <import> task.
- customize to your needs.

***********************
****** IMPORTANT ******
***********************
In all cases you must update the value of version-tag below to read 'custom' instead of an integer,
in order to avoid having your file be overridden by tools such as "android update project"

-->
<!-- version-tag: 1 -->

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



</project>

windows下命令行到工程所在目录,输入ant即可