windows下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件

原创文章,转载请注明:http://www.cnblogs.com/ycxyyzw/p/4535459.html

android 程序打包成apk,若是在是命令行方式,通常都要通过以下步骤:

1.用aapt命令生成R.java文件
2.用aidl命令生成相应java文件
3.用javac命令编译java源文件生成class文件
4.用dx.bat将class文件转换成classes.dex文件
5.用aapt命令生成资源包文件resources.ap_
6.用apkbuilder.bat打包资源和classes.dex文件,生成unsigned.apk
7.用jarsinger命令对apk认证,生成signed.apk
 
eclipase手动打包生成apk方式,只不过是eclipase代替咱们执行了以上命令而已。
 
eclipse用起来虽然方便,为何要使用Ant批量打包Android应用,对于我来讲主要有如下两方面考虑:
一、咱们在发布App的时候,可能须要发送到十几,甚至几十个不一样的分发渠道,好比360手机市场,百度,应用宝等等,咱们可能须要对各个渠道的下载量,用户存留和用户使用状况等数据进行分析,好比使用百度移动统计,友盟统计等。为了实现统计功能,咱们须要在配置文件中添加一个数据元,来标识咱们的应用要发布到哪个渠道上,所以,若使用传统的方法,咱们每发布一个渠道的版本,就须要修改清单文件中的数据元,而后再使用keystore进行签名和打包。若只有一两个分发渠道,工做量仍是能够接受的,可是若咱们的分发渠道打到几十个的时候,咱们若是再手动的进行修改而后签名打包发布,那工做量就很可观了。所以,为解决这种需求,咱们采用Ant来实现对Android应用的自动打包。
二、咱们作产品的时候,确定须要常常打不一样环境的包,好比开发环境,测试环境,生产环境,这个时候你怎么办,若是用传统方法,你打开发环境包你要把你的服务端IP和图片服务器IP改为开发的,打包,而后打测试的包,你又要改为测试服务器IP和图片服务器IP,这样多麻烦,若是你把这服务端IP和图片服务器IP,配置到一个xml文件里,用ant打包方式实现自动替换,多方便。
 
 
ant 自动打包APK通常有如下两种方式:
一、使用原生Ant方式,就是把全部的环境变量和打包的起个步骤都配置到build.xml文件里,这样能够实现,可是这样的话也有不方便之处,你若是引用了第三方library(不是第三方jar包),配置起来可能就比较麻烦了,并且跨平台你可能就要修改一些配置了,好比windows下和linux下批处理文件不一样等。因此我更推荐第二种方式,本文也主要介绍第二种方式。
二、使用Android SDK Ant方式,Android SDK中提供了包含以前写过的操做的封装,只须要使用一条命令android update project生成build.xml ,以后再修改配置文件支持不一样特性便可,彻底不用写ant代码,这些都由Android SDK自动生成。下面我来详细介绍下此种方式。
 
以咱们如今开发的产品为例:
 
一:设置JAVA环境变量
这个我就不说啥了,作android开发的配置这个是基础。
二:配置Android的SDK环境变量 
除了须要Java的环境变量,咱们还须要配置Android的sdk的位置,名字是ANDROID_HOME,值就是你的android的sdk的位置,好比个人,就以下所示:
 
