AndroidのBuild工具之Ant動手實踐

來源:互聯網
上載者:User

標籤:des   android   style   blog   http   io   ar   color   os   

好久沒有寫部落格了,沒半年也應該有幾個月了。在工作上的項目遇到過很多問題或者說積累了不少經驗,曾經都蠻想發到部落格留個紀念什麼的,不求可以為別人獲得點經驗技巧,只求在多年後遇到同樣的問題可以找到個記錄。但是,也許是懶終歸是懶,而且上班時間寫部落格有點不好吧(下班後就容易忘記)。

由於用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全部載入出來了

AndroidのBuild工具之Ant動手實踐

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.