成功:Android批量打包教程

1.配置JAVA的環境變量
   (參考http://jingyan.baidu.com/article/f96699bb8b38e0894e3c1bef.html),

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

2.  下載Ant(這裏的Ant不是eclipseandroid SDk裏面自帶的ant
     官方下載地址:http://ant.apache.org/

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能輕鬆解決這個問題.可以翻牆的同學可以到http://ant-contrib.sourceforge.net/自行下載,下載後直接把ant-contrib-1.0b3.jar放到Ant的lib文件夾即可.

5. 編寫build.xml
  將以下帶有顏色的字體(包括路徑,項目名稱)都改成正確的名稱
<?xml version="1.0" encoding="UTF-8"?>
<project name="ThumbPlay" 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" />

    <!-- 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:/androidDev/batch-package-tool/ant1.8.3/lib/ant-contrib-1.0b3.jar"/>  
 </classpath>
</taskdef>
<import file="${sdk.dir}/tools/ant/build.xml" />




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

<target name="modify_manifest">
<!--<replaceregexp file="AndroidManifest.xml" encoding="utf-8" match="android:value=&quot;(.*)&quot;" replace=""/>-->
   <replaceregexp flags="g" byline="false">  
   <regexp pattern="android:name=&quot;UMENG_CHANNEL&quot; android:value=&quot;(.*)&quot;" />  
   <substitution expression="android:name=&quot;UMENG_CHANNEL&quot; android:value=&quot;${channel}&quot;" />  
   <fileset dir="" includes="AndroidManifest.xml" />  
   </replaceregexp>
<!--<property  name="out.release.file" value="${out.absolute.dir}/${channel}.apk"/>-->
   <antcall target="release"/>
<copy tofile="${gos.path}/ThumbPlay_${channel}.apk">
    <fileset dir="${out.absolute.dir}/" includes="ThumbPlay-release.apk" />
</copy>
<delete includeEmptyDirs="true">  
   <fileset dir="${out.absolute.dir}" includes="**/*"/>           
</delete>
<echo message="==========================="/>
</target>
</project>

6. 配置local.properties
    sdk.dir=D:\\androidDev\\android-sdk 改成你的SDK所在的目錄,注意轉義字符

7. 配置ant.properties
# the config file for batch package.
application.package=com.leyou.thumb           (你的apk文件的包名)
ant.project.name=ThumbPlay                    (你的apk文件的工程名)
java.encoding=utf-8

out.absolute.dir=C:/compile
gos.path=Z:/app-version/test                             (打好的渠道包要放到的目的位置)

key.store=D:/androidApk/thumbplay/thumbplay.keystore     (keystore文件路徑)
key.store.password=wushenshiji999                        (keystore文件路徑)
key.alias=muzhigame                                      keystore文件別名)
key.alias.password=wushenshiji999                        keystore文件別名密碼)


app_version=1.0.4                                        (要打的渠道包的版本名稱)
market_channels=guanwang,shuihu,wushen,shenhua,huawei    (渠道名稱,要以逗號分隔,必須在一行內)

8. 最後一步,修改AndroidManifest.xml文件:

成功:Android批量打包教程
 
 以上這一行必須在同一行內,決不能換行,這是由於在build.xml做了如下限定

成功:Android批量打包教程

9.生成渠道包 
  a)進入工程根目錄,我的爲:D:\apps\workspace\ThumbPlay
     注意這裏必須要去工程根目錄,因爲Ant命令運行需要找到工程根目錄下的build.xml
    b)cmd輸入命令:ant deploy 第一次運行或許需要的時間要長些,我的爲大約2分50秒
    若控制檯最後出現Build Success,說明打包成功
    若控制檯最後出現Build Failed,查看詳細信息,找出錯誤所在,修改它,然後重新運行命令:ant deploy

備註:

再增加一些看到的優秀博文:

有些同志還將 jenkins + ant(jenkins就是一個計劃任務)聯合起來使用。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章