android多渠道打包最快方案

目前Android多渠道打包主要兩種方式:java

Gradle方式。

在build.gradle配置:python

productFlavors {
        huawei {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "huawei"]
        }
        xiaomi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
        }
        ...
    }複製代碼

渠道多的時候會致使gradle文件很長,若是想簡化書寫能夠這樣作:
在build.gradle中配置以下:android

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;...數組

當渠道不少的時候,每次打包的時間要幾十分鐘到幾個小時,那麼有沒有更快速的方式呢,請看第二種方式。app

META-INF方式。

使用此種方式能夠動態獲取當前渠道號,極大節省了打包時間。gradle

/** * 友盟配置 在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腳原本作這件事。ui

#!/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文件以下:spa

[Build-Config]
apk.path = your apk path
channel.prefix = channel-
channel.list = baidu,xiaomi複製代碼
相關文章
相關標籤/搜索