天天看點

Ant自動批量打包多管道APK

一、準備工作

1. 下載下傳安裝Ant     http://ant.apache.org/bindownload.cgi(官網下載下傳)

    下載下傳完成後解壓到指定目錄并将bin目錄配置到環境變量Path中

2. 下載下傳ant-contrib-1.0b3.jar     http://download.csdn.net/detail/tenggangren/5958967

    由于Ant自身不支援循環指令,需要下載下傳此擴充包來支援

二、在工程根目錄下建立并編寫build.xml、ant.properties、project.properties

    示例:

    build.xml代碼

<?xml version="1.0" encoding="UTF-8"?>
<project
    name="xuanshangbang"
    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" />
 
    <!--
         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 an env var"
        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>
 
    
    <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 -->
 
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" >
 
        <classpath>
 
            <pathelement location="D:/Ant/apache-ant-1.9.4/lib/ant-contrib-1.0b3.jar" /> <!--修改此處路徑 -->
        </classpath>
    </taskdef>
 
    <import file="${sdk.dir}/tools/ant/build.xml" /> <!--導入ant的build.xml用于編譯apk -->
 
    <target name="deploy" >
     <!--循環打包指令: market_channels(為ant.properties裡配置的管道号,用逗号隔開) -->
     <!--             param(為将上面取出來的一個個的管道号指派給channel參數          -->
     <!--             target(類似Makefile,執行目标modify_manifest                -->
        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>
 
    <target name="modify_manifest" >
 
        <!-- <replaceregexp file="AndroidManifest.xml" encoding="utf-8" match="android:value="(.*)"" replace=""/> -->
 
        <replaceregexp
            byline="false"
            flags="g" >
         <!-- 正則比對并替換 -->
         <!-- 在代碼中要用如下圖檔所示代碼替換 -->
            <regexp pattern="android:name="UMENG_CHANNEL" android:value="(.*)"" />  
   <substitution expression="android:name="UMENG_CHANNEL" android:value="${channel}"" />  
 
            <fileset
                dir=""
                includes="AndroidManifest.xml" />
        </replaceregexp>
        <!-- <property  name="out.release.file" value="${out.absolute.dir}/${channel}.apk"/> -->
 
        <antcall target="release" /> <!-- 調用上面導入的ant裡自帶的build.xml裡面的release目标 -->
 
        <copy tofile="${gos.path}/xuanshangbang_${app_version}_${channel}.apk" >
            <!-- 将每一次循環生成的apk包複制到指定目錄并用(包名_版本号_管道号)方式命名 -->
            <fileset
                dir="${out.absolute.dir}/"
                includes="xuanshangbang-release.apk" />
        </copy>
 
        <echo message="===========================" />
    </target>
 
</project>
           
注:拷貝build.xml到工程裡去的時候, 用此代碼替換上述注釋需要替換的代碼
Ant自動批量打包多管道APK
  ant.properties代碼
<pre name="code" class="plain"><pre name="code" class="plain">application.package=com.xunlei.xuanshangbang  
ant.project.name=xuanshangbang 
java.encoding=utf-8
 
out.absolute.dir=d:/xuanshangbang_apk_channels
gos.path=d:/xuanshangbang_apk_channels/apks
 
#配置簽名的路徑、密碼、别名、别名密碼
#注:千萬不要在值後面輸入額外的空格字元,否則會因為密碼不正确而報Build Failed!!!
key.store=D:/xuanshangbang_apk_channels/XuanShangBangKey
key.store.password=xljfqxsb
key.alias=xuanshangbang
key.alias.password=xljfqxsb
app_version=1.0.0.26
#配置管道号,以逗号隔開輸入,不要輸入回車符等額外的字元
market_channels=0x11100001,0x11100002,0x11100003,0x11100004,0x11100005,0x11100006,0x11100007,0x11100008,0x11100009,0x1110000A,0x1110000B,0x1110000C,0x1110000D,0x1110000E,0x1110000F,0x11100010,0x11100011,0x11100012,0x11100013,0x11100014,0x11100015,0x11100016,0x11100017,0x11100018,0x11100019,0x1110001A,0x1110001B,0x1110001C,0x1110001D
           

project.properties代碼

#在原來的project.properties最後面加上一句
#記得加上斜杠轉義符
sdk.dir=D:\\ADT_Bundle\\adt-bundle-windows-x86_64-20140702\\sdk
           

最後, cmd進入工程根目錄,執行ant deploy即可