您的位置:首页 > 编程语言 > Python开发

python 打安卓APK渠道包,分分钟千把个

2016-07-11 16:26 477 查看
目前安卓APK打渠道包,主要有3种方式,

一、传统手动, 一个一个的改渠道号,然后改一个打一个,这种只有当渠道数不多时用,如果渠道有几百甚至上千个,估计眼睛都会花手都会麻,还不知道何时何月才能完成

二、gradle配置,可以先把所有的渠道都在gradle里面配置好,然后执行打包,就会一个一个的去编译,直接到打完,一般情况打一个包需要半分到一分钟,如果100个包需要2个小时,1000个包需要一天,我的神啊,这种方式只能用于渠道包不是很多的情况下

三、接下来讲讲本文的重点方法,Python打包,原理很简单,就是把一个APK文件解压后放入一个渠道命名的空文件,然后再把这些文件压缩回APK,经过使用,确实非常方便快捷,一分钟打1000个绝对不是问题,下面有图有真相,我打了大概500个渠道包,耗时15秒,牛逼吧!

如图:



 下面贴下打包的Python代码,目的就是把APK解压,然后利用渠道号文件,生成对应渠道的APK

import sys,os,shutil,zipfile,time
apkVersion="1.0"
srcFileName="source.apk"
apksDir="apks"
destDir=os.path.abspath('.')
target_file="channel.apk"

file=open("channel.txt")

def writeChannelToApk(filename,channel):
z=zipfile.ZipFile(filename,'a',zipfile.ZIP_DEFLATED)
empty_channel_file="META-INF/channel_{channe}".format(channe=channel)
if not os.path.exists(target_file):
open(target_file,'a').close()
z.write(target_file,empty_channel_file)
z.close()
print "writeChannelToApkchannel"+channel+","+filename+"\n"

def cpFile(srcPath,fileName):
destPath = destDir + os.path.sep + fileName
if os.path.exists(srcPath) and not os.path.exists(destPath):
shutil.copy(srcPath,destPath)

if not os.path.exists(srcFileName):
print "source file "+srcFileName+" not exists"
sys.exit(1)

start = time.clock()

if not os.path.exists(apksDir):
os.makedirs(apksDir)

for line in file:
channel=line.strip('\n').strip()
targetFileName=apksDir+"/hoolay_"+channel+".apk"
print "copyfile:"+targetFileName
cpFile(srcFileName,targetFileName)
writeChannelToApk(targetFileName,channel)
end = time.clock()

print("The function run time is : %.03f seconds" %(end-start))

#heliao-app-91-5.1.1


接下来是Java文件代码,目的是根据APK中的渠道文件获取渠道号

package com.hoolay.core.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.text.TextUtils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ManifestUtil {

private static final String SP_KEY = "device_channel";
public static final String START_FLAG = "META-INF/channel_";

/**
* 获取META-INFO下面的渠道
*
* @param context
* @return
*/
public static String getChannel(Context context) {
SharedPreferences sp = context.getSharedPreferences("hoolay_sp", Context.MODE_PRIVATE);
String channel = sp.getString(SP_KEY, null);
if (!TextUtils.isEmpty(channel)) {
return channel;
}
ApplicationInfo appinfo = context.getApplicationInfo();
String sourceDir = appinfo.sourceDir;
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.contains(START_FLAG)) {
channel = entryName.replaceAll(START_FLAG, "");
sp.edit().putString(SP_KEY, channel).apply();
return channel;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (zipfile != null) {
try {
zipfile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return "";

}
}


下面是渠道号,按需自行增删

360
appChina
wandoujia
91
baidu
QQ
3G
eoe
anzhi
163
hiapk
jifeng
xiaomi
meizu
oppo
lenovo


打好的APK:



说下Windows下怎么运行运行Python脚本,首先要安装Python工具,我这里用的2.7,当然也可以用其他版本,可能会影响的python语法,有语法问题,自行修改,
安装好配置下环境变量,然后在命令提示符里面检查下:输入   python   



如果有上提示,说明就好了,都完事了就可以运行Python脚本了,接着需要进入到python脚本所在的文件目录,因为这个脚本当前用的是相对路径,然后输入:python  batch_apk.py 

回车后就会看到在打包了,如果提示有语法错误或者找不到某个文件,说明前置任务还没完成,请自行百度和检查,好了,各位童鞋,

最后我把打包工具的下载链接贴上

点击打开链接
http://download.csdn.net/detail/msn465780/9572954
后续问题请看这篇文章

点击打开链接
http://www.jianshu.com/p/52a3c3187dcc


Android 新一代多渠道打包神器

https://zhuanlan.zhihu.com/p/26674427 点击打开链接

新一代开源Android渠道包生成工具Walle
https://tech.meituan.com/android-apk-v2-signature-scheme.html   点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息