Android美团多渠道打包方式
来源:互联网 发布:淘宝9活动都有什么 编辑:程序博客网 时间:2024/05/16 08:25
以umeng为例,官方的渠道配置方式:
<!-- 友盟API Key --> <meta-data android:name="UMENG_APPKEY" android:value="***************"> </meta-data> <!--umeng 渠道 --> <meta-data android:name="UMENG_CHANNEL" android:value="baidu" />
然后在MyApplication中添加如下代码
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); String channel=ChannelUtil.getChannel(this, "default channel");//获取渠道名 AnalyticsConfig.setChannel(channel);//调用umeng api设置umeng渠道 }}
import java.io.IOException;import java.util.Enumeration;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.content.pm.ApplicationInfo;import android.content.pm.PackageManager.NameNotFoundException;import android.preference.PreferenceManager;import android.text.TextUtils;public class ChannelUtil {private static final String CHANNEL_KEY = "cztchannel";private static final String CHANNEL_VERSION_KEY = "cztchannel_version";private static String mChannel;/** * 返回市场。 如果获取失败返回"" * @param context * @return */public static String getChannel(Context context){return getChannel(context, "");}/** * 返回市场。 如果获取失败返回defaultChannel * @param context * @param defaultChannel * @return */public static String getChannel(Context context, String defaultChannel) {//内存中获取if(!TextUtils.isEmpty(mChannel)){return mChannel;}//sp中获取mChannel = getChannelBySharedPreferences(context);if(!TextUtils.isEmpty(mChannel)){return mChannel;}//从apk中获取mChannel = getChannelFromApk(context, CHANNEL_KEY);if(!TextUtils.isEmpty(mChannel)){//保存sp中备用saveChannelBySharedPreferences(context, mChannel);return mChannel;}//全部获取失败return defaultChannel; }/** * 从apk中获取版本信息 * @param context * @param channelKey * @return */private static String getChannelFromApk(Context context, String channelKey) {//从apk包中获取 ApplicationInfo appinfo = context.getApplicationInfo(); String sourceDir = appinfo.sourceDir; //默认放在meta-inf/里, 所以需要再拼接一下 String key = "META-INF/" + channelKey; 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(); if (entryName.startsWith(key)) { ret = entryName; break; } } } catch (IOException e) { e.printStackTrace(); } finally { if (zipfile != null) { try { zipfile.close(); } catch (IOException e) { e.printStackTrace(); } } } String[] split = ret.split("_"); String channel = ""; if (split != null && split.length >= 2) { channel = ret.substring(split[0].length() + 1); } return channel;}/** * 本地保存channel & 对应版本号 * @param context * @param channel */private static void saveChannelBySharedPreferences(Context context, String channel){SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);Editor editor = sp.edit();editor.putString(CHANNEL_KEY, channel);editor.putInt(CHANNEL_VERSION_KEY, getVersionCode(context));editor.commit();}/** * 从sp中获取channel * @param context * @return 为空表示获取异常、sp中的值已经失效、sp中没有此值 */private static String getChannelBySharedPreferences(Context context){SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);int currentVersionCode = getVersionCode(context);if(currentVersionCode == -1){//获取错误return "";}int versionCodeSaved = sp.getInt(CHANNEL_VERSION_KEY, -1);if(versionCodeSaved == -1){//本地没有存储的channel对应的版本号//第一次使用 或者 原先存储版本号异常return "";}if(currentVersionCode != versionCodeSaved){return "";}return sp.getString(CHANNEL_KEY, "");}/** * 从包信息中获取版本号 * @param context * @return */private static int getVersionCode(Context context){try{return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;}catch(NameNotFoundException e) {e.printStackTrace();}return -1;}}
到这里我们在项目中的工作完成了,然后需要安装一下python环境 (安装过程中有问题的自行百度)
登录CSDN 下载py脚本 (这个东西也是从别人那弄来的,由于时间长了,找不到来源了。。。所以才有了这篇博客做笔记) 解压出来
打开文件PythonTool\info\channel.txt,把需要的渠道写在这里面,每个渠道名占一行,就是回车,像这个样子:
上面说的都弄好之后,就开始打包工作了。
1、先使用android studio 或者 eclipse 打一个正常发布用的安装包出来。
2、把上一步的apk包放到PythonTool目录下(就是MultiChannelBuildTool.py同级目录)
3、鼠标双击MultiChannelBuildTool.py,呵呵,一眨眼的功夫,完事了
4‘、新生成了一个文件夹,里面就是所有的渠道包了,拿去发布吧
我觉得说的够简单明了吧,具体原理请看着个 http://tech.meituan.com/mt-apk-packaging.html
有知道以上资源原作者的请告知 ^_^
2 1
- Android美团多渠道打包方式
- Android美团多渠道打包
- 【Android 进阶】美团的多渠道打包方式
- Android多渠道打包汇总(六)—— 美团的多渠道打包方式
- Android多渠道打包(三):美团多渠道打包
- android 美团多渠道打包详解
- 美团多渠道打包Python 转groovy gradle 插件方式
- APK打包 (多渠道打包,美团打包)
- 美团多渠道打包记录
- 美团多渠道快速打包
- Android 一站式打包(二)美团多渠道方案
- android 中三种多渠道打包方式
- android 中三种多渠道打包方式
- Android的多渠道打包方式
- 美团多渠道打包方案初体验
- 美团多渠道打包原理以及使用
- 美团多渠道打包方案初体验
- 美团多渠道打包之实现篇
- linux 相关学习资源
- Java如何集成支付宝即可到账接口
- 虚拟现实现实吗?
- error C2665: “AfxMessageBox”: 2 个重载中没有一个可以转换所有参数类型
- Nodejs的express使用教程
- Android美团多渠道打包方式
- nodejs实战之简单实现服务器端(顺便讲一下URL)
- CSS实现超出DIV宽度文字自动隐藏并显示省略号
- css中height:100%不起作用的解决方法
- 2016年2月至3月Android学习计划
- 又找到的一个POJ的分类,好多了这次
- Java 入门 之 静态函数与静态变量
- UI方面总结
- ConcurrentLinkedQueue Demo