当前位置: 代码迷 >> 综合 >> 使用python脚本语言实现快速打包
  详细解决方案

使用python脚本语言实现快速打包

热度:85   发布时间:2023-10-12 18:55:28.0

好久没有写文档了,最近有个新的需求,需要将我们的apk交给运营来进行打包,并且运营人员可以自定义渠道包和渠道内容

废话不多说,直接上代码


1、在我们的项目中使用下面这个工具类获取渠道信息

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/里, 所以需要再拼接一下 META-INF/cztchannel  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;}
}

2、在应用启动的地方 获取渠道信息,

chanalID = ChannelUtil.getChannel(getApplicationContext());
String[] channels = chanalID.split("_");
数组的第0位是渠道信息,数组第一位是我们另外的配置信息,可以保存在sp里,在需要获取配置信息的地方进行使用

将渠道信息配置在友盟里

MobclickAgent.startWithConfigure(new MobclickAgent.UMAnalyticsConfig(mContext, "umeng_key", channels[0]));

3、将请单列表中的

<!--<meta-data-->  <!--android:name="UMENG_CHANNEL"-->  <!--android:value="${UMENG_CHANNEL_VALUE}" />-->
注释掉,因为我们现在要用的是脚本渠道打包

同时将APP build里的productFlavors也一并注释掉


4、在python下新建一个python文件,代码如下   代码直接copy

#!/usr/bin/python
# coding=utf-8
import zipfile
import shutil
import os


# 空文件 便于写入此空文件到apk包中作为channel文件
src_empty_file = 'info/czt.txt'
# 创建一个空文件(不存在则创建)
f = open(src_empty_file, 'w') 
f.close()


# 获取当前目录中所有的apk源包
src_apks = []
# python3 : os.listdir()即可,这里使用兼容Python2的os.listdir('.')
for file in os.listdir('.'):
    if os.path.isfile(file):
        extension = os.path.splitext(file)[1][1:]
        if extension in 'apk':
            src_apks.append(file)


# 获取渠道列表
channel_file = 'info/channel.txt'
f = open(channel_file)
lines = f.readlines()
f.close()


for src_apk in src_apks:
    # file name (with extension)
    src_apk_file_name = os.path.basename(src_apk)
    # 分割文件名与后缀
    temp_list = os.path.splitext(src_apk_file_name)
    # name without extension
    src_apk_name = temp_list[0]
    # 后缀名,包含.   例如: ".apk "
    src_apk_extension = temp_list[1]
    
    # 创建生成目录,与文件名相关
    output_dir = 'output_' + src_apk_name + '/'
    # 目录不存在则创建
    if not os.path.exists(output_dir):
        os.mkdir(output_dir)
        
    # 遍历渠道号并创建对应渠道号的apk文件
    for line in lines:
        # 获取当前渠道号,因为从渠道文件中获得带有\n,所有strip一下
#target_channel = xiaomi_packageName
# target_channel.split("_")[0]
        target_channel = line.strip()
        # 拼接对应渠道号的apk
        target_apk = output_dir + src_apk_name + "_" + target_channel.split("_")[0] + "_r" + src_apk_extension  
        # 拷贝建立新apk
        shutil.copy(src_apk,  target_apk)
        # zip获取新建立的apk文件
        zipped = zipfile.ZipFile(target_apk, 'a', zipfile.ZIP_DEFLATED)
        # 初始化渠道信息
        empty_channel_file = "META-INF/cztchannel_{channel}".format(channel = target_channel)
        # 写入渠道信息
        zipped.write(src_empty_file, empty_channel_file)
        # 关闭zip流
        zipped.close()


5、与 .py同级目录下新建一个文件夹,命名为info,info文件夹下新建一个空txt文件,命名为ctz.txt,同时新建一个channel.txt文档,用来存放渠道信息

channel.txt文件内,一行只能保存一个渠道信息,渠道信息用"_"分割开,例如 wodeshijie_com.temg.wodeshijie  “_”分割线前面部分为渠道名或渠道ID,分割线后面部分为渠道配置信息


6、搭建python环境,双击  .py文件,即可运行    我这里是命名为MultiChannelBuildTool.py的,亲测30个渠道包不到10秒就可以完成


  相关解决方案