如果你每次打包都需要不断地手动打包,通过不断地复制粘贴,当你打几百个包的情况下,我相信你一定会疯掉的,当然,这种事情在我们程序员看来,都是可以解决的事情,因为,我们的工作就是让本来繁琐的工作变得简单一点。今天我们就需要简单地介绍一下ant批量打包的问题吧。
网上看到的资料各有千秋吧,现在是时候做一个总结,站在各位大神的肩膀上我们才能够看得更加远。
首先你应该去官网上面下载apache-ant的最新版,我这边使用了一个apache-ant-1.9.4,这是当前的最新版的,现在附上下载链接——可以去我的资源上面去下载,下载链接为:http://download.youkuaiyun.com/detail/litianpeng1991/8396457
下载好了之后,我们开始配置环境 变量,下面是配置过程:
这是我的ant的路径:
配置环境变量:
配置好了之后我们可以去cmd下面验证一下:
如果出现上面的情况,说明ant已经配置成功了,接下来我们为了可以实现批量(循环)打包,必须要实用一个jar包——ant-contrib-1.0b3.jar
大家可以去我的资源页上面下载,附上下载链接——http://download.youkuaiyun.com/detail/litianpeng1991/8396465
下载好了之后大家一定要将后缀名从zip改为jar,然后将这个jar包拷贝到apche-ant-1.9.4/lib/文件夹下面
接下来就轮到我们project进场了。
1、首先我们进入project的目录下,输入命令,命令格式如下:
android update project --name <project_name> --target <target_ID>--path <path_to_your_project>
http://developer.android.com/tools/projects/projects-cmdline.html 具体如果有不明白的地方可以参考google官方给出的api文档
2、我们将第三方jar包可以直接放在libs文件夹下面,android在编译打包的时候可以自动将这些jar加进去的,但是我们也要考虑到一种情况的,就是android project是支持引用第三方项目library的,就是第三方library是以project的形式存在的,这时候我们应该怎么办呢?
下面介绍解决的办法:(下面的命令都要在一直的 项目目录下,千万别进错了目录)
android project --name 项目名 -p ./ --subprojects
这时候肯定会出现一个问题的:
Error:The project either has no target set or the target is invalid,
Please provide a --target to the "android.bat update" command.
这是因为我们的第三方项目library其实也是需要进行ant设置的,
这时候我们进行第三方项目的目录下:运行如下命令
android update 第三方项目名 -p ./
运行完这个之后,我们第三方项目目录下应该多了两个文件build.xml和local.properties两个文件
然后我们再进行原来的项目目录下运行原来的命令,但是如果还会出现上面红色标记的问题,此时也可以运行 下面的命令:
android update project -p . -t android-15 -s(其中这个android-15是可以随便写的,写你自己有的target)
这时候我们会发现我们的项目名下面也会有两个新生成的文件,分别是build.xml和local.properties
build.xml内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project
name="<span style="background-color: rgb(255, 0, 0);">项目名</span>"
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" />
<!-- version-tag: 1 -->
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
local.properties文件内容如下:
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.
# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.
sdk.dir=D:\\android_developer\\androidSDK\\sdk
这时候我们开始编写custom_rules.xml——这是我们新建的一个xml文件,下面的红色部分,要将我们上面下载的jar包放到libs中去
<?xml version="1.0" encoding="UTF-8"?>
<!-- 渠道包打包脚本 ant deploy -->
<project name="custom_rules" >
<!-- 引用支持循环的jar包ant-contrib-1.0b3.jar -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties" >
<classpath>
<span style="color:#ff0000;"><pathelement location="libs/ant-contrib-1.0b3.jar" /></span>
</classpath>
</taskdef>
<!-- 循环打包,target名为deploy,构建的时候必须选择deploy -->
<target name="deploy" >
<!-- 进来的时候先clean一下项目 -->
<antcall target="clean" />
<!-- for循环内容传进来的参数为market_channels,以","为分隔构建list,循环读取list,并将读取到的字符串以参数channel传进modify_manifest中 -->
<foreach
delimiter=","
list="${market_channels}"
param="channel"
target="modify_manifest" >
</foreach>
</target>
<target name="modify_manifest" >
<!-- 按正则匹配获取渠道名字,赋给channelname -->
<propertyregex
input="${channel}"
override="true"
property="channelname"
regexp="(.*):"
select="\1" />
<!-- 按正则匹配获取渠道号码,赋给channelkey -->
<propertyregex
input="${channel}"
override="true"
property="channelkey"
regexp=":(.*)"
select="\1" />
<!-- 将AndroidManifest.xml中的android:name="channelname"替换成为android:name="${channelname}",这里的${channelname}是我们上面正则匹配出来的内容 -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="channelname""
replace="android:name="${channelname}"" />
<!-- 将AndroidManifest.xml中的android:value="channelkey"替换成为android:value="${channelkey}" -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="channelkey""
replace="android:value="${channelkey}"" />
<!-- 用release打包 -->
<antcall target="release" />
<!-- 拷贝渠道包到指定目录下,out.absolute.dir在自带的build.xml中定义了,为bin目录,如果要修改,可以在local.properties中添加一行out.absolute.dir=指定目录 -->
<copy
file="${out.absolute.dir}/${ant.project.name}-release.apk"
tofile="${out.absolute.dir}/out/${ant.project.name}-v${version}-${channelkey}.apk" />
<!-- 打包完一个渠道包后把渠道名和渠道号替换回原来格式,这样可以进行下一次替换,AndroidManifest.xml里所有匹配的东西都会被替换 -->
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:name="${channelname}""
replace="android:name="channelname"" />
<replaceregexp
byline="true"
encoding="utf-8"
file="AndroidManifest.xml"
match="android:value="${channelkey}""
replace="android:value="channelkey"" />
</target>
</project>
接下来还需要新建一个ant.properties配置文件:
key.store=key.store路径
key.store.password=密码
key.alias=写上你的alias
key.alias.password=密码
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
market_channels=UMENG_CHANNEL:3000,UMENG_CHANNEL:3001,UMENG_CHANNEL:3002
version=1.1.2
写完这个 之后,我们还需要更改AndroidManifest.xml中一处:
<meta-data android:name="channelname" android:value="channelkey"/>
记住,让这个在一行之中,连空格都要控制好,因为匹配中已经写好了相关的配置。
接下来大家可以运行了,但是如果运行中出现了——因为很多警告导致错误的,
那么大家可以看一看,是不是自己写了混淆文件,不能讲刚刚加入的jar 包混淆,
如果还是出现问题,那么直接添加上忽略warnings就可以了,感谢大家的捧场,希望大家可以多多喜欢。向上吧,少年。