根據(jù)美團(tuán)打包修改的一鍵多名稱app打包

  1. 通過productFlavors打包不同資源文件,以改變應(yīng)用名稱等
  2. 通過variant.outputs設(shè)置打包路徑,移動(dòng)apk到py批量打包的目錄
productFlavors {
        production {}
        longname {}
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.release
            //minifyEnabled true
            proguardFiles 'proguard-project.txt'
        }
        release {
            signingConfig signingConfigs.release

            minifyEnabled true
            proguardFiles 'proguard-project.txt'
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    if (output.outputFile != null && output.outputFile.name.endsWith('.apk')
                            && 'release'.equals(variant.buildType.name)) {
                        def apkFile = new File(
                                output.outputFile.getParent()+"/makeapk",
                                "qichechaoren_v${variant.versionName}_${variant.productFlavors[0].name}.apk")
                        output.outputFile = apkFile
                    }
                }
            }
        }
    }
  1. 通過py腳本根據(jù)info里不同文件里的渠道號(hào)對(duì)應(yīng)批量打包不同資源的apk
#!/usr/bin/python
# coding=utf-8
import zipfile
import shutil
import os

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

# 獲取當(dāng)前目錄中所有的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_name = '_production'
channel_long_name = '_longname'
channel_file = 'info/channel.txt'
f = open(channel_file)
lines = f.readlines()
f.close()

channel_file2 = 'info/longname.txt'
f2 = open(channel_file2)
lines_longname = f2.readlines()
f2.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]

    # 創(chuàng)建生成目錄,與文件名相關(guān)
    output_dir = 'output_' + src_apk_name + '/'
    # 目錄不存在則創(chuàng)建
    if not os.path.exists(output_dir):
        os.mkdir(output_dir)

    if channel_name in src_apk_name:
        # 遍歷渠道號(hào)并創(chuàng)建對(duì)應(yīng)渠道號(hào)的apk文件
        for line in lines:
            # 獲取當(dāng)前渠道號(hào),因?yàn)閺那牢募蝎@得帶有\(zhòng)n,所有strip一下
            target_channel = line.strip()
            # 拼接對(duì)應(yīng)渠道號(hào)的apk
            apkname = src_apk_name.split(channel_name)
            target_apk = output_dir + apkname[0] + "_" + 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/cztchannel_{channel}".format(channel = target_channel)
            # 寫入渠道信息
            zipped.write(src_empty_file, empty_channel_file)
            # 關(guān)閉zip流
            zipped.close()
    if channel_long_name in src_apk_name:
        # 遍歷渠道號(hào)并創(chuàng)建對(duì)應(yīng)渠道號(hào)的apk文件
        for line in lines_longname:
            # 獲取當(dāng)前渠道號(hào),因?yàn)閺那牢募蝎@得帶有\(zhòng)n,所有strip一下
            target_channel = line.strip()
            # 拼接對(duì)應(yīng)渠道號(hào)的apk
            apkname = src_apk_name.split(channel_long_name)
            target_apk = output_dir + apkname[0] + "_" + 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/cztchannel_{channel}".format(channel=target_channel)
            # 寫入渠道信息
            zipped.write(src_empty_file, empty_channel_file)
            # 關(guān)閉zip流
            zipped.close()

此為blog備份,原地址:http://blog.yzapp.cn/markapk.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,940評(píng)論 25 709
  • 這一章主要針對(duì)項(xiàng)目中可以用到的一些實(shí)用功能來介紹Android Gradle,比如如何隱藏我們的證書文件,降低風(fēng)險(xiǎn)...
    acc8226閱讀 7,966評(píng)論 3 25
  • 一、什么是多渠道 我理解的是:APP發(fā)布到不同的應(yīng)用平臺(tái),監(jiān)測(cè)用戶是從哪個(gè)平臺(tái)安裝的。 二、為什么要多渠道打包 1...
    LucasAdam閱讀 1,033評(píng)論 0 1
  • 目錄一、Python打包及優(yōu)化(美團(tuán)多渠道打包)二、Gradle打包三、其他打包方案:修改Zip文件的commen...
    守望君閱讀 5,930評(píng)論 4 17
  • 今天,二輪終于過完了第一遍,計(jì)劃在二十天內(nèi)把二輪再過一遍,再把分值大的題型在另外一本書里面再過一遍。下階段...
    忽爾今至閱讀 202評(píng)論 0 0

友情鏈接更多精彩內(nèi)容