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 ...
随机推荐
- OFFLINE
2013年9月22日 20:47:42 OFFLINE 今天没有网络,或许,只有没有网络才去做一些更加有意义的事情.人就是这么贱,不是么.或许已经有点强迫症的味道的了. 中午吃完饭有2个小时的休息时间 ...
- 【C#/WPF】.Net生成二维码QRCode的工具
先马 http://qrcodenet.codeplex.com/ 使用该工具WPF生成二维码的简单例子: 前台XAML准备一个Image控件显示二维码. string qrcodeStr = &qu ...
- 百度地图Api进阶教程-基础地图示例1.html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- MYSQL的用户变量(@)和系统变量(@@)
9.3. 用户变量 可以先在用户变量中保存值然后在以后引用它:这样可以将值从一个语句传递到另一个语句.用户变量与连接有关.也就是说,一个客户端定义的变量不能被其它客户端看到或使用.当客户端退出时,该客 ...
- 25+开源的在线购物软件(PHP, JavaScript 和 ASP.Net)
25 +免费开源的电子商务解决方案,提供了建立一个在线购物所有主要功能,并能够连接到一个支付处理系统1. Magento Magento是一套专业开源的PHP电子商务系统.Magento设计得非常灵活 ...
- 【转】MFC 之CEvent
event是用来同步不同线程的.一旦一个线程结束了自己对全局资源的使用,他通过调用SetEvent通知别人可以使用了.如果这个被删了,其他线程将被阻塞 当一个线程里调用了::WaitForSingle ...
- am335x gpio分析
/************************************************************************ * am335x_gpio * 本文主要记录am33 ...
- C++ 有用的书籍
C++ 有用的书籍Essential C++ 中文版C++ Primer Plus 第6版中文版C++ Primer中文版(第5版) #include <iostream> /* run ...
- WiFi(网络)调试Android手机
手机需要root 使用adb tcpip命令开启网络调试功能,一旦手机重启,又要重复这些步骤,比较麻烦. 一劳永逸的方法是,使用re管理器(给予root权限)在手机的/system/build.pro ...
- 《开源框架那些事儿22》:UI框架设计实战
UI是User Interface的缩写.通常被觉得是MVC中View的部分,作用是提供跟人机交互的可视化操作界面. MVC中Model提供内容给UI进行渲染,用户通过UI框架产生响应,一般而言会由控 ...