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" /> 


其中“UMENG_CHANNEL”字段为渠道信息,使用美团的方式这里可以注释掉,只需填写api key。

然后在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渠道    }}


然后是ChannelUtil.java,这个是在别的博客弄来的,至于来源我是找不到了,所有才写了这篇博客做笔记 ^_^

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
原创粉丝点击