基于美团的多渠道打包shell 脚本

如果之前没有了解过美团多渠道打包的请查阅美团多渠道打包

好了话不多说下面来看一下脚本文件

#!/usr/bin/env bash
# !/usr/bin/bash
#//由于项目中需根据不同项目打不同的渠道包故传入参数 项目名
model=$1
# 空文件 便于写入此空文件到apk包中作为channel文件
src_empty_file='wc.txt'
# 创建一个空文件(不存在则创建)
if [ -e $src_empty_file ]; then
    rm  $src_empty_file
fi

touch $src_empty_file

if [ ! $? -eq 0 ]; then
   exit 1;
fi
# 获取当前目录中所有的apk源包
src_apks=()
#//由于目录问题,我将shell脚本放在project/doc/channel 下 ,如果和我的不同请自行更改
file_path=$(dirname $0)/../../../"${model}/build/outputs/apk/"
for file in $(cd $file_path;ls); do
newFile=${file_path}$file
  if [ -f $newFile ];then
      basename $file | grep '\.apk$' && src_apks=("${src_apks[@]}" "${file_path}$file")
      echo $basename
  fi
done
  echo ${src_apks[@]}

# 获取渠道列表,txt文件中只需写入渠道,每个渠道单行
if [ $model = "ssq" ];then
    channel_file='SSQChannel.txt'
else
    channel_file='CommonChannel.txt'
fi
f=$(cat $channel_file)

for src_apk in ${src_apks[@]};do
    # file name (with extension)
    src_apk_file_name=$(basename $src_apk)
    # 分割文件名与后缀
    src_apk_name=${src_apk_file_name%.apk}
    # 后缀名,包含.   例如: ".apk "
    src_apk_extension=${src_apk_file_name##*.}
    # 创建生成目录,与文件名相关
#    output_dir='output-'${src_apk_name}//如果需要生成新的目录则启用
    output_dir=$(dirname $0)/../../../"${model}/build/outputs/apk/"
#   if [ -e $output_dir ]; then
#       rm -r $output_dir
#   fi
    mkdir -p $output_dir

    # 遍历渠道号并创建对应渠道号的apk文件
    for line in $f;do
         target_channel=$line
         target_apk=${output_dir}${model}-${target_channel}-"release".${src_apk_extension}
        echo "${output_dir}=======>${src_apk_name}=======>, ${target_channel}  =======>, ${src_apk_extension}  =======>"
        # 拷贝建立新apk
        cp  $src_apk $target_apk
        # 初始化渠道信息
        empty_channel_file="META-INF/wcchannel_${target_channel}"
        # 写入渠道信息
        if [ ! -e $(dirname $empty_channel_file) ]; then
            mkdir -p $(dirname $empty_channel_file)
        fi
        touch $empty_channel_file
        jar -uvf $target_apk -C . $empty_channel_file
    done
done
rm -rf ./META-INF/

这里的话 执行本脚本之前请确保 已经存在release包并且位于model/build/outputs/apk/下执行完成本脚本之后为命名方便,我的release包为modelname-channel-release.apk

重点内容 注意这种方式只适用于加在不同资源在代码中根据渠道号进行切换

下面给出获取渠道号的java代码(其实上面链接中已经有了):

public static String getChannel(Context context) {
        ApplicationInfo appinfo = context.getApplicationInfo();
        String sourceDir = appinfo.sourceDir;
        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("mtchannel")) {
                    ret = entryName;
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipfile != null) {
                try {
                    zipfile.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        String[] split = ret.split("_");
        if (split != null && split.length >= 2) {
            return ret.substring(split[0].length() + 1);

        } else {
            return "";
        }
    }

好到此结束,大功告成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值