Android 使用Python实现多渠道打包
2017-06-22 13:57
417 查看
使用前提
需要结合Umeng的多渠道统计,Umeng的集成可以自己去官网了解,参考地址。环境配置
Python2.x 可以再Python官网下载Android Studio
具体使用
1 . 首先在集成Umeng统计时 不要使用标签在Manifest配置Umeng的APPID和channel。必须在自己的Application实现类中使用如下方式配置:private void initUmeng() { String appkey =SocializeConstants.APPKEY="58a2ae*************"; String channelId = ManifestUtils.getChannel(this); //这个方法会从META_INFO文件下读取渠道标志 MobclickAgent.UMAnalyticsConfig config = new MobclickAgent.UMAnalyticsConfig(this, appkey, channelId); //初始化友盟配置信息 LogUtil.e(config.mChannelId); MobclickAgent.startWithConfigure(config); //设置友盟配置信息 }
2 . 编写空的
channel.apk文件 可以直接新建一个空白txt文档,然后通过改后缀名实现。
3 . 新建
channel.txt文档,也就是渠道列表,如下图所示,每个列表隔行显示:
xiaomi 360 wandoujia QQ baidu oppo sougou zhongyi
4 . 编写python执行文件 名字随意命名.
注意不要直接在新建的文本编辑器中编写,会出现乱码问题。文件内容如下:
import sys,os,shutil,zipfile,time #导入依赖库 apkVersion="1.0" #版本号 srcFileName="test.apk" #需要多渠道打包apk的名字 outputFileName="test_V" #生成apk的前缀名 destDir=os.path.abspath('.') 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) target_file="channel.apk" 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) def getTime(): return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time())) if not os.path.exists(srcFileName): print ("sourcefile"+srcFileName+"notexists") sys.exit(1) start = time.clock() for line in file: channel=line.strip('\n').strip() targetFileName="_"+outputFileName+apkVersion+"_"+channel+".apk" #在这里调整渠道包的名称 print ("copyfile:"+targetFileName) cpFile(srcFileName,targetFileName) writeChannelToApk(targetFileName,channel) end = time.clock() print("The function run time is : %.03f seconds" %(end-start))
5 . 利用Android Studio打包apk(Eclipse的也可以)
需要注意的是 ,在Android Studio不要配置
productFlavors信息,配置好签名信息后,直接在
Terminal输入
gradlew assembleRelease进行签名打包编译。打包后的apk文件放在与上面文件同名目录下。
6 . 在命令控制台中输入
python channel.py命令执行打包操作,1~2s即可完成30个包。实在是太方便了。
7 . 不放心的同学可以再app内键入以下代码查看channel是否正确了
String channelId = AnalyticsConfig.getChannel(getApplicationContext()); //获取友盟的渠道配置信息
8 .
ManifestUtils工具类中用到的方法如下:
/** * 获取META-INFO下面的渠道 * @param context * @return */ public static String getChannel(Context context) { if (!TextUtils.isEmpty(channel)) { return channel; } ApplicationInfo appinfo = context.getApplicationInfo(); String sourceDir = appinfo.sourceDir; ZipFile zipfile = null; final String start_flag = "META-INF/channel_"; try { zipfile = new ZipFile(sourceDir); Enumeration<? extends ZipEntry> 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, ""); return channel; } } } catch (IOException e) { e.printStackTrace(); } finally { if (zipfile != null) { try { zipfile.close(); } catch (IOException e) { e.printStackTrace(); } } } return ""; }
上面用到的文件可以到这个地址下载
相关文章推荐
- Android 使用Python实现多渠道打包实践
- 使用gradle的productFlavors实现Android项目多渠道打包
- 使用gradle的productFlavors实现Android项目多渠道打包
- Android QA专用,Python实现不一样的多渠道打包工具
- 使用gradle的productFlavors实现Android项目多渠道打包
- 使用Python脚本语言实现自动多渠道打包心得
- 使用python实现多渠道打包
- Android使用Gradle实现多渠道打包
- 使用ANT编译--实现外部传参(多渠道替换)、批处理打包------第二步什么是ANT,如何使用ANT命令为Android项目打包
- Python实现Android Apk 加固及多渠道打包
- 使用gradle的productFlavors实现Android项目多渠道打包
- Android 使用gradle的productFlavors实现多渠道打包
- android 多渠道打包---使用python 3.3.2
- Android快速使用Gradle实现多渠道打包
- Android QA专用,Python实现不一样的多渠道打包工具
- Android——使用Python脚本进行多渠道打包-秒打100个包!
- Android使用兰贝壳儿实现多渠道打包
- [置顶] Android 多渠道打包:使用Gradle和Android Studio
- android中,使用ant多渠道打包时出现Perhaps JAVA_HOME does not point to the JDK问题的解决方法
- Android Studio使用Gradle多渠道自动打包