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

ANT 打包笔记

2016-12-13 14:30 113 查看
参照:

https://github.com/cundong/blog/blob/master/Android%20Ant%20%E6%89%B9%E9%87%8F%E5%A4%9A%E6%B8%A0%E9%81%93%E6%89%93%E5%8C%85%E5%AE%9E%E4%BE%8B.md

一、工具下载:

1.apache-ant

http://ant.apache.org/manualdownload.cgi

2. ant-contrib

https://sourceforge.net/projects/ant-contrib/files/ant-contrib/

二、环境搭配

如图:



三、生成local.properties、build.xml文件

命令格式:

android update project –target {target版本} –name {工程名字} –path {工程目录}

比如: D:\workspace\ 目录下有两个项目 MyProject,DependLib,MyProject 依赖于DependLib,

则需要在控制台上执行:

D:\build\sdk\tools\android update project –target android-19 –name MyProject –path D:\Brian\workspace\MyProject

D:\build\sdk\tools\android update project –target android-19 –name DependLib –path D:\workspace\DependLib

四、添加ant.properties

这个需要在MyProject里配置,DependLib若没用到则不需要。

输入内容如下:



五、修改local.properties

MyProject项目需要修改成如图:这里配置的是打包环境

Sdk.dir 指定sdk目录,ant.dir指定ant目录,target.dir指定打包后输出的目录



DependLib项目需要修改local.properties成:



这里只需要指定sdk目录即可。

六、编辑build.xml脚本

想要实现多渠道打包MyProject这里就需要更改,而DependLib项目不需要配置这项。

内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<project
name="Yikaduohao_V1.3.2"
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" /> -->

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

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

<!-- 以下是打包添加的配置 -->

<property file="ant.properties" />
<!-- taskdef作用是,创建一个名字相同的task,在task被调用时,相应的taskdef中的方法会被执行者 -->

<taskdef resource="net/sf/antcontrib/antcontrib.properties" >

<classpath>

<!--
Ant-contrib 为 Ant 提供了与通常所使用的编程语言功能相同的
<if> 、 <for>、<switch>等逻辑判断任务,支持对字符串的排序<sortlist>任务,
甚至还支持常见的数学运算,如加、减、乘、除、求余等功能。
在构建过程中灵活运用这两个扩展包,将大大增强 Ant 的可编程性,
这其实就是一种基于 XML 脚本的编程。支持循环执行

-->

<pathelement location="${ant.dir}/lib/ant-contrib-1.0b3.jar" />
</classpath>
</taskdef>

<target name="deploy" >

<!-- 替换 版本号 -->

<replaceregexp
encoding="utf-8"
file="AndroidManifest.xml"
flags="g"
match="0000000000"
replace="${deployYear}${deployMonth}${deployDay}${deployHour}" />
<!-- 替换 版本名称 -->

<replaceregexp
encoding="utf-8"
file="AndroidManifest.xml"
flags="g"
match="XTXLVersionName"
replace="${app_version}" />

<!-- 替换 发布日期 -->

<replaceregexp
encoding="utf-8"
file="res/values/strings.xml"
flags="g"
match="XTXLFBRQ"
replace="${deployYear}.${deployMonth}.${deployDay}" />
<!-- 替换 版本名称 -->

<replaceregexp
encoding="utf-8"
file="res/values/strings.xml"
flags="g"
match="XTXLVersionName"
replace="${app_version}" />
<!-- 迭代 1.target:每次迭代调用的target 2.list:列表 3.delimiter:分隔符 4.param:迭代使用的变量名 -->
<!-- 执行正常打包 -->

<!--  <antcall target="normal_build" /> -->
<antcall target="channel_build" />

<echo>
====================================================================
build 完成。
====================================================================

</echo>
<!-- 将修改的各个字段还原成原来的字段 -->

<replaceregexp
encoding="utf-8"
file="AndroidManifest.xml"
flags="g"
match="${deployYear}${deployMonth}${deployDay}${deployHour}"
replace="0000000000" />

