windows下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件
原创文章,转载请注明:http://www.cnblogs.com/ycxyyzw/p/4535459.html
android 程序打包成apk,如果在是命令行方式,一般都要经过如下步骤:
















<?xml version="1.0" encoding="UTF-8"?>
<project name="epeiwang" 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 -->
<!-- 表示我们引用了sdk的ant的build文件-->
<import file="${sdk.dir}/tools/ant/build.xml" />
</project>
#keystore文件存放目录
key.store=./epeiwang_keystore
#keystore别名
key.alias=epeiwang_keystore
#keystore密码
key.store.password=xxxxxxx
#组织密码
key.alias.password=xxxxxxxx
#如果还没有生成keystore证书,可以使用下面命令在项目目录下生成一个test.keystore证书文件
#generate test.keystore
#keytool -genkey -alias test.keystore -keyalg RSA -validity 20000 -keystore test.keystore
#apk.dir表示存放最终生成apk的目录
apk.dir=./apk
#定义项目名称
app.name=epeiwang
#渠道号,多个渠道号用逗号分隔,每个渠道号不要使用违规字符例如/:等,因为渠道号会在打包的时候放在apk的文件名中,所以包含#违规字符将无法生成最终的apk,哥就是被这个细节给坑了一个下午。这里定义了两个渠道号myapp-12345和BAI-3s322d
market_channels=epeiwang,baidu,91
#测试环境服务器配置
test.server.url=192.168.1.10/epeiwang
test.server.image.url=192.168.1.9
test.epeiwang.url=192.168.1.10
#生产环境服务器配置
rel.server.url=111.111.111.222/epeiwang
rel.server.image.url=111.111.111.229
rel.epeiwang.url=www.epeiwang.com
#测试环境标识 给apk命名的时候用
test.tag.name=test
#生产环境标识 给apk命名的时候用
release.tag.name=release
<?xml version="1.0" encoding="UTF-8"?>
<project name="custom_rules" >
<!-- 引用ant-contlib这个扩展包,声明一下 -->
<taskdef resource="net/sf/antcontrib/antcontrib.properties" >
<classpath>
<pathelement location="${ant.ANT_HOME}/lib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>
<!-- 定义一个时间变量,打完包后跟渠道号一起命名apk -->
<tstamp>
<format
pattern="yyyyMMddhhmm"
property="pktime"
unit="hour" />
</tstamp>
<!-- 创建apk存放目录 -->
<mkdir dir="${apk.dir}" >
</mkdir>
<!-- 替换参数 然后打包APK -->
<target name="replace_parameter" >
<!-- 替换服务器配置 -->
<replaceregexp
byline="false"
encoding="UTF-8"
flags="g" >
<!-- 这个是正则表达式匹配hostconfig中epeiwang_server的值 -->
<regexp pattern="epeiwang_server>(.*)</epeiwang_server" />
<substitution expression="epeiwang_server>${server_url}</epeiwang_server" />
<fileset
dir=""
includes="res/xml/hostconfig.xml" />
</replaceregexp>
<replaceregexp
byline="false"
encoding="UTF-8"
flags="g" >
<!-- 这个是正则表达式匹配hostconfig中epeiwang_img_server的值 -->
<regexp pattern="epeiwang_img_server>(.*)</epeiwang_img_server" />
<substitution expression="epeiwang_img_server>${server_image_url}</epeiwang_img_server" />
<fileset
dir=""
includes="res/xml/hostconfig.xml" />
</replaceregexp>
<replaceregexp
byline="false"
encoding="UTF-8"
flags="g" >
<!-- 这个是正则表达式匹配hostconfig中epeiwang_url的值 -->
<regexp pattern="epeiwang_url>(.*)</epeiwang_url" />
<substitution expression="epeiwang_url>${epeiwang_url}</epeiwang_url" />
<fileset
dir=""
includes="res/xml/hostconfig.xml" />
</replaceregexp>
</target>
<!-- 打包测试环境命令就用这个 -->
<target name="deploytest" >
<!-- 传服务器配置参数到 replace_parameter这个打包target -->
<antcall target="replace_parameter" >
<param
name="server_url"
value="${test.server.url}" />
<param
name="server_image_url"
value="${test.server.image.url}" />
<param
name="epeiwang_url"
value="${test.epeiwang.url}" />
</antcall>
<!-- 执行循环打包target foreach_replacechannel -->
<antcall target="foreach_replacechannel" >
<!-- apk命名时候用到的参数 -->
<param
name="deploy_environment"
value="${test.tag.name}" />
</antcall>
</target>
<!-- 打包生产环境命令就用这个 -->
<target name="deployrel" >
<!-- 传服务器配置参数到 replace_parameter这个打包target -->
<antcall target="replace_parameter" >
<param
name="server_url"
value="${rel.server.url}" />
<param
name="server_image_url"
value="${rel.server.image.url}" />
<param
name="epeiwang_url"
value="${rel.epeiwang.url}" />
</antcall>
<!-- 执行循环打包target foreach_replacechannel -->
<antcall target="foreach_replacechannel" >
<!-- apk命名时候用到的参数 -->
<param
name="deploy_environment"
value="${release.tag.name}" />
</antcall>
</target>
<!-- 循环打包的target -->
<target name="foreach_replacechannel" >
<!-- 开始循环打包,从market_channels参数中取出一个渠道号用channel标识,然后通过正则修改manifest文件 -->
<foreach
delimiter=","
list="${market_channels}"
param="channel"
target="modify_manifest" >
</foreach>
</target>
<target name="modify_manifest" >
<replaceregexp
byline="false"
encoding="UTF-8"
flags="g" >
<!--
这个是正则表达式匹配manifest中meta,我用的友盟的统计,我 AndroidManifest中的配置为:
<meta-data android:value="360shichang" android:name="UMENG_CHANNEL"
-->
<regexp pattern="android:value="(.*)" android:name="UMENG_CHANNEL"" />
<substitution expression="android:value="${channel}" android:name="UMENG_CHANNEL"" />
<fileset
dir=""
includes="AndroidManifest.xml" />
</replaceregexp>
<!-- 这里设置最终生成包的存放目录以及apk的名称,注意这里是文件名称,所以变量中不允许出现违规字符,否则将无法生成最终的apk(会出现output is not valid 的错误) -->
<property
name="out.final.file"
location="${apk.dir}/${app.name}_${channel}_${deploy_environment}_${pktime}.apk" />
<antcall target="clean" />
<antcall target="release" />
</target>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<!-- 为了ant打包时候正则匹配,请不要格式化该文件 -->
<hostconfig>
<epeiwang_server>1111.1111.1111.1/epeiwang</epeiwang_server>
<epeiwang_img_server>1111.1111.1111.1</epeiwang_img_server>
<epeiwang_url>www.epeiwang.com</epeiwang_url>
</hostconfig>








