Flutter多渠道多资源终极解决方案之自动化打包

1,397 阅读4分钟

前言

在上一篇文章中,介绍了flutterandroidios的多渠道多资源配置
文章连接:Flutter多渠道多资源配置终极解决方案
一套完整的多渠道配置方案,打包的自动化也是必不可少的。
本篇文章将延续上文的配置项目,使用shell脚本 + jenkins来配置自动化打包。

脚本编写

演示的脚本编写比较简单,可以结合实际情况自己添加
1、打包只需要知道两个参数,一个是打包的渠道chennel一个是打包的平台platform
2、打包指令分别为

android
flutter build apk --flavor ${channel} --dart-define=CHANNEL=${channel}

ios
flutter build ios
flutter build ipa --flavor ${channel} --dart-define=CHANNEL=${channel}

3、代码编写,代码比较简单,直接看注释吧

# 创建文件夹 与 clean操作
if [ ! -d '../output_dir' ]; then
  mkdir '../output_dir'
fi
rm -rf ../output_dir/*

# 接收渠道参数
channel=$1
# 接收平台参数,默认android + ios  可以指定android 或者 ios
platform=$2
channel_low=`echo $channel | tr 'A-Z' 'a-z'}`
echo "[channel]:${channel}"
echo "[platform]:${platform}"

# 打包操作
if [ ! -n "$platform" ]; then
  echo 'all'
  flutter build apk --flavor ${channel} --dart-define=CHANNEL=${channel}
  mv ../build/app/outputs/flutter-apk/app-${channel_low}-release.apk ../output_dir/app-${channel}-release.apk
  flutter build ios --release
  flutter build ipa --flavor ${channel} --dart-define=CHANNEL=${channel}
  mv ../build/ios/archive/${channel}.xcarchive ../output_dir/${channel}.xcarchive
else
  if [ $platform == 'android' ]; then
    flutter build apk --flavor ${channel} --dart-define=CHANNEL=${channel}
    mv ../build/app/outputs/flutter-apk/app-${channel_low}-release.apk ../output_dir/app-${channel}-release.apk
  elif [ $platform == 'ios' ]; then
    flutter build ios --release
    flutter build ipa --flavor ${channel} --dart-define=CHANNEL=${channel}
    mv ../build/ios/archive/${channel}.xcarchive ../output_dir/${channel}.xcarchive
  else
    echo 'all'
    flutter build apk --flavor ${channel} --dart-define=CHANNEL=${channel}
    mv ../build/app/outputs/flutter-apk/app-${channel_low}-release.apk ../output_dir/app-${channel}-release.apk
    flutter build ios --release
    flutter build ipa --flavor ${channel} --dart-define=CHANNEL=${channel}
    mv ../build/ios/archive/${channel}.xcarchive ../output_dir/${channel}.xcarchive
  fi
fi

# 上传output_dir里的打包文件到cstore,钉钉,企业微信等....
# 下载链接为: https://jenkins域名/job/工作空间/ws/生成apk的上级目录/apk名称
# 如本案例为:http://localhost:8080/job/channel_demo/ws/output_dir/app-channelA-release.apk
# 一般会发送到钉钉,企业微信等....
# 企业微信Api入口:https://developer.work.weixin.qq.com/document/path/91770
# 钉钉Api入口:https://open.dingtalk.com/document/robots/custom-robot-access
#如: curl -s -X POST -d "本次构建的下载链接为:$downUrl" "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${token}"

路径

image.png 4、脚本使用

# 打包channelB 安卓
./build_shell/build.sh channelB android 
# 打包channelA ios
./build_shell/build.sh channelA ios
# 打包channelB 安卓和ios
./build_shell/build.sh channelB 

最终生成在output_dir目录下
如果你只是想做本地打包,那到此就已经实现了。
但为了更加自动化,一般还会搭配jenkins使用,任意人使用浏览器即可完成构建

Jenkins搭建

1、安装jenkins

本次演示是基于自己本地的电脑macos做演示
可以针对自己的需求选择是在服务器还是在自己的电脑进行配置
配置流程上基本是一致的
不同系统安装流程可能不一样。
具体请看这里

macos在终端窗口输入brew install jenkins即可安装
2、配置服务
相关服务启动命令

# 启动jenkins
brew services start jenkins
#停止jenkins
brew services stop jenkins
#重启jenkins
brew services restart jenkins
#获取初始密码
cat ~/.jenkins/secrets/initialAdminPassword

安装成功后,先运行服务 在终端窗口输入brew services start jenkins
先启动jenkins服务
打开浏览器输入http://localhost:8080 点击安装新手入门插件即可 image.png

如果遇到卡住不动,运行brew services restart jenkins重启服务后,继续安装即可

image.png

开始新建任务,点击左侧新建任务

image.png

构建项目

image.png

配置项目

image.png

配置选项参数,渠道参数channel和平台参数platform

image.png

image.png

配置源码

image.png

编写构建脚本

image.png

#!/bin/bash
chmod 777 ./build_shell/build.sh 
cd ./build_shell
./build.sh $channel $platform
cd ..

点击保存即可。

打包使用

在浏览器输入http://localhost:8080/ 进入jenkins 点击配置好的任务

image.png

选择对应的渠道参数chennel和对应的平台参数platform,开始构建

image.png

最终生成的文件,在工作空间查看

image.png

获取最终文件的下载地址

下载链接为:http://jenkins域名/job/工作空间/ws/生成apk的上级目录/apk名称
如本案例为:http://localhost:8080/job/channel_demo/ws/output_dir/app-channelA-release.apk

在工作空间里选中最终打包的apk或者xcarchive文件,右键复制链接即可看到

image.png

本次演示是基于自己本地的电脑macos做演示
可以针对自己的需求选择是在服务器还是在自己的电脑进行配置
配置流程上基本是一致的
需要将localhost换成服务器ip地址即可

最后

到目前为止,我们已经整理了

Flutter多渠道多资源的android和ios配置
Flutter多渠道多资源的自动化打包和jenkins配置(本篇)\

但对于一套完整完整的多渠道多资源配置来讲
目前能定制化的资源实在太少了,只有图标app名称启动页

对于androidsourceset能够实现
对于iostarget能够实现
flutter框架目前似乎并不支持分渠道的资源配置。

因此,下篇文章将针对多资源的分渠道配置问题,通过脚本来实现

flutter项目资源的多渠道配置能力 下次再说吧。