Files
English/kouyu_english/install_app.sh
T

172 lines
4.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
set -e
cd "$(dirname "$0")"
BUILD_MODE="debug"
TARGET=""
for arg in "$@"; do
case "$arg" in
android|apk)
TARGET="android"
;;
mac|macos|desktop)
TARGET="mac"
;;
all)
TARGET="all"
;;
--release|-r)
BUILD_MODE="release"
;;
--debug|-d)
BUILD_MODE="debug"
;;
--help|-h)
echo "用法: ./install_app.sh [目标] [选项]"
echo ""
echo "目标 (可选):"
echo " android 打包并安装到连接的 Android 真机"
echo " mac 打包并安装到 macOS (/Applications) 并启动"
echo " all 同时打包 Android 和 macOS 版本"
echo ""
echo "选项 (可选):"
echo " --debug 编译 Debug 版本 (默认,构建更快)"
echo " --release 编译 Release 版本 (体积更小,性能更好)"
echo " -h, --help 显示帮助信息"
exit 0
;;
*)
;;
esac
done
# 如果未指定目标且处于交互终端,提示用户选择
if [ -z "$TARGET" ]; then
if [ -t 0 ]; then
echo "========================================="
echo " 🌱 芽说英语 (Kouyu English) 打包安装工具"
echo "========================================="
echo "请选择要打包安装的目标平台:"
echo " 1) Android 真机 (自动检测设备并安装)"
echo " 2) macOS 桌面版 (打包并安装到 /Applications 并启动)"
echo " 3) 全部 (Android + macOS)"
read -p "请输入序号 [1/2/3] (默认 1): " choice
case "$choice" in
2)
TARGET="mac"
;;
3)
TARGET="all"
;;
*)
TARGET="android"
;;
esac
else
# 非交互模式下,优先检测 Android 设备
if adb devices 2>/dev/null | grep -v "List of devices" | grep -q "device$"; then
TARGET="android"
else
TARGET="mac"
fi
fi
fi
install_android() {
echo ""
echo "📱 ================= Android 打包与安装 ================="
echo "🔍 检查连接的 Android 设备..."
DEVICE_COUNT=$(adb devices 2>/dev/null | grep -v "List of devices" | grep "device$" | wc -l | tr -d ' ')
if [ "$DEVICE_COUNT" -eq 0 ]; then
echo "❌ 未检测到连接的 Android 设备,请确保手机已开启 USB 调试并通过数据线连接!"
return 1
fi
echo "📦 正在编译 Android ${BUILD_MODE} APK (包含最新的 assets 配置)..."
if [ "$BUILD_MODE" = "release" ]; then
flutter build apk --release
APK_PATH="build/app/outputs/flutter-apk/app-release.apk"
else
flutter build apk --debug
APK_PATH="build/app/outputs/flutter-apk/app-debug.apk"
fi
if [ ! -f "$APK_PATH" ]; then
echo "❌ 找不到编译输出的 APK: $APK_PATH"
return 1
fi
echo "🚀 正在安装 APK 到手机..."
if ! adb install -r "$APK_PATH"; then
echo "⚠️ 覆盖安装失败(签名不一致或版本冲突),正在卸载旧版本并重新安装..."
adb uninstall com.shen.kouyu_english || true
adb install -r "$APK_PATH"
fi
echo "▶️ 正在手机上启动 芽说英语 App..."
adb shell monkey -p com.shen.kouyu_english -c android.intent.category.LAUNCHER 1 > /dev/null 2>&1 || true
echo "✅ Android 版本安装并启动成功!"
}
install_mac() {
echo ""
echo "🖥️ ================= macOS 打包与安装 ================="
echo "📦 正在编译 macOS ${BUILD_MODE} 应用..."
if [ "$BUILD_MODE" = "release" ]; then
flutter build macos --release
APP_SRC="build/macos/Build/Products/Release/kouyu_english.app"
else
flutter build macos --debug
APP_SRC="build/macos/Build/Products/Debug/kouyu_english.app"
fi
if [ ! -d "$APP_SRC" ]; then
echo "❌ 找不到编译输出的 macOS App: $APP_SRC"
return 1
fi
APP_NAME="芽说英语.app"
DEST_DIR="/Applications"
DEST_APP="$DEST_DIR/$APP_NAME"
echo "📂 正在安装到 $DEST_APP..."
# 检查是否有权限写入 /Applications,如果无权限则回退至 ~/Applications
if [ ! -w "$DEST_DIR" ]; then
DEST_DIR="$HOME/Applications"
mkdir -p "$DEST_DIR"
DEST_APP="$DEST_DIR/$APP_NAME"
echo "️ /Applications 无写入权限,改用 $DEST_APP"
fi
# 关闭可能正在运行的实例
pkill -f "芽说英语" 2>/dev/null || pkill -f "kouyu_english" 2>/dev/null || true
sleep 1
# 同步/拷贝应用
rm -rf "$DEST_APP"
cp -R "$APP_SRC" "$DEST_APP"
echo "▶️ 正在启动 macOS 芽说英语 App..."
open "$DEST_APP"
echo "✅ macOS 版本已成功安装到 $DEST_APP 并启动!"
}
case "$TARGET" in
android)
install_android
;;
mac)
install_mac
;;
all)
install_android || true
install_mac
;;
esac
echo ""
echo "🎉 全部操作已完成!"