二:安装ant并设置ant环境变量
一、在Ant官网(http://ant.apache.org/bindownload.cgi)下载最新Ant包,在http://sourceforge.net/projects/ant-contrib/files 下载Ant扩展包ant-contrib-1.0b3.jar(这个包就是用于循环编译多个渠道包)。
二、将Ant包解压到经常使用开发工具目录(自行选择,个人放在D:/Dev目录下),而后将下载下来的Ant扩展包ant-contrilb拷贝到Ant安装目录下的lib文件夹中。
 
三、设置Ant环境变量:ANT_HOME,变量值指向ant目录。
四、在环境变量Path里增长:%ANT_HOME%/bin;%ANT_HOME%/lib;
五、设置好了以后验证一下。打开CMD 输入ant -version命令出现下面反馈,说明ant 安装成功
3、配置打包项目
个人项目文件目录以下:
一、生成build.xml文件
打开cmd并进入到epeiwang这个项目目录下 使用android update project -n epeiwang -p . 命令(注意-n表示项目的名称,-p参数后面有个点 表示当前目录)。
 
执行这个命令后,会在项目中自动生成build.xml和local.properties文件。
 
build.xml文件内容以下,并注意红色部门标识的代码。这两个文件是须要用户本身建立的,并存放在当前项目目录下。
 
<?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>

 

 
二、在epeiwangyxhd项目目录下建立ant.properties和custom_rules.xml文件。ant.properties文件定义一些变量例如keystore密码,apk存放目录等;而custom_rules.xml这个文件就是用户自定义的编译规则文件。代码分别以下:
 
A、ant.properties文件内容以下:
 
#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

 

B、custom_rules.xml文件内容以下:
 
<?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>(.*)&lt;/epeiwang_server" />
<substitution expression="epeiwang_server>${server_url}&lt;/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>(.*)&lt;/epeiwang_img_server" />
<substitution expression="epeiwang_img_server>${server_image_url}&lt;/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>(.*)&lt;/epeiwang_url" />
<substitution expression="epeiwang_url>${epeiwang_url}&lt;/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=&quot;(.*)&quot; android:name=&quot;UMENG_CHANNEL&quot;" />
<substitution expression="android:value=&quot;${channel}&quot; android:name=&quot;UMENG_CHANNEL&quot;" />
<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>

 

个人服务器IP配置hostconfig.xml文件内容为:
 
<?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>

 

 
4、打包
一、若是项目中没有引入第三方工程library,那么通过以上环节,就把整个项目Ant打包配置好了,进入CMD,在项目目录下使用ant deployrel命令,自动打生产环境的包,使用 ant deploytest,自动打测试环境的包。
二、若是项目中引入了第三方工程library,好比个人项目,引入三个第三方工程:
若是执行打包命令,那么会报错,这是由于那个library 还不支持ant自动编译,咱们须要先让它也支持(注意:第三方工程要设置为Lib:
project->properties->Android->Library->Is Library 这个勾选上)。
进入到library项目所在的目录,输入命令 android update lib-project -p ./  (注意是 lib-project);
执行完以后,你会发现第三方工程目录下多了build.xml文件和local.properties文件。而后你在执行打包命令就能够成功打包了。
 
五:总结
一、build.xml和local.properties能够用命令生成,custom_rules.xml和ant.properties能够收藏起来,任何项目中均可以用。
二、ant脚本中还能够加入自动从SVN下载最新版本源码和打包以后经过ftp自动上传到服务器供下载,还能够加入定时打包功能。
三、一些异常状况的解决办法:
 
异常一:
BUILD FAILED
D:\Android\sdk\tools\ant\build.xml:601: The following error occurred while executing this line:
D:\Android\sdk\tools\ant\build.xml:653: The following error occurred while executing this line:
D:\Android\sdk\tools\ant\build.xml:698: null returned: 1
 
解决办法:本身项目的build.xml文件中加入:
<property name="aapt.ignore.assets" value="!.svn:!.git:\x3Cdir\x3E_*:.*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~:crunch" />
 
异常二:
BUILD FAILED
D:\ProjectDemo\build.xml:83: Cannot find D:\ProjectDemo\android-sdk-windows\tools\ant\build.xml imported from D:\ProjectDemo\build.xml
 
解决办法:修改local.projects,必须是双斜杠
sdk.dir=D:\\android-sdk-windows
 
异常三:
 [aapt] D:\ProjectDemo\res\layout\activity_main.xml:2: error: Error: String types not allowed (at 'layout_width' with value 'match_parent').
     [aapt] D:\ProjectDemo\res\layout\activity_main.xml:2: error: Error: String types not allowed (at 'layout_height' with value 'match_parent').
 
BUILD FAILED
D:android-sdk-windows\tools\ant\build.xml:650: The following error occurred while executing this line:
D:android-sdk-windows\tools\ant\build.xml:691: null returned: 1
 
Total time: 1 second
 
解决办法:当前Andorid版本不支持match_parent属性值,match_parent是Android 8之后开始支持的属性值,修改AndroidManifest中<uses-sdk android:minSdkVersion="8" />最少也要大于8。不过也能够把match_parent改成FILL_PARENT
 

相关文章
相关标签/搜索