【轉】Android項目使用Ant打包,生成build.xml


記不住,於是原帖轉過來,請看原帖:http://blog.csdn.net/ms03001620/article/details/8490238

 

一、生成build.xml

Eclipse中使用Ant為Android打包並且簽名

SDK自帶文件 在<sdk>tools/ant目錄下這3個文件

 

其中build.xml和uibuild.xml中定義了大量基礎構建方法和打包策略。我們只需要建立我們自己的build.xml並且存放在項目的根目錄下,然后引用一下<sdk>tools/ant/build.xml即可。當然也可以拷貝它然后直接操作。

 

那么如何。以項目AntForAndroid為例。生成一個針對該項目的build.xml呢?

這里需要一個工具。就是SDK自帶的android.bat .所在目錄<sdk>tools/android.bat

然后通過命令行執行“android update project --path .”即可。完畢后即可通過android命令在當前項目目錄下生成一個build.xml文件

 

新生成的3個文件 build.xml,local.properties,proguard-project.exe

二、配置ant.properties 

 

在項目根目錄新建一個ant.properties文件(不要去改變他的名字,因為<sdk>\tools\ant\build.xml會引用到這個名字)

並且在里面寫入一下數據:

#keystore文件的目錄,因為在根目錄下所以直接寫名字了

key.store=android.keystore

#這個名字就是在生成keystore時那個alias字段的值

key.alias=android

#兩個密碼分別寫建立keystore時的兩個密碼

key.store.password=(your pwd)

key.alias.password=(your pwd)

 

三、實現自動打簽名apk

右鍵點擊項目中的build.xml->run as... 先查看下有多少種任務,當然這些任務都是來自<sdk>\tools\ant\build.xml,項目里的只是引用了它

 

 

窗口打開

 

默認的選擇在help上。打簽名包的時候執行release這個任務即可。

 

按照以上的步驟配置好之后就能生成簽名apk。並且大家可以通過學習<sdk>\tools\ant\build.xml來理解ant的基本奧妙。

最后在項目跟目錄\bin下生成apk包

 

Ant是非常強大的。可以實現一下功能

例如

1.在生成apk的文件名里加上日期。

2.把apk文件自動通過郵件發給其他人。

3.apk文件上傳服務器

等等

 

第二篇:http://www.xmumu.com/post/2011-11-22/7022215

通過Android命令自動編譯出build.xml文件

用途: 做自動集成編譯環境.

環境搭配我就不說了(SDK/環境變量等).
進入cmd界面.
進入項目目錄.


輸入: android.bat list target  來查詢我們現有的版本list有哪些.



記住這個ID號碼,下的代碼面會用到.
輸入: android update project -n ButtonDemo -t 1 -p E:\workspace\android\ButtonDemo

-n 對應的是項目名稱
-t 就是我們之前查詢的SDK版本對應的ID,大家根據自己的項目版本做出選擇即可,我這個是android-8 所以用ID 1 .
-p就是生成的路徑
成功后顯示:


好了以下是生成的build.xml代碼段:

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

<project name="ButtonDemo" 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. -->

    <loadproperties srcFile="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" />

 

    <!-- 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'"

            unless="sdk.dir"

    />

 

 

<!-- extension targets. Uncomment the ones where you want to do custom work

     in between standard targets -->

<!--

    <target name="-pre-build">

    </target>

    <target name="-pre-compile">

    </target>

 

    /* 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} */

    <target name="-post-compile">

    </target>

-->

 

    <!-- 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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM