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

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

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

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

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

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

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

5.编译源代码:

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

6.创建jar文件:

   

   

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

7.创建目录:

 
创建dir的文件夹。

8.删除目录:

 
删除classes文件夹

9.拷贝文件:

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

10.执行一个类:

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

11.生成javadoc:



   < ![CDATA[

Ant Test]]> < ![CDATA[Copyright (c) 2002-2007]]>

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

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

 
    
    

 
    
    
    

 
    

 
       
           
           
       

13.单元测试:


   
   
    
     
    
   
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"?>

    
    
    
    
    
    
    
        
    
    
    
    
    
    
    
    
    
    

拷贝

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

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

android_build.xml文件内容修改版如下(有注释):

< ?xml version="1.0" encoding="UTF-8"?>

    
    
    
    
    
    
    
        
    
    
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
    
    
    
    
    
    
        
        
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
        
    
    
    
        
            
            
            
        
    
    
        
            
            
            
        
    
    
    
    
    
        
            
            
                
            
        
    
    
    
    
    
    
        
        
        
        
            
                
            
            
                @{elseText}
            
        
        
    
    
    
        
        
        
        
            
                
            
            
                
                    
                        
                    
                    
                        @{elseText}
                    
                
            
        
        
    
    
    
        
        
        
            
            
            
            
                
                    
                
                
                    
                        
                    
                
            
            
                
                
                
            
        
    
    
    
        
        
            
                
                
                
                
                
                
                
                
            
        
    
    
    
        
        
        
            
        
    
    
        
        
        
            Running tests ...
            
                
                
                
                
                
                
                
                
                
                
            
        
    
    
        
        
        
            
                
            
        
    
    
        
            
            
            
            
        
    
    
        
        
            Uninstalling @{app.package} from the default emulator or device...
            
                
                
                
            
        
    
    
    
    
    
    
        
    
    
    
        
    
    
    
        Project Name: ${ant.project.name}
        
        
        
            
        
        
            
        
        
            
        
        
        
            
                
            
        
        
        
    
    
    
    
    
        
        
        
        
            
                
                
                    
                    
                
            
        
        
        
            
            
                
                
                    
                        
                    
                    
                        
                        
                            
                            
                        
                    
                
            
        
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
            
                
                    
                    
                
            
            
                Switching between debug and non debug build: Deleting previous compilation output...
                
            
            
                
                
                    
                        
                            
                            
                        
                    
                    
                        Switching from instrumented to non-instrumented build: Deleting previous compilation output...
                        
                    
                
            
        
        Resolving Build Target for ${ant.project.name}...
        
        
        
        
        ----------
        Creating output directories if needed...
        
        
        
        
        
        
        
            
            
            
        
        ----------
        Resolving Dependencies for ${ant.project.name}...
        
        
        
            
                
                    
                    
                
            
            
                
                
                    
                
                ----------
                Building Libraries with '${project.libraries.target}'...
                
                
                    
                    
                    
                
            
        
        
        
            
                
                
                    
                
                ----------
                Building tested project at ${tested.project.absolute.dir} with '${tested.project.target}'...
                
                    
                
                
                
            
            
                
                
            
        
    
    
    
    
    
        
        
            
        
        
            Handling aidl files...
            
                
            
            
            ----------
            Handling RenderScript files...
            
            
            
                
            
            ----------
            Handling Resources...
            
                
                
            
            ----------
            Handling BuildConfig class...
            
        
    
    
    
    
    
        
            
            
                
                
                
            
            
                
                
                
            
            
            
                
                    Instrumenting classes from ${out.absolute.dir}/classes...
                    
                    
                    
                    
                    
                    
                        
                            
                            
                        
                    
                
            
            
            
                
                    Creating library output jar file...
                    
                    
                        
                            
                        
                        
                            Custom jar packaging exclusion: ${android.package.excludes}
                        
                    
                    
                    
                        
                        
                    
                
            
        
    
    
    
    
    
        
            
                
                
                
                
                
                
                
                
                
                
                    
                        
                        
                    
                
                
                
                    
                    
                
                
                
                    
                        
                        
                    
                
                
                
                    
                
                
                
                
                
                
                
                    -include      "${proguard.configcmd}"
                    -include      "${out.absolute.dir}/proguard.txt"
                    -injars       ${project.all.classes.value}
                    -outjars      "${obfuscated.jar.file}"
                    -libraryjars  ${project.target.classpath.value}
                    -dump         "${obfuscate.absolute.dir}/dump.txt"
                    -printseeds   "${obfuscate.absolute.dir}/seeds.txt"
                    -printusage   "${obfuscate.absolute.dir}/usage.txt"
                    -printmapping "${obfuscate.absolute.dir}/mapping.txt"
                
            
        
    
    
    
        
            
            
                
                
                    
                        
                            
                                
                            
                        
                    
                    
                        
                    
                
            
        
    

    
        
            
            
            
            
            
            
        
    
    
    
        
        
            
                
                
                
                
            
        
    
    
    
        
        
            
                
                    
                        
                            
                            
                        
                    
                
                
                    
                
            
        
    
    
    
    
        
    
    
    
    
    
        
        
        
    
    
        
        
        
            
                
                    
                    
                
            
            
                
            
            
                
            
        
        
        
        
        
        
        
    
    
        
        
    
    
    
        
        
            
                
                Debug Package: ${out.final.file}
            
        
        
    
    
    
    
    
    
    
    
    
        
        
        
    
    
    
        
        
            
                No key.store and key.alias properties found in build.properties.
                Please sign ${out.packaged.file} manually
                and run zipalign from the Android SDK tools.
            
        
        
    
    
        proguard.config is ${proguard.config}
        
            
                
                
            
        
        
            
                Proguard.config is enabled
                
                
            
        
    
          
        
        
        
        
        
        
        
        
        
        
        
        
        build.is.product:${build.is.product}
        ENVIRONMENT:${inter.env.ENVIRONMENT}
        
            
                
            
            
                
                
                
                
                
            
        
        
        
        
        
        
        
        
        
        
        
        
        
            
                *************************************************
                ****  Android Manifest has debuggable=true   ****
                **** Doing DEBUG packaging with RELEASE keys ****
                *************************************************
            
            
                
                
            
        
    
    
        
        
            
                
                
                Signing final apk...
                
                
                
                Release Package: ${out.final.file}
            
        
        
    
    
    
        
        
        
   
    
    
    
        
        
        
    
    
    
    
        
        
        
    
    
    
        
    
    
    
        
    
  
    
    
        
    
    
    
    
    
    
        
        
        
        
        
    
    
    
        
        
            
                
                Instrumented Package: ${out.final.file}
            
        
        
    
    
    
    
    
    
        
    
    
    
        
            
                
                    
                    
                
            
            
                
            
        
    
    
        
        
        
            
            
            
            
                
                    
                    
                
            
        
        
            
            
            
                
                    
                    
                    
                
            
        
        
        
        
            
                Running tests...
                
                    
                        
                           
                           
                    
                
                Setting permission to download the coverage file...
                
                    
                    
                    
                    
                    
                    
                    
                
                Downloading coverage file into project directory...
                
                    
                    
                    
                    
                
                
                    
                        
                        
                    
                
                Extracting coverage report...
                
                    
                    
                        
                        
                        
                        
                        
                        
                        
                   
                
                Cleaning up temporary files...
                
                
                
                    
                    
                    
                    
                    
                    
                
                Saving the coverage reports in ${out.absolute.dir}
            
            
                
            
        
    
    
    
    
    
        
    
    
    
    
    
        
        
            
                
                    
                
                
                    
                        
                            
                                
                            
                        
                        
                            Installing ${out.final.file} onto default emulator or device...
                            
                                
                                
                                
                                
                            
                            
                            
                            
                                
                                    
                                        
                                        
                                            
                                        
                                    
                                
                                
                                    
                                    
                                    
                                        
                                    
                                    
                                        
                                    
                                
                            
                        
                        
                            
                        
                    
                
                
                    Install file not specified.
                    
                    'ant install' now requires the build target to be specified as well.
                    
                    
                        ant debug install
                        ant release install
                        ant instrument install
                    This will build the given package and install it.
                    
                    Alternatively, you can use
                        ant installd
                        ant installr
                        ant installi
                        ant installt
                    to only install an existing package (this will not rebuild the package.)
                    
                
            
        
    
    
    
    
    
    
    
        
            
                
            
            
                
            
            
                
            
        
        
        
            
                
                    
                    
                        
                    
                
            
            
                
                
                
                
                    
                        
                    
                    
                        
                    
                    
                        
                    
                
            
        
    
    
    
    
    
    
    
        
        
        
        
        
        
        
        
        
        更新所有的package id
        
                    
        
        
    
    
    
        
        
        
        
        
        
        
        
        
        
        
    
    
    
    
    
        
          
            
          
          
            
          
          
          
          
          
          
          
          
        
    
 
  
    
    
    
        
            
        
    
    
    
        
            
            
            
                
                
                
                
                
                
            
        
    
    
    
    
    
        
            
        
    
    
    
        
            
            
            
                
                
                
                
                
                
            
        
       
 
    
    
    
    
        
        Android Ant Build. Available targets:
           help:      Displays this help.
           clean:     Removes output files created by other targets.
                      This calls the same target on all dependent projects.
                      Use 'ant nodeps clean' to only clean the local project
           debug:     Builds the application and signs it with a debug key.
                      The 'nodeps' target can be used to only build the
                      current project and ignore the libraries using:
                      'ant nodeps debug'
           release:   Builds the application. The generated apk file must be
                      signed before it is published.
                      The 'nodeps' target can be used to only build the
                      current project and ignore the libraries using:
                      'ant nodeps release'
           instrument:Builds an instrumented package and signs it with a
                      debug key.
           test:      Runs the tests. Project must be a test project and
                      must have been built. Typical usage would be:
                          ant [emma] debug install test
           emma:      Transiently enables code coverage for subsequent
                      targets.
           install:   Installs the newly build package. Must either be used
                      in conjunction with a build target (debug/release/
                      instrument) or with the proper suffix indicating
                      which package to install (see below).
                      If the application was previously installed, the
                      application is reinstalled if the signature matches.
           installd:  Installs (only) the debug package.
           installr:  Installs (only) the release package.
           installi:  Installs (only) the instrumented package.
           installt:  Installs (only) the test and tested packages (unless
                      nodeps is used as well.
           uninstall: Uninstalls the application from a running emulator or
                      device. Also uninstall tested package if applicable
                      unless 'nodeps' is used as well.
    

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

#ant releaseTest

打开发环境包,执行

#ant releaseDev

打正式发布包,执行

#ant releasePro

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

发表评论

电子邮件地址不会被公开。 必填项已用*标注