背景:
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