删除打包后带入的本地目录和切换图标功能

This commit is contained in:
谢宇宁
2026-11-09 20:29:13 +07:00
parent b29916a6bb
commit f39c8eef17
45 changed files with 116 additions and 445 deletions
+90 -9
View File
@@ -82,6 +82,22 @@ RAW_VERSION=$(grep '^version:' "$PUBSPEC_FILE" | awk '{print $2}')
SAFE_VERSION=${RAW_VERSION//+/_}
TIME_TAG=$(date +"%m%d-%H%M")
# 混淆 + 抽离调试信息:
# --obfuscate 混淆 Dart 符号;--split-debug-info 把调试信息(含 libapp.so 里
# 嵌入的本机绝对路径 .../huangguo_android/.dart_tool/.../dart_plugin_registrant.dart)
# 抽到单独目录,不再残留在产物里。反编译后搜不到本机用户名/工程路径。
# 仅 release/profile 生效(debug 为 JIT,不支持也无需)。symbols 目录务必保留、勿外传,
# 崩溃堆栈符号化要用它。
OBFUSCATE_ARGS=""
if [ "$MODE" = "release" ] || [ "$MODE" = "profile" ]; then
SYMBOLS_DIR="$(pwd)/build/symbols/${MODE}_${SAFE_VERSION}_${TIME_TAG}"
mkdir -p "$SYMBOLS_DIR"
# 注意:本项目已启用 BlackObfuscator,再叠加 Flutter --obfuscate 会导致启动黑屏,故关闭。
# 去除 libapp.so 内嵌构建路径改由出包后的 strip_build_path 二进制替换完成,与此无关。
OBFUSCATE_ARGS=""
# OBFUSCATE_ARGS="--obfuscate --split-debug-info=$SYMBOLS_DIR"
fi
# 读取应用内 Config.isDebug(lib/config/config.dart),决定产物命名标识:
# isDebug=true → 命名标识固定为 debug(如 hgdj_debug_xxxx),方便区分内测包
# isDebug=false → 正式环境,命名沿用构建模式(release/profile),保持不变
@@ -119,15 +135,77 @@ echo "=========================================="
echo "确认无误?(按回车继续,Ctrl+C 取消)"
read -r CONFIRM
# 清除 libapp.so 里嵌入的本机工程绝对路径(.../<工程>/.dart_tool/.../dart_plugin_registrant.dart)
# 做法:对该 URI 字符串做「等长」替换为中性路径(不含用户名/工程名),再 zipalign + 用原 keystore 重签。
# 等长替换保证 Dart String 长度字段不变、运行时安全;该 URI 仅为元数据,不参与运行逻辑。
strip_build_path() {
local APK="$1"
[ -f "$APK" ] || return 0
local BT; BT=$(ls -d "$HOME/Library/Android/sdk/build-tools/"* 2>/dev/null | sort -V | tail -1)
# 优先用 gradle 指定的 keystore(android/keystore/app-release.jks),避免误选到同目录备份
local KS=""
for cand in android/keystore/app-release.jks android/app/../keystore/app-release.jks; do
[ -f "$cand" ] && { KS="$cand"; break; }
done
[ -z "$KS" ] && KS=$(find android -name '*.jks' ! -name '*old*' ! -name '*bak*' 2>/dev/null | head -1)
local SP KP KA
SP=$(grep -E "storePassword" android/app/build.gradle | head -1 | sed -E "s/.*['\"]([^'\"]+)['\"].*/\1/")
KP=$(grep -E "keyPassword" android/app/build.gradle | head -1 | sed -E "s/.*['\"]([^'\"]+)['\"].*/\1/")
KA=$(grep -E "keyAlias" android/app/build.gradle | head -1 | sed -E "s/.*['\"]([^'\"]+)['\"].*/\1/")
if [ -z "$BT" ] || [ -z "$KS" ] || [ -z "$SP" ] || [ -z "$KA" ]; then
echo "⚠️ 跳过路径清理:缺少 build-tools / keystore / 签名信息"; return 0
fi
echo "🧹 清理 libapp.so 内嵌构建路径..."
local TMP; TMP=$(mktemp -d)
PROJ="$(pwd)" python3 - "$APK" "$TMP/patched.apk" <<'PY'
import sys, os, zipfile
from urllib.parse import quote
src, dst = sys.argv[1], sys.argv[2]
TAIL = b'/.dart_tool/flutter_build/dart_plugin_registrant.dart'
proj = os.environ["PROJ"]
# Flutter 以百分号编码嵌入非 ASCII 路径段;精确构造「当前工程」那条完整字节串
needle = quote(proj, safe="/").encode() + TAIL
# 等长中性替换:保留结构性尾巴,头部用 /aaa.../ 填满到同样长度(不含用户名/工程名)
repl = b'/' + b'a' * (len(needle) - 1 - len(TAIL)) + TAIL
assert len(repl) == len(needle)
def patch(d):
c = d.count(needle)
return (d.replace(needle, repl), c) if c else (d, 0)
zin = zipfile.ZipFile(src); zout = zipfile.ZipFile(dst, 'w'); total = 0
for it in zin.infolist():
data = zin.read(it.filename)
if it.filename.endswith('/libapp.so'):
data, n = patch(data); total += n
zi = zipfile.ZipInfo(it.filename, date_time=it.date_time)
zi.compress_type = it.compress_type; zi.external_attr = it.external_attr
zi.internal_attr = it.internal_attr; zi.create_system = it.create_system
zout.writestr(zi, data)
zout.close(); zin.close()
print(" 精确替换次数:", total)
if total == 0:
print(" ⚠️ 未找到工程路径字节串,未改动(宁可保留也不乱替换)")
PY
"$BT/zipalign" -f -p 4 "$TMP/patched.apk" "$TMP/aligned.apk" >/dev/null 2>&1 \
&& "$BT/apksigner" sign --ks "$KS" --ks-pass "pass:$SP" --ks-key-alias "$KA" --key-pass "pass:${KP:-$SP}" \
--v1-signing-enabled false --v2-signing-enabled true --out "$TMP/signed.apk" "$TMP/aligned.apk" >/dev/null 2>&1 \
&& "$BT/apksigner" verify "$TMP/signed.apk" >/dev/null 2>&1
if [ $? -eq 0 ]; then
cp "$TMP/signed.apk" "$APK"; echo "✅ 已清理并重签:$APK"
else
echo "⚠️ 清理失败,保留原包未改动:$APK"
fi
rm -rf "$TMP"
}
# Android 构建
build_android() {
echo "▶️ 开始构建 Android $MODE 包..."
if [ "$MODE" = "release" ]; then
flutter build apk --release --target-platform android-arm64
flutter build apk --release --target-platform android-arm64 $OBFUSCATE_ARGS
APK_SOURCE="build/app/outputs/flutter-apk/app-release.apk"
elif [ "$MODE" = "profile" ]; then
flutter build apk --profile --target-platform android-arm64
flutter build apk --profile --target-platform android-arm64 $OBFUSCATE_ARGS
APK_SOURCE="build/app/outputs/flutter-apk/app-profile.apk"
else
flutter build apk --debug --target-platform android-arm64
@@ -143,7 +221,10 @@ build_android() {
APK_DEST="${DIST_DIR}/${APK_NAME}"
cp "$APK_SOURCE" "$APK_DEST"
APK_SIZE=$(du -h "$APK_DEST" | awk '{print $1}')
# 注意:本 App 有运行时自校验,任何出包后修改 libapp.so(即使等长精确替换并重签)都会
# 导致启动卡在启动页。故不再对产物做二进制补丁。去除内嵌构建路径改用“中性目录构建”方案。
# strip_build_path 函数保留备查,但默认不调用。
echo "✅ Android 构建成功!"
echo "📁 输出路径: $APK_DEST"
echo "📏 文件大小: $APK_SIZE"
@@ -164,19 +245,19 @@ build_ios() {
if [ "$MODE" = "release" ]; then
if [ "$SIGN_MODE" = "nocodesign" ]; then
flutter build ipa --release --no-codesign
flutter build ipa --release --no-codesign $OBFUSCATE_ARGS
elif [ -n "$EXPORT_OPTIONS_PLIST" ]; then
flutter build ipa --release --export-options-plist="$EXPORT_OPTIONS_PLIST"
flutter build ipa --release --export-options-plist="$EXPORT_OPTIONS_PLIST" $OBFUSCATE_ARGS
else
flutter build ipa --release --export-method "$EXPORT_METHOD"
flutter build ipa --release --export-method "$EXPORT_METHOD" $OBFUSCATE_ARGS
fi
else
if [ "$SIGN_MODE" = "nocodesign" ]; then
flutter build ipa --profile --no-codesign
flutter build ipa --profile --no-codesign $OBFUSCATE_ARGS
elif [ -n "$EXPORT_OPTIONS_PLIST" ]; then
flutter build ipa --profile --export-options-plist="$EXPORT_OPTIONS_PLIST"
flutter build ipa --profile --export-options-plist="$EXPORT_OPTIONS_PLIST" $OBFUSCATE_ARGS
else
flutter build ipa --profile --export-method "$EXPORT_METHOD"
flutter build ipa --profile --export-method "$EXPORT_METHOD" $OBFUSCATE_ARGS
fi
fi