背景:
1,、精細化運營需要
2、android studio flavor 打包效率低(每個分發包都要從頭構建)(100個渠道15分鐘。。)
方案:
獲得渠道號:
public static String getChannel(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/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 "xxx";
}
}
兩種方式:
1、用rar等壓縮軟件打開apk 在MET-INF中創建空文件夾命名爲channel_xxxx;
2、用Python腳本將空文件channel_xxxx寫入apk壓縮文件MET-INF中,可批量寫入導出,具體腳本參見Python
mport zipfile
zipped = zipfile.ZipFile(your_apk, 'a', zipfile.ZIP_DEFLATED)
empty_channel_file = "META-INF/mtchannel_{channel}".format(channel=your_channel)
zipped.write(your_empty_file, empty_channel_file)
以上兩種方法任何人都可以實現,不需要開發人員參與
啓發參考文獻:
http://tech.meituan.com/mt-apk-packaging.html