<replaceregexp
encoding="utf-8"
file="AndroidManifest.xml"
flags="g"
match="${app_version}"
replace="XTXLVersionName" />

<replaceregexp
encoding="utf-8"
file="res/values/strings.xml"
flags="g"
match="${deployYear}.${deployMonth}.${deployDay}"
replace="XTXLFBRQ" />

<replaceregexp
encoding="utf-8"
file="res/values/strings.xml"
flags="g"
match="${app_version}"
replace="XTXLVersionName" />
</target>

<!-- 无多渠道入口 -->

<target name="normal_build" >

<echo>
-----------------

无 多渠道入口
normal_build...
-----------------

</echo>

<property
name="out.final.file"
location="${target.dir}/${app_name}_v${app_version}_${deployYear}${deployMonth}${deployDay}${deployHour}.apk" />

<antcall target="clean" />

<antcall target="release" />

</target>

<!-- 多渠道入口 -->

<target name="channel_build" >

<echo>
多渠道入口
channel_build...
-----------------

</echo>

<foreach
delimiter=","
list="${market_channels}"
param="channelnameandkey"
target="edit_and_build" >
</foreach>
</target>

<target name="edit_and_build" >

<propertyregex
input="${channelnameandkey}"
override="true"
property="channelname"
regexp="(.*)-"
select="\1" />

<propertyregex
input="${channelnameandkey}"
override="true"
property="channelkey"
regexp="-(.*)"
select="\1" />

<replaceregexp
encoding="utf-8"
file="AndroidManifest.xml"
flags="g"
match="A3BC8CD48B0BB0D1110264C9181AEFC4"
replace="${channelkey}" />

<property
name="out.final.file"
location="${target.dir}/${app_name}_v${app_version}_${deployYear}${deployMonth}${deployDay}${deployHour}_${channelname}.apk" />

<antcall target="clean" />

<antcall target="release" />

<replaceregexp
encoding="utf-8"
file="AndroidManifest.xml"
flags="g"
match="${channelkey}"
replace="A3BC8CD48B0BB0D1110264C9181AEFC4" />
</target>

</project>


注意:这一项需要指定ant-contrib-1.0b3.jar 所在目录。

七:修改AndroidManifest.xml

在配置文件中添加 app_key项以实现多渠道打包,VersionCode和VersionName 在打包时将会被ant.properties 里的内容所替换。



八、打包

进入MyProject 根目录,执行命令:

Ant deploy

若无意外,打包即可完成。重点内容

九、遇到的一些问题,及解决方案。

问题一:



解决方案: 修改sdk\tools\ant 下的build.xml, 如图:



问题二:



解决方案:这个是由于编码格式而出现的问题,项目本身是使用GBK,而build.xml指定使用UTF-8。修改sdk\tools\ant 下的build.xml, 如图:



问题三:



出现以上信息,内心奔溃,查看了下项目,果然出现了中文乱码,将项目中的乱码解决了就好。

我的项目默认是GBK,而上面的文件是别人以UTF-8编码格式提交的代码。

问题四:



这里出现问题的原因没能完全知道。R.java在生成的时候会根据资源文件来生成,其中主要是由 aapt 来生成,如果资源上有中文注释,那么R.java也会生成中文注释,这本身并不存在问题,正常情况也是能够编译通过,但我的项目是svn上check下来的,项目的编码本来约定为GBK,但就是有某个人使用了UTF-8格式,造成了乱码,我试着调整编码,但始终都出现了上述错误,至于是否是由于编码而造成的还不能定乱,但是把中文注释全删了问题就解决了,令我费解的是同事的项目却能正常编译,后来想了下,既然问题的根源是中文注释出了差错,那么在生成R.java文件时不生成便可以解决问题,后来分析了下appt命令,发现appt命令默认生成注释,ok,那只有在生成了R.java之后再删掉注释了,哈哈哈,问题也终于解决了。

解决方案: 在生成R.java文件后把R.java文件里的注释全部删掉(好粗暴 (๑´ڡ`๑) )





注:上面的代码都是在 target –code-gen 里面添加,一个在头部,一个在尾部。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息