使用Python多渠道打包apk


使用Python生成多渠道包

往apk包中追加到一個空文件到META-INF目錄以標識渠道,Android中獲取此文件即可獲得App的下載渠道

  1. 首先在info文件夾新建一個qdb.txt的空文本文件
  2. 新建channel.txt存放渠道來源
  3. 運行Python代碼即可將目錄下的apk生成多渠道文件包

Python代碼如下:

    #!/usr/bin/python
    # coding=utf-8
    import zipfile
    import shutil
    import os

    # 空文件 便於寫入此空文件到apk包中作為channel文件
    src_empty_file = 'info/qdb.txt'
    # 創建一個空文件(不存在則創建)
    f = open(src_empty_file, 'w') 
    f.close()

    # 獲取當前目錄中所有的apk源包
    src_apks = []
    # python3 : os.listdir()即可,這里使用兼容Python2的os.listdir('.')
    for file in os.listdir('.'):
        if os.path.isfile(file):
            extension = os.path.splitext(file)[1][1:]
            if extension in 'apk':
                src_apks.append(file)

    # 獲取渠道列表
    channel_file = 'info/channel.txt'
    f = open(channel_file)
    lines = f.readlines()
    f.close()

    for src_apk in src_apks:
        # 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 = 'output_' + src_apk_name + '/'
        # 目錄不存在則創建
        if not os.path.exists(output_dir):
            os.mkdir(output_dir)
            
        # 遍歷渠道號並創建對應渠道號的apk文件
        for line in lines:
            # 獲取當前渠道號,因為從渠道文件中獲得帶有\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)
            # 初始化渠道信息
            empty_channel_file = "META-INF/qdb_{channel}".format(channel = target_channel)
            # 寫入渠道信息
            zipped.write(src_empty_file, empty_channel_file)
            # 關閉zip流
            zipped.close()

Android中讀取文件取得渠道ID

    /** 獲取渠道ID **/
        public String getChannelId() {
            Context context = cordova.getActivity().getApplicationContext();
            //從apk包中獲取
            ApplicationInfo appinfo = context.getApplicationInfo();
            String sourceDir = appinfo.sourceDir;
            //默認放在meta-inf/里, 所以需要再拼接一下
            String key = "META-INF/qdbchannel";
            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("_");
            String channel = "";
            if (split != null && split.length >= 2) {
                channel = ret.substring(split[0].length() + 1);
            }
            return channel;
        }

附上查看apk內文件方法

  1. 新建一個壓縮包
  2. 打開壓縮包
  3. 在壓縮包內回退雙擊進入apk文件即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM