ant工具-多渠道自動打包android項目

(一)ant介紹

ant是自動化拷貝、編譯、發佈的構建工具,簡單跨平臺。


(二)ant使用前奏

1.安裝jdk並配合環境變量

2.安裝sdk並配合環境變量

3.新版的android sdk已經自帶了ant在/eclipse/plugins目錄下,如需下載到http://ant.apache.org,新建環境變量ANT_HOME爲ant目錄,path爲%ANT_HOME%/lib


(三)編譯發佈android項目

1.生成build.xml文件

運行android update project -p xxx     (xxx爲項目路徑)

在項目根目錄下自動生成build.xml、local.properties兩個文件

build.xml已做基本配置和調用sdk本身自帶的build.xml文件,local.properties已設置sdk目錄

2.打包項目

新建ant.properties,放入keystore簽名信息

key.store=zhangzhongcai.keystore
key.store.password=xxx
key.alias=xxx
key.alias.password=xxx

根目錄下放入zhangzhongcai.keystore簽名文件

若不指定key.store.password和key.alias.password會在運行過程中要求輸入,提高安全性

運行ant release打包


(四)多渠道自動化打包

1.多渠道打包需要修改配置文件,完成渠道信息修改再打包,ant本身沒有for命令,修改xml文件也不方便,需要使用第三方擴展包Ant-contrib,從這裏可以下載到,下載完在項目中新建lib文件夾放入。

2.新建channel.properties加入渠道信息

<span style="font-size:18px;">market_channels=anzhi,360,baidu</span>

3.修改build.xml支持多渠道自動化打包,有兩種方式

(1)添加custom_rules.xml,將自動化打包腳本寫入,打包前初始化會調用custom_rules.xml內容

(2)將自動化打包腳本寫入build.xml,如下

<?xml version="1.0" encoding="UTF-8"?>
<project name="TestRelease" default="deploy">

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

	<property file="channel.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"
    -->

	<!--渠道包打包腳本  ant deploy   --> 
	<!--讀入AndroidManifest.xml信息,方便獲取軟件版本號   --> 
	<xmlproperty file="AndroidManifest.xml" prefix="appinf" collapseAttributes="true"/>
	
	<taskdef resource="net/sf/antcontrib/antcontrib.properties">  
	    <classpath>  
	        <pathelement location="./lib/ant-contrib-1.0b3.jar"/>  
	    </classpath>  
	</taskdef>  
	  
	<target name="deploy">  
	   <foreach target="modify_manifest" list="${market_channels}" param="channel" delimiter=",">       
	   </foreach>  
	</target>  
	
	<target name="modify_manifest">  
	    <replaceregexp flags="g" byline="false">  
	        <!--匹配的內容是 android:value="*****" android:name="UMENG_CHANNEL"-->
	        <regexp pattern='android:name="UMENG_CHANNEL"
            android:value="(.*)"' />  
	        <!--匹配之後將其替換爲 android:value="渠道名" android:name="UMENG_CHANNEL"  -->
	        <substitution expression='android:value="${channel}" android:name="UMENG_CHANNEL"' /> 
	        <!--正則表達式需要匹配的文件爲AndroidManifest.xml  -->
	         <fileset dir="" includes="AndroidManifest.xml" />  
	    </replaceregexp>  
	    <property name="out.release.file" location="${out.absolute.dir}/${ant.project.name}_${channel}.apk" />  
	     <!--打包  -->
	     <antcall target="release" />  
	     <!--輸出渠道包到bin/out目錄下  -->
	    <copy tofile="${out.absolute.dir}/out/${ant.project.name}V${appinf.manifest.android:versionName}_${channel}_release.apk" file="bin/${ant.project.name}-release.apk"/>  
	</target>

    <!-- version-tag: 1 -->
    <import file="${sdk.dir}/tools/ant/build.xml" />

</project>

在該目錄下運行ant命令,就可以打包出各個渠道的版本到bin/out目錄中


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章