AndroidのBuild工具之Ant动手实践
好久没有写博客了,没半年也应该有几个月了。在工作上的项目遇到过很多问题或者说积累了不少经验,曾经都蛮想发到博客留个纪念什么的,不求可以为别人获得点经验技巧,只求在多年后遇到同样的问题可以找到个记录。但是,也许是懒终归是懒,而且上班时间写博客有点不好吧(下班后就容易忘记)。
由于用Eclipse导出签名包经常失败,或因内存不足或者其他什么搞不懂的原因,项目也确实算得上是有点庞大了,光library就有七八个,好不容易守着打包界面代码都不能敲一行看着打包对话框结束(通常就意味着导出apk成功了),最后又缺斤少两。正常情况下是大概有6.8M体积,占用空间7M多,但打不出来的包就会少那么一点有的时候5.8M有的时候6.8M,这样的包运行起来是会找不到类的。最恼人的时候,打了一天包都难打出一个标准无误的apk。就算打出来,这个过程也是非常久的,而且是占着Eclipse前台,在此期间你不能做别的事,所以就特别讨厌,然后就尝试学学ant,此前也用过Maven,在android上用起来还是有点不方便。
在网上看教程也看得忙累的,倒是找到一片博客照着做,最终也成功了(忘了是哪篇博客地址,不好意识),期间也遇到过几个问题,但是现在都忘了。
一.自动调用sdk目录下的build.xml来打包
如果在主工程build必须每一个library也update出来自己build.xml,而且library的目录最好不能有中文,要不然会出错。
用cmd进入library的目录,然后执行这条命令就可以为你的项目自动生成一个build.xml文件。
android update project --path
再然后就在你的主工程的build.xml执行ant release就可以胜利打包了。
如果要打签名包,就要在ant.properties写好keystore信息就可以自动生成签名包了。
<?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>
android update project --path命令执行后生成build.xml
key.store = C:/Users/Bvin/Desktop/test.key
key.alias = test
key.store.password = 101817
key.alias.password = 101817
java.encoding = UTF-8
ant.properties用以记录key.store信息。

-release-unaligned.apk未zip对齐的apk
-release-unsigned.apk未签名的apk
-release.apk对齐了也签了名的apk
如果混淆了在prouard有个mapping.xml文件保存了每次生成后的混淆对应文件
二:自定义build.xml实现扩展功能
第一种方法有局限性,用的是SDK目录下的build.xml文件所以也有很多不必要的文件,也不能重新对apk命名和复制到某个目录
前几天一直想做这个事,就是把bin目录下的release文件重名并赋值到指定目录,这样我就可以叫测试去我共享的目录去测试,而不是每次都是我用QQ发给他网速又慢。


如上图做个测试,在build.xml引入custom_rules.xml,在custom_rules定义一个target放在release后面,我就以为在build.xml执行release后会自动出来mytest的输出。
结果令我失望了。今天刚好看了一下《Ant权威指南》一书(PDF版,轻喷),里面说到"依赖关系指定了在当前目标执行前,Ant必须执行的目标",这下我就豁然开朗了,别的目标依赖release,但执行release目标并不会执行依赖它的目标,应该是执行mytest,这样ant就会先调用release然后执行mytest。
按照这个想法直接执行custom_rules.xml的mytest结果报错了,稍微百度一下然后想想应该是缺少了什么引用,然后就反过来import这个build.xml文件,这样就可以果然可以了
<?xml version="1.0" encoding="UTF-8"?>
<project name="test" default = ""> <!-- 申明sdk.dir -->
<property file="local.properties" /> <!-- 申明keystore -->
<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" /> <!-- 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"
/> <target name="mytest" depends="release">
<echo>finally suc</echo>
</target>
<!-- depends = "-release-sign" -->
<target name="copyApk"> <tstamp>
<format property= "nowtime" pattern = "yyyyMMdd_HH.mm"/>
</tstamp>
<echo level="info">copyApk...${ant.properties.name}-${nowtime}</echo>
<copyfile src = "${ant.project}/bin/${ant.project.name}-release.apk"
dest = "C:/Users/Bvin/Desktop/${ant.project.name}-${nowtime}.apk"
forceoverwrite = "true" />
<echo level="info">copy dir:"C:/Users/Bvin/Desktop/${ant.project.name}-${nowtime}.apk"</echo>
</target> <import file="${sdk.dir}/tools/ant/build.xml" />
</project>
最后胜利在打包然后输出了finally suc

