#!/bin/bash # 打包 release APK,产物命名 observer-x.y.z.apk(管理端上传版本号从文件名识别)。 # 用法: # build_apk.sh 用当前 pubspec 版本构建 # build_apk.sh --bump 自动递增 patch+1、build+1 后构建 # build_apk.sh 1.0.7 用指定版本(versionName)构建, build+1 set -euo pipefail cd "$(dirname "$0")" CUR=$(grep -m1 '^version:' pubspec.yaml | awk '{print $2}') VER=${CUR%+*} BLD=${CUR#*+} case "${1:-}" in "") ;; --bump) VER=$(awk -F. '{print $1"."$2"."($3+1)}' <<<"$VER") BLD=$((BLD + 1)) ;; *) VER="$1" BLD=$((BLD + 1)) ;; esac NEW="$VER+$BLD" if [ "$NEW" != "$CUR" ]; then sed -i '' "s/^version: .*/version: $NEW/" pubspec.yaml echo "版本号: $CUR -> $NEW" fi flutter build apk --release APK="build/app/outputs/flutter-apk/observer-${VER}.apk" mv -f build/app/outputs/flutter-apk/app-release.apk "$APK" rm -f build/app/outputs/apk/release/app-release.apk echo "APK: $APK"