Android Eclipse Ant 编译打包

有三篇文章写得很好一起附上

http://blog.csdn.net/likebamboo/article/details/17888563
http://blog.csdn.net/likebamboo/article/details/17953259
http://www.cnblogs.com/sink_cup/p/android-ant-auto-release.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应用,对于我来说主要有以下两方面考虑:
1、我们在发布App的时候,可能需要发送到十几,甚至几十个不同的分发渠道,比如360手机市场,百度,应用宝等等,我们可能需要对各个渠道的下载量,用户存留和用户使用情况等数据进行分析,比如使用百度移动统计,友盟统计等。为了实现统计功能,我们需要在配置文件中添加一个数据元,来标识我们的应用要发布到哪一个渠道上,因此,若使用传统的方法,我们每发布一个渠道的版本,就需要修改清单文件中的数据元,然后再使用keystore进行签名和打包。若只有一两个分发渠道,工作量还是可以接受的,但是若我们的分发渠道打到几十个的时候,我们如果再手动的进行修改然后签名打包发布,那工作量就很可观了。因此,为解决这种需求,我们采用Ant来实现对Android应用的自动打包。
2、我们做产品的时候,肯定需要经常打不同环境的包,比如开发环境,测试环境,生产环境,这个时候你怎么办,如果用传统方法,你打开发环境包你要把你的服务端IP和图片服务器IP改成开发的,打包,然后打测试的包,你又要改成测试服务器IP和图片服务器IP,这样多麻烦,如果你把这服务端IP和图片服务器IP,配置到一个xml文件里,用ant打包方式实现自动替换,多方便。
 
 
ant 自动打包APK一般有以下两种方式:
1、使用原生Ant方式,就是把所有的环境变量和打包的起个步骤都配置到build.xml文件里,这样可以实现,但是这样的话也有不方便之处,你如果引用了第三方library(不是第三方jar包),配置起来可能就比较麻烦了,而且跨平台你可能就要修改一些配置了,比如windows下和linux下批处理文件不同等。所以我更推荐第二种方式,本文也主要介绍第二种方式。
2、使用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环境变量
1、在Ant官网(http://ant.apache.org/bindownload.cgi)下载最新Ant包,在http://sourceforge.net/projects/ant-contrib/files 下载Ant扩展包ant-contrib-1.0b3.jar(这个包就是用于循环编译多个渠道包)。
2、将Ant包解压到常用开发工具目录(自行选择,我的放在D:/Dev目录下),然后将下载下来的Ant扩展包ant-contrilb拷贝到Ant安装目录下的lib文件夹中。
 
3、设置Ant环境变量:ANT_HOME,变量值指向ant目录。
4、在环境变量Path里增加:%ANT_HOME%/bin;%ANT_HOME%/lib;
5、设置好了之后验证一下。打开CMD 输入ant -version命令出现下面反馈,说明ant 安装成功
三、配置打包项目
我的项目文件目录如下:

1、生成build.xml文件
打开cmd并进入到CalendarDemo这个项目目录下 使用android update project --name CalendarDemo --target android-19 -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>
复制代码
2、在项目目录下创建ant.properties和custom_rules.xml文件。ant.properties文件定义一些变量例如keystore密码,apk存放目录等;而custom_rules.xml这个文件就是用户自定义的编译规则文件。代码分别如下:
A、ant.properties文件内容如下
#keystore文件存放目录

key.store=./calendardemo_keystore

#keystore别名

key.alias=calendardemo_keystore

#keystore密码

key.store.password=xxxxxxx

#组织密码

key.alias.password=xxxxxxxx

四、打包
1、如果项目中没有引入第三方工程library,那么经过以上环节,就把整个项目Ant打包配置好了
2、如果项目中引入了第三方工程library,比如我的项目,引入三个第三方工程:
如果执行打包命令,那么会报错,这是因为那个library 还不支持ant自动编译,我们需要先让它也支持(注意:第三方工程要设置为Lib:
project->properties->Android->Library->Is Library 这个勾选上)。
进入到library项目所在的目录,输入命令 android update lib-project -p ./  (注意是 lib-project);
执行完之后,你会发现第三方工程目录下多了build.xml文件和local.properties文件。然后你在执行打包命令就可以成功打包了。
五:总结
1、build.xml和local.properties可以用命令生成,custom_rules.xml和ant.properties可以收藏起来,任何项目中都可以用。
2、ant脚本中还可以加入自动从SVN下载最新版本源码和打包之后通过ftp自动上传到服务器供下载,还可以加入定时打包功能。
3、一些异常情况的解决办法:
 
异常一:
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" />

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值