├── README.md └── package.py /README.md: -------------------------------------------------------------------------------- 1 | # 打包方法 2 | 3 | 因为Android在安装apk时,不对META-INF文件夹的文件经行签名校验, 4 | 5 | 所以可以在这个文件夹随添加/修改相关文件作为渠道标识 6 | 7 | 8 | python ./package.py xxxxx.apk dev 9 | 10 | 11 | 12 | 最后一个参数是想要的渠道名称 13 | 14 | 15 | # 读取渠道包 16 | 17 | 18 | 19 | 20 | 21 | public static String getCustomChannelInfo(Context context){ 22 | 23 | if(!TextUtils.isEmpty(mChannel)){ 24 | return mChannel; 25 | } 26 | 27 | mChannel = "default"; 28 | 29 | ApplicationInfo appinfo = context.getApplicationInfo(); 30 | String sourceDir = appinfo.sourceDir; 31 | Log.d("getChannel sourceDir", sourceDir); 32 | 33 | ZipFile zf = null; 34 | InputStream in = null; 35 | ZipInputStream zin = null; 36 | 37 | try { 38 | zf = new ZipFile(sourceDir); 39 | 40 | in = new BufferedInputStream(new FileInputStream(sourceDir)); 41 | zin = new ZipInputStream(in); 42 | 43 | ZipEntry ze; 44 | Enumeration entries = zf.entries(); 45 | 46 | while (entries.hasMoreElements()) { 47 | ZipEntry entry = ((ZipEntry) entries.nextElement()); 48 | Log.d("getChannel getName", entry.getName()); 49 | if( entry.getName().equalsIgnoreCase("META-INF/channel_info")){ 50 | long size = entry.getSize(); 51 | if (size > 0) { 52 | BufferedReader br = new BufferedReader( new InputStreamReader(zf.getInputStream(entry))); 53 | String line; 54 | while ((line = br.readLine()) != null) { 55 | mChannel = line; 56 | } 57 | 58 | br.close(); 59 | } 60 | } 61 | 62 | } 63 | 64 | } catch (IOException e) { 65 | e.printStackTrace(); 66 | } 67 | finally { 68 | if(in != null){ 69 | try { 70 | in.close(); 71 | } 72 | catch (Exception e){ 73 | 74 | } 75 | } 76 | 77 | if(zin != null){ 78 | try { 79 | zin.closeEntry(); 80 | } 81 | catch (Exception e){ 82 | 83 | } 84 | } 85 | 86 | if(zf != null){ 87 | try { 88 | zin.closeEntry(); 89 | } 90 | catch (Exception e){ 91 | 92 | } 93 | } 94 | } 95 | 96 | Log.d("getChannel", mChannel); 97 | 98 | return mChannel; 99 | 100 | } 101 | -------------------------------------------------------------------------------- /package.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # encoding: utf-8 3 | import zipfile,sys,shutil,os 4 | 5 | if len(sys.argv)< 3 : 6 | print "parameter error" 7 | sys.exit() 8 | 9 | if sys.argv[1].endswith('.apk') != True: 10 | print "First parameter must be apk file !" 11 | sys.exit() 12 | 13 | if os.path.isfile(sys.argv[1]) != True: 14 | print sys.argv[1] + " not exist !" 15 | sys.exit() 16 | 17 | tempFile = sys.argv[1]+"___tem" 18 | shutil.copy(sys.argv[1], tempFile) 19 | your_channel = sys.argv[2] 20 | 21 | zipped = zipfile.ZipFile(tempFile, 'a', zipfile.ZIP_DEFLATED) 22 | 23 | empty_channel_file = "META-INF/channel_info" 24 | 25 | file_object = open("empty_file", 'w') 26 | try: 27 | file_object.write(your_channel) 28 | finally: 29 | file_object.close( ) 30 | 31 | 32 | zipped.write("empty_file", empty_channel_file) 33 | 34 | zipped.close() 35 | 36 | targetFile = sys.argv[1].replace(".apk", "-" + sys.argv[2]) + ".apk" 37 | shutil.move(tempFile, targetFile) 38 | print "渠道包为:" + your_channel 39 | print "生成文件:" + targetFile 40 | 41 | 42 | 43 | --------------------------------------------------------------------------------