windows下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件的更多相关文章
- linux(以ubuntu为例)下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件
原创,转载请注明:http://www.cnblogs.com/ycxyyzw/p/4555328.html 之前写过一篇<windows下Android利用ant自动编译.修改配置文件.批量 ...
- Android 自动编译、打包生成apk文件 3 - 使用SDK Ant方式
相关文章列表: < Android 自动编译.打包生成apk文件 1 - 命令行方式> < Android 自动编译.打包生成apk文件 2 - 使用原生Ant方式> &l ...
- Android 自动编译、打包生成apk文件 2 - 使用原生Ant方式
from://http://blog.csdn.net/androiddevelop/article/details/11100109 相关文章列表: <Android 自动编译.打包生成apk ...
- Android 自动编译、打包生成apk文件 4 - 多渠道批量打包
相关文章列表: < Android 自动编译.打包生成apk文件 1 - 命令行方式> < Android 自动编译.打包生成apk文件 2 - 使用原生Ant方式 > < ...
- 使用ant自动编译、打包生成apk文件
上次使用命令行生成apk文件<Android 命令行编译.打包生成apk文件>,学习命令行生成的目的是为了编写ant打下基础. 一. ant环境 下载ant包,配置环境变量 二.ant编译 ...
- 在eclipse中将android工程打包生成apk文件
1.)生成keystore 按照下面的命令行 在C:\Program Files\Java\jdk1.6.0_10\bin>目录下,输入keytool -genkey -alias androi ...
- Android 命令行编译、打包生成apk文件
一.搭建搭建环境 1. 安装JDK 和 Android SDK 2. 配置环境变量 D:\android-sdk-windows\tools C:\Program Files\Java\jdk1. ...
- Ant自动编译打包&发布 android项目
Eclipse用起来虽然方便,但是编译打包android项目还是比较慢,尤其将应用打包发布到各个渠道时,用Eclipse手动打包各种渠道包就有点不切实际了,这时候我们用到Ant帮我们自动编译打包了. ...
- Android - Ant自动编译打包android项目 -- 1(转)
1. 背景: Eclipse用起来虽然方便,但是编译打包android项目还是比较慢,尤其当要将应用打包发布到各个渠道时,用Eclipse手动打包各种渠道包就有点不切实际了,这时候我们用到Ant帮我 ...
随机推荐
- UIAlertView、 UIActionSheet
一.UIAlertView. UIActionSheet都是ios系统自带的弹出式对话框,当UIAlertView或UIActionSheet弹出来时用户无法与应用界面中的其它控件交互,UIAlert ...
- JSP标准标签库(JSTL)之核心标签(下)
前面记录了核心标签的前几种,现在来接着记录. 七.<c:import>标签 使用<c:import>标签可以包含一个FTP服务器中不同的网页内容.语法格式: <c:i ...
- 【概念笔记】JavaEE - web part2
IT`huhui前言录 续JavaEE - web part1 链接http://www.cnblogs.com/ithuhui/p/5930745.html, 持续修改更新. Cookie 1. 定 ...
- 新手容易混乱的String+和StringBuffer,以及Java的方法参数传递方式。
之前在交流群里和猿友们讨论string+和stringbuffer哪个速度快以及Java的方法参数传递的问题,引起了群里猿友的小讨论.最终LZ得出的结果是string+没有stringbuffer快, ...
- Nginx学习笔记(四) 源码分析&socket/UDP/shmem
源码分析 在茫茫的源码中,看到了几个好像挺熟悉的名字(socket/UDP/shmem).那就来看看这个文件吧!从简单的开始~~~ src/os/unix/Ngx_socket.h&Ngx_s ...
- [stm32][ucos] 1、基于ucos操作系统的LED闪烁、串口通信简单例程
* 内容简述: 本例程操作系统采用ucos2.86a版本, 建立了5个任务 任务名 优先级 ...
- AngularJS快速入门指南05:控制器
AngularJS控制器用来控制AngularJS applications的数据. AngularJS控制器就是普通的JavaScript对象. AngularJS控制器 AngularJS app ...
- JavaScript事件详解-zepto的事件实现
zepto的event 可以结合上一篇JavaScript事件详解-原生事件基础(一)综合考虑源码暂且不表,github里还有中文网站都能下到最新版的zepto.整个event模块不长,274行,我们 ...
- C语言实现二叉树
二叉树的重要性就不用多说啦: 我以前也学习过,但是一直没有总结: 网上找到的例子,要么是理论一大堆,然后是伪代码实现: 要么是复杂的代码,没有什么解释: 最终,还是靠FQ找到一些好的文章,参考地址我会 ...
- duilib进阶教程 -- 各种控件的响应 (10)
到上一个教程为止,界面显示的代码就都介绍完啦,现在开始介绍控件的响应,其实在<2013 duilib入门简明教程 -- 事件处理和消息响应 (17)>里已经列出了duilib自己定义的所有 ...