使用Python多渠道打包apk

2022-01-13 06:01:21 字數 2358 閱讀 7574

首先在info資料夾新建乙個qdb.txt的空文字檔案

新建channel.txt存放渠道**

執行python**即可將目錄下的apk生成多渠道檔案包

# 空檔案 便於寫入此空檔案到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':

# 獲取渠道列表

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_".format(channel = target_channel)

# 寫入渠道資訊

zipped.write(src_empty_file, empty_channel_file)

# 關閉zip流

zipped.close()

/** 獲取渠道id **/

public string getchannelid()

}} catch (ioexception e) finally catch (ioexception e) }}

string split = ret.split("_");

string channel = "";

if (split != null && split.length >= 2)

return channel;

}

新建乙個壓縮包

開啟壓縮包

在壓縮包內回退雙擊進入apk檔案即可

Android多渠道打包

度娘能搜到很多種多渠道打包方式,我這裡簡單說下我們目前正在使用的打包方法。首先背景情況 我們不同渠道,除了渠道號 vendorid 不一樣外,還有功能上的稍許不同,所以還有幾個開關控制專案。方法原理 專案 中在res raw 下增加config.dat檔案,裡面有渠道號,和功能開關 apk包,其實是...

Gradle多渠道打包

廢話不多說,以友盟統計為例,在androidmanifest.xml裡面會有這麼一段 meta data android name umeng channel android value channel id 裡面的channel id就是渠道標示。我們的目標就是在編譯的時候這個值能夠自動變化。或者...

android Ant 批量多渠道打包

支援多渠道打包 需要配置在androidmanifest.xml配置個 android name和android value位置不能變 這裡先要說下必須的 1 ant版本1.7以上 adt版本的eclipse裡的ant是1.8.3 ok eclipse plugins org.apache.ant ...