AndroidのBuild工具之Ant动手实践

来源:互联网 发布:淘宝会员要钱吗 编辑:程序博客网 时间:2024/04/29 17:59
好久没有写博客了,没半年也应该有几个月了。在工作上的项目遇到过很多问题或者说积累了不少经验,曾经都蛮想发到博客留个纪念什么的,不求可以为别人获得点经验技巧,只求在多年后遇到同样的问题可以找到个记录。但是,也许是懒终归是懒,而且上班时间写博客有点不好吧(下班后就容易忘记)。

由于用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.keykey.alias = testkey.store.password = 101817key.alias.password = 101817java.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全部加载出来了

0 0
原创粉丝点击