引入build.xml这样ant也把build.xml里面的target全部加载出来了

AndroidのBuild工具之Ant动手实践的更多相关文章
- 【Android开发经验】使用Ant批量打包Android应用全然指南
本文章由Socks完毕.博客地址:http://blog.csdn.net/zhaokaiqiang1992 转载请说明. 折腾了一下午.百度了一下午,最终实现了使用Ant对Android应用的批量打 ...
- Android开发工具全面转向Android Studio(3)——AS project/module的目录结构(与Eclipse对比)
如果AS完全还没摸懂的,建议先看下Android开发工具全面转向Android Studio(2)——AS project/module的CRUD. 注:以下以Windows平台为标准,AS以目前最新 ...
- Android开发工具全面转向Android Studio(2)——AS project/module的CRUD
本文有些地方可能需要衔接Android开发工具全面转向Android Studio(1)——准备开发环境,读起来效果会更好. 这个世界很奇妙,所有的东西离不开CRUD,即增删改查.即使人本身也遵循这个 ...
- Android 常见工具类封装
1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...
- Gradle: The New Android Build System
Gradle: The New Android Build System Google selected Gradle as the foundation of the Android SDK bui ...
- 【转】Android ROM研究---Android build system增加模块
原文网址:http://hualang.iteye.com/blog/1141315 Android build system就是编译系统的意思 在我们需要向自己编译的源代码中增加模块的时候,需要一些 ...
- Android群英传》读书笔记 (1) 第一章 Android体系与系统架构 + 第二章 Android开发工具新接触
第一章 Android体系与系统架构 1.Dalvik 和 ARTDalvik好比是一辆可折叠的自行车,平时是折叠的,只有骑的时候,才需要组装起来用.ART好比是一辆组装好了的自行车,装好就可以骑了. ...
- 【转】理解 Android Build 系统----不错
$ mmm -help用法:make [选项] [目标] ...选项: -b, -m 忽略兼容性. -B, --always-make Unconditionally make all targets ...
- Android 测试工具集01
Appium是一个支持原生,混合和移动web apps的开源的跨平台测试框架工具. ANDROID依赖 Android SDK API >= 17 (Additional features re ...
随机推荐
- SQL Server 2008下日志清理方法 2
SQL Server 2008下日志清理方法 (2011-07-14 10:30:45) 转自 http://blog.sina.com.cn/s/blog_4bdd3d0b0100wfvq.html ...
- spark.Accumulator
scala> val accum = sc.accumulator() accum: org.apache.spark.Accumulator[Int] = scala> sc.paral ...
- flume sourcetype avro http
flume 当sourcetype=http的时候可以用 curl -X POST -d '[{"headers" :{"APPSTORE" : " ...
- DRAM 各项基本参数记录
记录一下DRAM的各项基本参数 tCL CAS Latency CAS 潜伏期, 列地址寻找/读写命令执行完毕,准备要读出来,需要一个延时缓一缓 tRAS: RAS Active Timeing 行有 ...
- hive中创建表失败
使用create table命令创建表失败,如下错误信息: hive> create table test(id int,name string,age int,sex string); FAI ...
- iOS开发小技巧--textField成为密码框,view加载完后textField获取焦点
文本框安全输入:Secure Text Entry(安全文本输入) view加载完后textField获取焦点的正确做法
- 专题实验 Statspack & 9大动态视图
statspack 是一个DBA经常用的调优工具, 它的主要作用是, 针对数据库的不同时刻做快照, 然后来比对快照之前的差异和瓶颈, 快照可以是手动的也可以是自动的, 从 oracle 10g开始, ...
- 判断asp.net中session过期的方法
判断asp.net中session过期的方法 转载自:http://www.cnblogs.com/xilipu31/archive/2013/04/12/3016830.html 方法一:最麻烦也是 ...
- 20个优秀的JavaScript 键盘事件处理库
键盘事件是 Web 开发中最常用的事件之一,通过对键盘事件的捕获和处理可以提高网站的易用性和交互体验.下面,我们向大家介绍收集的20款优秀的 JavaScript 键盘事件处理库,帮助开发人员轻松处理 ...
- qlineedit控件获得焦点
出处:http://blog.sina.com.cn/s/blog_640531380100wld9.html qlineedit控件获得焦点 lineEdit->setFocus();