目前Android多渠道打包主要兩種方式:
Gradle方式。
在build.gradle配置:
productFlavors {
huawei {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "huawei"]
}
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
...
}複製程式碼
渠道多的時候會導致gradle檔案很長,如果想簡化書寫可以這樣做:
在build.gradle中配置如下:
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
...
android{
...
productFlavors {
def channelArray = properties.getProperty("channel").split(";");
for (int i = 0; i < channelArray.size(); i++) {
def channel = channelArray[i]
"${channel}" {
manifestPlaceholders = [CHANNEL_VALUE: channel]
}
}
}
...
}複製程式碼
local.properties中新增所有渠道號:channel=wandoujia;_360;yingyongbao;xiaomi;baidu;huawei;...
當渠道很多的時候,每次打包的時間要幾十分鐘到幾個小時,那麼有沒有更快速的方式呢,請看第二種方式。
META-INF方式。
使用此種方式可以動態獲取當前渠道號,極大節省了打包時間。
/**
* 友盟配置
在application onCreate方法呼叫
*/
private void umengConfig() {
String channel = getChannelFromApk(application, "channel-");
if (TextUtils.isEmpty(channel)) {
channel = "default";
}
if (BuildConfig.IS_DEBUG) {
Toast.makeText(application, "當前渠道:" + channel, Toast.LENGTH_SHORT).show();
}
AnalyticsConfig.setChannel(channel);
}
/**
* 從apk中獲取版本資訊
* @param context
* @param channelPrefix 渠道字首
* @return
*/
public static String getChannelFromApk(Context context, String channelPrefix) {
//從apk包中獲取
ApplicationInfo appinfo = context.getApplicationInfo();
String sourceDir = appinfo.sourceDir;
//預設放在meta-inf/裡, 所以需要再拼接一下
String key = "META-INF/" + channelPrefix;
String ret = "";
ZipFile zipfile = null;
try {
zipfile = new ZipFile(sourceDir);
Enumeration<?> entries = zipfile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
String entryName = entry.getName();
if (entryName.startsWith(key)) {
ret = entryName;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String[] split = ret.split(channelPrefix);
String channel = "";
if (split != null && split.length >= 2) {
channel = ret.substring(key.length());
}
return channel;
}複製程式碼
每次動態獲取應用包下meta-inf資料夾中的渠道檔案取得渠道號,那麼meta-inf下的渠道檔案如何新增呢?我們可以使用python指令碼來做這件事。
#!/usr/bin/python
# coding=utf-8
import zipfile
import shutil
import os
import ConfigParser
#讀取配置檔案
config = ConfigParser.ConfigParser()
config.read("channels-config.ini")
#apk路徑
apk_path = config.get("Build-Config", "apk.path")
print "src apk path=" + apk_path
#渠道識別字首
channel_prefix = config.get("Build-Config", "channel.prefix")
print "channel prefix=" + channel_prefix
#渠道列表
channel_list = config.get("Build-Config", "channel.list")
print "channel list=" + channel_list
#解析渠道,生成渠道陣列
channel_array = channel_list.split(',')
# 空檔案 便於寫入此空檔案到apk包中作為channel檔案
src_temp_file = 'temp_.txt'
# 建立一個空檔案(不存在則建立)
f = open(src_temp_file, 'w')
f.close()
src_apk = apk_path
# file name (with extension)
src_apk_file_name = os.path.basename(src_apk)
# 分割檔名與字尾
temp_list = os.path.splitext(src_apk_file_name)
# name without extension
src_apk_name = temp_list[0]
# 字尾名,包含. 例如: ".apk "
src_apk_extension = temp_list[1]
# 建立生成目錄,與檔名相關
output_dir = 'apks_' + src_apk_name + '/'
# 目錄不存在則建立
if not os.path.exists(output_dir):
os.mkdir(output_dir)
# 遍歷渠道號並建立對應渠道號的apk檔案
for line in channel_array:
# 獲取當前渠道號,因為從渠道檔案中獲得帶有\n,所有strip一下
target_channel = line.strip()
# 拼接對應渠道號的apk
target_apk = output_dir + src_apk_name + "-" + target_channel + src_apk_extension
# 拷貝建立新apk
shutil.copy(src_apk, target_apk)
# zip獲取新建立的apk檔案
zipped = zipfile.ZipFile(target_apk, 'a', zipfile.ZIP_DEFLATED)
# 初始化渠道資訊
target_channel_file = "META-INF/" + channel_prefix + "{channel}".format(channel = target_channel)
# 寫入渠道資訊
zipped.write(src_temp_file, target_channel_file)
# 關閉zip流
zipped.close()
#刪除臨時檔案
os.remove(src_temp_file)複製程式碼
channels-config.ini檔案如下:
[Build-Config]
apk.path = your apk path
channel.prefix = channel-
channel.list = baidu,xiaomi複製程式碼