Android使用Ant批量打包

 

                        Android使用Ant批量打包

1.配置java環境變量

   很多Java程序員由於使用Eclipse不配置Java環境變量也能正常運行代碼。但是如果想使用Ant命令批量打包本步驟必不可少。

2.下載Ant(這裏的Ant不是Eclipse和Android SDK 裏面自帶的ant)

      官方下載網址:http://ant.apache.org/

  下載.zip即可

3.解壓Ant並配置環境變量(注意是系統變量,不是用戶變量)  

  a) 解壓Ant,比如解壓到D:\ant
  b) 我的電腦->屬性->高級->環境變量
  c) 系統變量新建ANT_HOME,變量值爲d:\ant
  d) 系統變量新建或修改PATH:將%ANT_HOME%\bin;%ANT_HOME%\lib添加到環境變量的PATH中 (注意以上
路徑均用反斜槓)

4.驗證Ant配置是否正確

  在控制檯輸入cmd回車, ant 回車,如果出現:
   Buildfile: build.xml does not exist!
   Build failed
 恭喜你已經ant配置成功!!

===============================================================================================

第二階段:

   Ant批量打包的基本思想是,每次打包後自動替換渠道號,然後再次打包從而實現多渠道打包的目的。
但是Ant不支持循環,怎樣循環打包? 擴展包Ant-contrib能輕鬆解決這個問題。下載ant-contrib-1.0b3.jar放到Ant的lib文件夾即可。直接百度搜索ant-contrib-1.0b3.jar。

  Ant批量打包中比較重要的三個文件:build.xml,ant.properties,local.properties

      這三個文件要放在Android工程根目錄下。

1.build.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- 項目名稱XXXX,可用全局替換爲當前項目名稱 -->
<project
    name="XXXX"
    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/lib/ant-contrib-1.0b3.jar</span>" />
        </classpath>
    </taskdef>

    <import file="${sdk.dir}/tools/ant/build.xml" />

    <target name="deploy" >

        <foreach
            delimiter=","
            list="${market_channels}"
            param="channel"
            target="modify_manifest" >
        </foreach>
    </target>

    <target name="modify_manifest" >
        <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>
        
        <antcall target="release" />

        <copy tofile="${gos.path}/XXXX_${channel}.apk" >

            <fileset
                dir="${out.absolute.dir}/"
                includes="XXXX-release.apk" />
        </copy>

        <delete includeEmptyDirs="true" >

            <fileset
                dir="${out.absolute.dir}"
                includes="**/*" />
        </delete>

        <echo message="=======Build Success!!!====================" />
    </target>

</project></span>


解釋下:紅色標註部分以自己的Ant安裝路徑爲準!

2.ant.properties

application.package= //項目包名
ant.project.name=//項目名(同build.xml)
java.encoding=UTF-8 //項目編碼

out.absolute.dir=d:/apk/compile //打包時生成臨時文件存放地,最終build會自己清空
gos.path=d:/apk //最終apk存放文件夾

key.store=//簽名文件位置
key.store.password=//簽名密碼

key.alias=//簽名別名(生成keystore時的單位名)

key.alias.password=//別名密碼

app_version=//apk版本

market_channels=huawei,Google,Gfan,AnZhi,MuMayi //apk渠道(逗號分隔 )

掃盲一下:如何生成keystore

偷一個懶用Eclipse IDE來生成keystore,當然你也可以用命令行生成,自己隨意吧。

2.1在Eclipse中右鍵工程

2.2選擇Export Sign Application Package...點擊Next

2.3選擇create new keystore,Browser選擇你要生成的key的路徑

設置密碼,點擊Next

2.4 填寫你的Alias 別名和密碼 點擊Next生成keystore

對應ant.properties中的各個字段,仔細二認真的核對吧

3.local.properties

 

# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must *NOT* be checked into Version Control Systems,
# as it contains information specific to your local configuration.

# location of the SDK. This is only used by Ant
# For customization when using a Version Control System, please read the
# header note.

sdk.dir=D:\\adt-bundle-windows\\sdk

 

解釋一下:紅色標註部分填寫你自己的sdk目錄

4.最後一步修改AnroidManifest.xml文件

 <meta-data
            android:name="UMENG_APPKEY"
            android:value="53bf9d4556240b753100a99d" />
 <meta-data android:name="UMENG_CHANNEL" android:value="MuMayi"/>


紅色標註部分必須在一行

以上這一行必須在同一行內,決不能換行,這是由於在build.xml做了如下限定

<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></span>


 

AnroidManifest.xml文件中如果有<!-- -->中文的註釋,建議你刪除AnroidManifest.xml中的註釋,或者換成英文註釋,否則你循環打包時,當打完一個包後拋出UTF-8編碼錯誤。原因就是因爲註釋的原因。切記!

5.命令行下,cd到自己要打包的Android工程的目錄,ant deploy,觀察cmd命令行打印信息,有錯誤調試後再次運行該命令。如果批量打包成功後會打印出build success!!!

6.當你打包好後,在用反編譯工具,AndroidBy或者ApkTool等反編譯工具查看打包後的AnroidManifest.xml文件中的渠道值是否變化?如果變化了再將打包後的APK安裝在手機上看是否能正常安裝?,如果都是肯定的答案,那麼恭喜你,你成功了!!!

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