上一篇《Android多渠道打包沒你想的那么復(fù)雜》中說了用gradle方法進(jìn)行多渠道打包虱咧,這個方法簡單也好理解豪椿,但是速度不敢恭維篮昧。今天跟大家說說用Python的方法來打包壳坪,那感覺...
1437707079_452x562.png
1.添加渠道
在info
目錄下的channel.txt
中添加要上傳的渠道名。一定以回車分隔莺戒,就是每個名字占一行伴嗡。
2.Python腳本
其實方法來自于美團。就是將apk解壓脏毯,在META-INF
目錄下添加一個以渠道名channel_渠道名
為前綴的空文件闹究。如圖:
創(chuàng)建一個名為
empty.txt
的空文件
# 空文件 便于寫入此空文件到apk包中作為channel文件
src_empty_file = 'info/empty.txt'
# 創(chuàng)建一個空文件(不存在則創(chuàng)建)
f = open(src_empty_file, 'w')
f.close()
獲取目錄下所有的apk(所以,可以同時多個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]
# 創(chuàng)建生成目錄,與文件名相關(guān)
output_dir = 'output_' + src_apk_name + '/'
# 目錄不存在則創(chuàng)建
if not os.path.exists(output_dir):
os.mkdir(output_dir)
# 創(chuàng)建臨時目錄,與文件名相關(guān)
temp_dir = 'temp' + src_apk_name + '/'
# 目錄不存在則創(chuàng)建
if not os.path.exists(temp_dir):
os.mkdir(temp_dir)
out_temp_apk = temp_dir + src_apk_name+'temp'+src_apk_extension;
#copy一份沒有渠道信息的apk
zin = zipfile.ZipFile (src_apk, 'a', zipfile.ZIP_DEFLATED)
zout = zipfile.ZipFile (out_temp_apk, 'w')
for item in zin.infolist():
buffer = zin.read(item.filename)
if (item.filename[0:16:] != 'META-INF/channel'):
zout.writestr(item, buffer)
zout.close()
zin.close()
# 遍歷渠道號并創(chuàng)建對應(yīng)渠道號的apk文件
for line in lines:
# 獲取當(dāng)前渠道號吉嫩,因為從渠道文件中獲得帶有\(zhòng)n,所有strip一下
target_channel = line.strip()
# 拼接對應(yīng)渠道號的apk
target_apk = output_dir + src_apk_name + "_" + target_channel + src_apk_extension
# 拷貝建立新apk
shutil.copy(out_temp_apk, target_apk)
# zip獲取新建立的apk文件
zipped = zipfile.ZipFile(target_apk, 'a', zipfile.ZIP_DEFLATED)
# 初始化渠道信息
empty_channel_file = "META-INF/channel_{channel}".format(channel=target_channel)
for item in zipped.infolist():
if (item.filename[0:16:] == 'META-INF/channel'):
print (item.filename)
# 寫入渠道信息
zipped.write(src_empty_file, empty_channel_file)
# 關(guān)閉zip流
zipped.close()
3.第三方統(tǒng)計平臺設(shè)置對應(yīng)渠道名
美團有說過友盟的設(shè)置方法价认,由于我用的是騰訊分析,我在這里就說下騰訊的自娩,在MainActivity
的onCreate
方法里添加:
StatConfig.setInstallChannel(ChannelUtils.getChannel(this));
ChannelUtils.java文件:
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.util.Log;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* 渠道工具類
* Created by shd on 2016/6/28.
*/
public class ChannelUtils {
public static final String Version = "version";
public static final String Channel = "channel";
public static final String DEFAULT_CHANNEL = "yunxiao";
public static final String Channel_File = "channel";
public static String getChannelFromMeta(Context context) {
ApplicationInfo appinfo = context.getApplicationInfo();
String sourceDir = appinfo.sourceDir;
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("META-INF") && entryName.contains("channel_")) {
ret = entryName;
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
String[] split = ret.split("_");
if (split != null && split.length >= 2) {
return ret.substring(split[0].length() + 1);
} else {
return DEFAULT_CHANNEL;
}
}
/**
* 得到渠道名
*
* @param mContext
* @return
*/
public static String getChannel(Context mContext) {
String channel = "";
if (isNewVersion(mContext)) {//是新版本
Log.e("isNewVersion %s", "isNewVersion");
saveChannel(mContext);//保存當(dāng)前版本
channel = getChannelFromMeta(mContext);
} else {
channel = getCachChannel(mContext);
}
return channel;
}
/**
* 保存當(dāng)前的版本號和渠道名
*
* @param mContext
*/
public static void saveChannel(Context mContext) {
SharedPreferences mSettinsSP = mContext.getSharedPreferences(Channel_File, Activity.MODE_PRIVATE);
SharedPreferences.Editor mSettinsEd = mSettinsSP.edit();
mSettinsEd.putString(Version, PhoneInformationUtils.getAppVersionName(mContext));
mSettinsEd.putString(Channel, getChannelFromMeta(mContext));
//提交保存
mSettinsEd.commit();
}
private static boolean isNewVersion(Context mContext) {
SharedPreferences mSettinsSP = mContext.getSharedPreferences(Channel_File, Activity.MODE_PRIVATE);
String version = PhoneInformationUtils.getAppVersionName(mContext);
Log.e("version%s", version);
return !mSettinsSP.getString(Version, "").equals(version);
}
private static String getCachChannel(Context mContext) {
SharedPreferences mSettinsSP = mContext.getSharedPreferences(Channel_File, Activity.MODE_PRIVATE);
return mSettinsSP.getString(Channel, DEFAULT_CHANNEL);
}
}
源代碼在Github上用踩,需要的可以拿走:https://github.com/shd188/MultiChannel