解壓apk,解壓後的根目錄會有一個META-INF目錄。如果在META-INF目錄内添加空檔案,可以不用重新簽名應用。是以,通過為不同管道的應用添加不同的空檔案,可以唯一辨別一個管道。
下面代碼可以實作往apk中的META-INF目錄下添加一個空檔案zipped = zipfile.ZipFile([apk絕對路徑], 'a', zipfile.ZIP_DEFLATED)
empty_channel_file = "META-INF/[CHANNEL_PREFIX]{channel}".format(channel=[管道名])
zipped.write([源空檔案], empty_channel_file)
zipped.close()
下面代碼為Java擷取管道号代碼public static String getChannel() {
ApplicationInfo appinfo = StudyApplication.getContext().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();
int index = entryName.lastIndexOf(BuildConfig.CHANNEL_PREFIX);
if (index > 0) {
return entryName.substring(index + BuildConfig.CHANNEL_PREFIX.length());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";
}
這樣,每打一個管道包隻需複制一個apk,在META-INF中添加一個使用管道号命名的空檔案即可。這種打包方式速度非常快,900多個管道不到一分鐘就能打完。