利用ant自动编译打包android应用

Ant是著名Java开源组织Apache的一个项目,是一个基于java的build工具。它可以使你通过ant脚本语言,自动你的项目拷贝到某个目录,发布项目,或者生成一些代码,执行SQL语言。总之它可以帮助你完成项目开发中除了开发代码以外的大部分辅助性工作。
ANT语法简介:
1.ant的编译文件默认为build.xml,一般无需改变。
2.build.xml的根节点为,一般格式如下:

<project name="AntStudy" default="init" basedir="."></project>

name为工程名称;
default为默认的target,就是任务;
basedir就是基路径。一般为”.”

3.property可以定义变量,一般格式如下:

<property name="test" value="shit"></property>

引用的时候这么引用${test}
如果变量是路径的话,则可以这么定义:

<property name="src" value="src"></property>

这么定义就可以不用担心””和”/”的问题了。

4.定义target:

<target name="compile" depends="init"><!--other command--></target>

name的值为target的名称,可以在编译的时候指定是完成哪个target,否则采用project那里定义的default。
depends则定义了依赖关系,值为其他target的name。多个依赖关系用”,”隔开,顺序执行完定义的依赖关系,才会执行这个target。
target在build.xml中定义的顺序无所谓,但是depends中的顺序必须正确。

5.编译源代码:

<javac srcdir="src" destdir="classes">
    <classpath> 
        <fileset dir="lib"> 
            <include name="**/*.jar"></include> 
        </fileset>
    </classpath> 
</javac>

这个标签自动寻找src中以.java为扩展名的文件,并且调用javac命令。这个任务有个特点,它仅仅编译那些需要编译的源文件。如果没有更新,就不需要编译,速度就加快。编译文件和ant使用的同一个jvm,大大减少资源浪费。还可以指定classpath。classpath中指定文件夹,然后指定包含的文件的规则。

6.创建jar文件:

<jar destfile="antstudy.jar" basedir="classes">
    <manifest>
        <attribute name="Main-Class" value="bean.ant.TestAnt"></attribute>
    </manifest>
</jar>

manifest指定manifest中文件的属性,比如可以指定Main-Class

7.创建目录:

<mkdir dir="classes"></mkdir>

创建dir的文件夹。

8.删除目录:

<delete dir="classes"></delete>

删除classes文件夹

9.拷贝文件:

<copy todir="{backup.dir}">      

把fileset文件夹下面的所有文件拷贝到 backup.dir

10.执行一个类:

<java dir="${build}" classname="bean.ant.TestAnt" fork="true"></java>

dir为工作文件夹,classname为类名。fork要设置为true。因为你编译放class的文件夹正在使用,所以要新打开一个虚拟机。

11.生成javadoc:

<javadoc sourcepath="{src}" destdir="{docs}/javadoc" encoding="utf-8" charset="utf-8" 
windowtitle="Spring Framework" source="1.5" access="package" author="true" version="true" 
use="true" defaultexcludes="true">
    <doctitle>
    < ![CDATA[[Ant Test]]>
   </doctitle>
    <bottom>
    < ![CDATA[<i>Copyright (c) 2002-2007]]>
   </bottom>
    <packageset dir="${src}">
    <include name="bean/ant/**"></include>
    </packageset>
</javadoc>

encoding=”utf-8″ charset=”utf-8″ 需要,否则javadoc是乱码。packageset一定要设定,否则找不到源码,格式可以是**.意思是所有源码。

12.path的使用,可以定义path对象,在其他地方可以直接复用。

<path id="1"> 
   <pathelement location="."></pathelement> 
   <pathelement location="./lib/junit.jar"></pathelement> 
</path>
<path id="2"> 
   <fileset dir="lib"> 
   <include name="**/*.jar"></include> 
   </fileset> 
</path>
<javac srcdir="./src" destdir="./classes"> 
   <classpath refid="1"></classpath> 
</javac>
<javac srcdir="./src" destdir="./classes"> 
      <classpath refid="1"> 
          <pathelement location="."></pathelement> 
          <pathelement location="./lib/junit.jar"></pathelement> 
      </classpath> 
</javac>

13.单元测试:

<junit printsummary="yes">
   <classpath refid="testpath"></classpath>
   <batchtest>
    <fileset dir="${test}">
     <include name="**/*Test.java"></include>
    </fileset>
   </batchtest>
</junit>

batchtest为批量test,需要设置符合条件的类名称。在进行单元测试之前必须先进行编译。 配置的classpath指向一个testpath的path,就是上面哪一条里面讲的。一开始会报一个找不到junit的错误,可以用下面方法解决:Windows>>Preference>>Ant>>Runtime>>Classpath 添加一个junit.jar 在 [eclipse home]pluginsorg.junit_3.8.1可以找到。
熟悉以上基本的语法,基本够用了,不明白的地方可以随时百度或者Google。
利用命令

#android update project --path . --name projectname(此参数修改为项目名称) --target android-19

会自动生成ant编译需要的build.xml文件,大致内容如下:

< ?xml version="1.0" encoding="UTF-8"?>
<project name="qianlong" default="release">
    <!-- 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"></property>
    <!-- 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"></property>
    <!-- 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"></property>
    <condition property="sdk.dir" value="${env.ANDROID_HOME}">
        <isset property="env.ANDROID_HOME"></isset>
    </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"></loadproperties>
    <!-- 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"></fail>
    <!--
        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>
    <!-- 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><import file="${sdk.dir}/tools/ant/build.xml"></import>
</project>

拷贝

${sdk.dir}/tools/ant/build.xml

文件到项目目录下,改名为android_build.xml,将build.xml中

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

改成

<import file="android_build.xml"></import>

配置好后,打Android测试包,执行

#ant releaseTest

打开发环境包,执行

#ant releaseDev

打正式发布包,执行

#ant releasePro

将配置文件上传SVN,在Jenkins中配置好后即可实现自动构建打包了。