因爲目前渠道不少,須要批量打包,通常是window下打包,可是mac下如何批量打包呢?其實挺麻煩的,具體步驟以下:android
一.打包前準備工做app
1.首先肯定你的JDK版本爲1.6!ide
2.在AndroidManifest.xml中application標籤下添加一個用來識別渠道的標籤:ui
<meta-data android:name="qudao" android:value="channel" />this
3.爲了讓ant支持循環功能,咱們要在Android SDK/tools/lib下放一個ant-contrib-1.0b3.jar包spa
4.項目中放置第三方jar包的文件夾必須叫libs而不是libregexp
二.build.xml等文件的生成和配置orm
1.經過終端(cmd)命令自動生成build.xml和local.properties兩個文件,方法以下:xml
<sdk>/tools/android update project -p <project> -t <target>ip
例如:
/Users/moushou/Downloads/AndroidSDK/tools/android update project -p /Users/moushou/Documents/workspace/HelloWorld -t 14
其中<sdk>爲SDK全路徑,<project>爲項目全路徑,<target>爲API版本。
執行完成截圖以下:
執行完成後,Refresh你的項目就會發現項目的根目錄下多了兩個文件:build.xml和local.properties
其中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=/Users/moushou/Downloads/AndroidSDK
project.properties的內容以下:
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system use,
# "ant.properties", and override values to adapt the script to your
# project structure.
#proguard.config=proguard.cfg
# Project target.
target=Google Inc.:Google APIs:14
proguard.config=proguard.cfg
項目的目錄結構以下圖所示:
注:project.properties中target=GoogleInc.:GoogleAPIs:14表明所使用的SDK的版本,可進行手動修改。
2.手動爲項目新建一個File,該文件名爲:ant.properties,建立完成項目的目錄結構以下圖:
建立完成後在ant.properties中添加以下內容:
key.store=<keystore>
key.alias=<key>
key.store.password=<keystore pwd>
key.alias.password=<key pwd>
market_channels=xx,yy,zz
app_version=1_0_build_0
例如:
key.store=/Users/moushou/Desktop/qianming
key.alias=meilihuaduo
key.store.password=123456xx
key.alias.password=123456xx
market_channels=anzhuoshichang,jifengshichang,baiduyingyongzhongxin
app_version=1_0_build_0
其中:
keystore爲簽名文件的全路徑。
key.alias爲簽名須要使用的私鑰。
key.store.password爲私鑰庫的密碼。
key.alias.password爲私鑰的密碼。
market_channels爲渠道集合。
app_version爲apk的版本(此字段可根據本身喜愛編寫)。
--------------------------------------------------------------------------------------------------
至此,除build.xml外,其他文件配置完成。
三.build.xml的編寫方法:
1.修改build.xml的第二行,修改方法以下:
<project name="HelloWorld" default="release">
其中name爲你項目的名稱,default設置爲release。
2.循環替換AndroidManifest.xml中qudao的value值並進行自動簽名打包,方法以下:
1 <import file="${sdk.dir}/tools/ant/build.xml" />
2 <property name="out.unaligned.dir" value="/Users/moushou/Desktop/HelloWorld_${app_version}/" />
3 <mkdir dir="${out.unaligned.dir}" />
4 <target name="modify_update_file">
5 <echo>*********************** make channel ${channel}</echo>
6
7 <replaceregexp file="AndroidManifest.xml"
8 match='channel'
9 replace='${channel}'
10 byline="false"
11 encoding="utf-8"
12 />
13 <property name="out.unaligned.file" location="${out.unaligned.dir}/HelloWorld_${app_version}_${channel}_android.apk"/>
14
15 </target>
16
17 <target name="make_one_channels" depends="savemanifest,modify_update_file,release,replacemanifest,deletebin" description="description">
18 </target>
19
20 <target name="replacemanifest">
21 <echo>*********************** replacemanifest</echo>
22 <delete file="${basedir}\AndroidManifest.xml"/>
23 <copy file="..\temp\build\META-INF\AndroidManifest.xml" todir="${basedir}" encoding="utf-8"/>
24 </target>
25
26 <target name="savemanifest">
27 <echo>*********************** savemanifest</echo>
28 <copy file="${basedir}\AndroidManifest.xml" todir="..\temp\build\META-INF" encoding="utf-8" />
29 </target>
30
31 <target name="deletebin">
32 <delete dir="${basedir}\bin" />
33 </target>
34
35 <taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" classpath="/Users/moushou/Downloads/AndroidSDK/tools/lib/ant-contrib-1.0b3.jar" />
36 <target name="make_channels">
37 <foreach target="make_one_channels" list="${market_channels}" delimiter="," param="channel">
38 </foreach>
39 </target>
其中:
1.out.unaligned.dir的value值爲apk輸出文件夾的絕對路徑,文件夾採用HelloWorld結合app_version命名,app_version爲ant.properties中的app_version
2.out.unaligned.file的location爲apk最終的輸出路徑,apk命名採用HelloWorld加app_version加當前的channel加android方式
3.說一下打包的過程:
(1)第36行make_channels的target是ant的入口,該target中使用foreach循環調用名爲make_one_channels的target(第17行)並把market_channels集合中的每一個值替換給channel
(2)make_one_channels的target指定了每次打包的過程:
savemanifest:打包前先將原始的AndroidManifest.xml複製到與項目同一層級目錄下的temp下build下META-INF中
modify_update_file:匹配到AndroidManifest.xml中的channel並將其替換
release:自動編譯加簽名
replacemanifest:刪除AndroidManifest.xml,將temp/build/META-INF中的原始AndroidManifest.xml複製回項目根目錄下
deletebin:刪除bin文件(注:這步很重要,不然只能打出一個渠道的APK,當時作這塊的時候碰到的問題)
4.第35行taskdef標籤下的classpath是ant-contrib-1.0b3.jar的絕對路徑
四.打包方法的使用
打開終端(cmd),執行:
cd /Users/moushou/Documents/workspace/HelloWorld
而後執行:
ant make_channels
此時,打包就開始進行啦!當出現BUILD SUCCESSFUL表明打包成功!以下圖所示:
此時你會發現你輸出的文件夾中多了三個APK,以下圖:
注:1.每次打包前必定要刪除掉temp/build/META-INF中的AndroidManifest.xml,特別是在給不一樣項目作打包時
2.打包前請檢查AndroidManifest.xml中qudao的value值是否爲channel,特別是打包失敗後再次從新打包的時候必定要將value值改成channel
3.若是打包時出現Cannot recover key錯誤致使BUILD FAILD的話,請檢查ant.properties中key.alias.password的值後面是否有多餘的空格!有的話請把空格刪除掉!
五.在代碼中獲取渠道值,方法以下:
try {
ApplicationInfo appInfo = getPackageManager().getApplicationInfo
(getPackageName(),PackageManager.GET_META_DATA);
qudao = appInfo.metaData.getString("qudao");
} catch (NameNotFoundException e) {
e.printStackTrace();
}