前言
自2021年8月起,Google建议在Google Play发布的应用使用 Android App Bundle(AAB)格式; AAB可以提供更小的App体积,提升用户的下载转化率并减少卸载量,其要求应用程序的大小不超过150MB。
海外应用包体积优化,原先使用的是APK格式;后来使用Android App Bundle格式,大幅度降低了包体积大小; 单是so文件的分包配置,降低96M;
本文记录怎么修改工程,打包机的配置从apk到AAB格式的发布。
1、Bitrise构建apk时,通知到slack的打包群。
登录Bitrise后台,找老板申请管理员权限;选择一个workflow,然后添加一个message的step步骤。
1.2、填写send a slack message的step
分别是Slack Webhook URL,和Target Slack channel
下图是Target Slack channel的填写
下图是Slack Webhook URL的填写, 需要找管理员开通Collaborate
点击新增Webhook URL的copy按钮,去bitrise配置一个系统变量;key需要自己命名,值就是刚刚copy的Webhook URL;
2、Slack发送消息触发Bitrise的workflow
核心是创建Create New Command,填写command 和 Request URL两个参数。
2.1、创建Create New Command
打开slack配置后台,左侧菜单打开Slack Commands,点击Create New Command。
2.2、填写command 和 Request URL两个参数
command也是自定义的,只需要达意就行没有固定写法,一般Android打包可以写成 /build-android
Request URL还需要到Bitrise后台,在Setting中寻找
3、构建时候,获取版本号
3.1、在bitrise的steps中添加一个script的step,并编辑添加shell脚本。
3.1、shell脚本的编写
核心是使用envman命令加一个临时系统变量
#!/usr/bin/env bash
# fail if any commands fails
set -e
# make pipelines' return status equal the last command to exit with a non-zero status, or zero if all commands exit successfully
set -o pipefail
# debug log
set -x
# write your script here
echo "Hello World!"
current_dir=$(pwd)
echo "current_dir=${current_dir}"
# 列出当前目录下的文件和文件夹,并通过grep过滤出文件夹
folders=$(ls -l | grep '^d' | awk '{print $9}')
# 打印文件夹列表
echo "当前目录下的文件夹有:"
echo "$folders"
# or run a script from your repository, like:
# bash ./path/to/script.sh
# not just bash, e.g.:
# ruby ./path/to/script.rb
GLOBAL_APP_VERSION=$(grep -m 1 -w "var GLOBAL_APP_VERSION =" project/app/build.gradle | tr -cd "[0-9]")
echo "GLOBAL_APP_VERSION=${GLOBAL_APP_VERSION}"
MAJOR_FEATURE_VERSION=$(grep -m 1 -w "var MAJOR_FEATURE_VERSION =" project/app/build.gradle | tr -cd "[0-9]")
echo "MAJOR_FEATURE_VERSION=${MAJOR_FEATURE_VERSION}"
MINOR_FEATURE_VERSION=$(grep -m 1 -w "var MINOR_FEATURE_VERSION =" project/app/build.gradle | tr -cd "[0-9]")
echo "MINOR_FEATURE_VERSION=${MINOR_FEATURE_VERSION}"
BUGFIX_VERSION=$(grep -m 1 -w "var BUGFIX_VERSION =" project/app/build.gradle | tr -cd "[0-9]")
echo "BUGFIX_VERSION=${BUGFIX_VERSION}"
appVersion="${GLOBAL_APP_VERSION}.${MAJOR_FEATURE_VERSION}.${MINOR_FEATURE_VERSION}.${BUGFIX_VERSION}"
envman add --key Android_App_Version --value "$appVersion"
echo "appVersion=${appVersion}"
4、构建时候,动态修改版本号
核心添加script的step,内部是shell脚本;通过shell脚本直接修改build.gradle文件
shell脚本参考
#!/usr/bin/env bash
# fail if any commands fails
set -e
# make pipelines' return status equal the last command to exit with a non-zero status, or zero if all commands exit successfully
set -o pipefail
# debug log
set -x
# write your script here
#!/bin/bash
fileName="project/app/build.gradle"
GLOBAL_APP_VERSION=$(grep -m 1 -w "GLOBAL_APP_VERSION =" $fileName | tr -cd "[0-9]")
echo "GLOBAL_APP_VERSION=${GLOBAL_APP_VERSION}"
MAJOR_FEATURE_VERSION=$(grep -m 1 -w "var MAJOR_FEATURE_VERSION =" $fileName | tr -cd "[0-9]")
echo "MAJOR_FEATURE_VERSION=${MAJOR_FEATURE_VERSION}"
MINOR_FEATURE_VERSION=$(grep -m 1 -w "var MINOR_FEATURE_VERSION =" $fileName | tr -cd "[0-9]")
echo "MINOR_FEATURE_VERSION=${MINOR_FEATURE_VERSION}"
BUGFIX_VERSION=$(grep -m 1 -w "var BUGFIX_VERSION =" $fileName | tr -cd "[0-9]")
echo "BUGFIX_VERSION=${BUGFIX_VERSION}"
version=$BITRISE_GIT_MESSAGE
echo "inputVersion=${version}"
if [[ $version =~ ^(v|version-)+[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "✅ It must start with 'v' or 'version-' and be followed by a four-digit version number."
index=$(echo "$version" | grep -bo '[0-9]' | head -n 1 | cut -d: -f1)
if [ -n "$index" ]; then
result="${version:$index}"
echo "result: $result"
IFS='.' read -r -a arr <<<"$result"
len=${#arr[@]}
if [ ${#arr[@]} -eq 4 ]; then
for ((i = 0; i < len; i++)); do
if [ "$i" -eq 0 ]; then
old="GLOBAL_APP_VERSION = ${GLOBAL_APP_VERSION}"
new="GLOBAL_APP_VERSION = ${arr[$i]}"
sed -i "s/$old/$new/g" $fileName
elif [ "$i" -eq 1 ]; then
old="MAJOR_FEATURE_VERSION = ${MAJOR_FEATURE_VERSION}"
new="MAJOR_FEATURE_VERSION = ${arr[$i]}"
sed -i "s/$old/$new/g" $fileName
elif [ "$i" -eq 2 ]; then
old="MINOR_FEATURE_VERSION = ${MINOR_FEATURE_VERSION}"
new="MINOR_FEATURE_VERSION = ${arr[$i]}"
sed -i "s/$old/$new/g" $fileName
elif [ "$i" -eq 3 ]; then
old="BUGFIX_VERSION = ${BUGFIX_VERSION}"
new="BUGFIX_VERSION = ${arr[$i]}"
sed -i "s/$old/$new/g" $fileName
else
old="BUGFIX_VERSION = ${BUGFIX_VERSION}"
new="BUGFIX_VERSION = ${arr[$i]}"
sed -i "s/$old/$new/g" $fileName
fi
done
else
echo "The version format is not 4 digits"
fi
else
echo "not found '.'"
fi
else
echo "❌ Does not conform to the format, must start with 'v' or 'version-', suffixed with a four-digit version number"
fi
# or run a script from your repository, like:
# bash ./path/to/script.sh
# not just bash, e.g.:
# ruby ./path/to/script.rb
修改成功后的打包命令参考
/build-android w:xxx-google|b:feature/app_xxx|m:v7.9.12.5
5、收工测试,见标题截图
总结
今年领悟了一句话,做对事情比勤奋重要的多,慢就是快;