[关闭]
@wangwangheng 2015-06-10T10:23:02.000000Z 字数 2012 阅读 1876

批量打包

公司文档


在此输入正文

1. 使用方式

1.1 按照正常流程打包APK

具体的打包规范可以参考SVN/doc下的打包规范

1.2 修改渠道文件channels.txt

SVN路径:https://org2012.oicp.net/svn/doc/branches/android/technology/packages_apk

文件内的每一行一个渠道号

例子:

  1. 360
  2. xiaomi
  3. baidu
  4. 91
  5. guanwang
  6. offline
  7. tencent
  8. wandoujia

1.3 SVN上下载批量打包Python脚本

SVN路径:https://org2012.oicp.net/svn/doc/branches/android/technology/packages_apk

机器上一定要安装python,建议安装2.7.*版本

1.4 执行python脚本

  1. python package.py APK文件名 输出文件夹名

例子:

  1. python package.py guamu-2015-05-19.apk out

2 原理

  1. import zipfile
  2. import shutil
  3. import sys
  4. import os
  5. apk_path = sys.argv[1]
  6. out_path = sys.argv[2]
  7. if not os.path.exists(out_path):
  8. os.makedirs(out_path)
  9. name = os.path.basename(apk_path)
  10. channels_file = open('channels.txt')
  11. origin_apk_name = os.path.splitext(name)[0]
  12. for channel in channels_file:
  13. channel_apk_name = "{}_{}.apk".format(origin_apk_name, channel.strip())
  14. channel_apk_path = os.path.join(out_path, channel_apk_name)
  15. shutil.copy2(apk_path, channel_apk_path)
  16. zipped = zipfile.ZipFile(channel_apk_path, 'a', zipfile.ZIP_DEFLATED)
  17. empty_channel_file = "META-INF/hengeasy_{}".format(channel.strip())
  18. zipped.writestr(empty_channel_file, '')
  19. zipped.close()
  1. public static String getMetaInfChannel(Context context) {
  2. ApplicationInfo appinfo = context.getApplicationInfo();
  3. String sourceDir = appinfo.sourceDir;
  4. String ret = "";
  5. ZipFile zipfile = null;
  6. try {
  7. zipfile = new ZipFile(sourceDir);
  8. Enumeration<?> entries = zipfile.entries();
  9. while (entries.hasMoreElements()) {
  10. ZipEntry entry = ((ZipEntry) entries.nextElement());
  11. String entryName = entry.getName();
  12. //如果想修改此标示,直接编辑pack.py即可
  13. if (entryName.startsWith("META-INF/hengeasy")) {
  14. ret = entryName;
  15. break;
  16. }
  17. }
  18. } catch (IOException e) {
  19. e.printStackTrace();
  20. } finally {
  21. if (zipfile != null) {
  22. try {
  23. zipfile.close();
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. }
  29. String[] split = ret.split("_");
  30. if (split != null && split.length >= 2) {
  31. return ret.substring(split[0].length() + 1);
  32. } else {
  33. return "";
  34. }
  35. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注