chore(android): 构建脚本拆分开发与生产两套环境

- 新增 scripts/android-sync.ps1:dev 注入局域网接口地址,prod 清掉 VITE_API_BASE_URL 走 .env.production
- 新增 android:sync:dev / android:sync:prod,android:sync 保留为 prod 别名
- android-open.ps1 改为复用 android:sync:dev,不再自己拼环境变量
- NATIVE_UPGRADE.md 补充「打壳前必须 sync:prod」与产物核对项
This commit is contained in:
Timi
2026-11-17 14:51:00 +08:00
parent f3689a2ccc
commit bac3a15cbe
4 changed files with 37 additions and 10 deletions
+5
View File
@@ -40,6 +40,11 @@ grep versionCode android/app/build.gradle
- 增删插件后执行 `pnpm exec cap sync android`
- 确认 `capacitor.config.ts` 无遗漏
- **打壳前先 `pnpm run android:sync:prod` 重建资源**:它用 `.env.production` 构建并会清掉
残留的 `VITE_API_BASE_URL`。真机调试用的 `android:sync:dev`(或 `android:open`)会把局域网地址
写进 `dist` 和 `android/app/src/main/assets/public`,直接拿去打包就会打出一个连内网的正式包
- 打完核对:`aapt2 dump badging` 的 versionCode、`apksigner verify --print-certs` 的证书,
以及解包 grep 接口地址(内网地址必须为 0 次)
- 用 Android Studio 打包,**签名必须与线上包一致**:线上 1.0.0 用的是
`twa/android.keystore`(alias `gao-customer`,证书 `CN=yeyu` / SHA-256 `3E:9C:CC:8A:…`)。
签名不一致时系统会拒绝覆盖安装,老用户只能卸载重装
+4 -2
View File
@@ -14,9 +14,11 @@
"lint": "eslint . --ext .ts,.vue",
"type-check": "vue-tsc -p tsconfig.app.json --noEmit",
"type-check:publish": "vue-tsc -p tsconfig.publish.json --noEmit",
"android:sync": "pnpm run build && cap sync android",
"android:sync:dev": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/android-sync.ps1 -Mode dev",
"android:sync:prod": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/android-sync.ps1 -Mode prod",
"android:sync": "pnpm run android:sync:prod",
"android:open": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/android-open.ps1",
"android:apk": "pnpm run android:sync && cd android && gradlew.bat assembleDebug"
"android:apk": "pnpm run android:sync:prod && cd android && gradlew.bat assembleDebug"
},
"dependencies": {
"@capacitor/android": "^8",
+2 -8
View File
@@ -1,13 +1,7 @@
$ErrorActionPreference = "Stop"
$env:VITE_API_BASE_URL = if ($env:ANDROID_API_BASE_URL) {
$env:ANDROID_API_BASE_URL
} else {
"http://192.168.11.14:18091"
}
Write-Host "Android 调试接口地址:$env:VITE_API_BASE_URL"
& pnpm run android:sync
# 真机调试固定用开发环境构建,环境变量与构建流程统一收在 android-sync.ps1
& pnpm run android:sync:dev
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
& pnpm exec cap open android
+26
View File
@@ -0,0 +1,26 @@
param(
[ValidateSet("dev", "prod")]
[string]$Mode = "prod"
)
$ErrorActionPreference = "Stop"
if ($Mode -eq "dev") {
# 调试包打局域网后端:真机访问不到 localhost,模拟器才用 10.0.2.2
$env:VITE_API_BASE_URL = if ($env:ANDROID_API_BASE_URL) {
$env:ANDROID_API_BASE_URL
} else {
"http://192.168.11.14:18091"
}
Write-Host "接口地址(开发):$env:VITE_API_BASE_URL"
} else {
# 生产包只能用 .env.production,父进程残留的同名变量会覆盖文件配置,先清掉
Remove-Item Env:VITE_API_BASE_URL -ErrorAction SilentlyContinue
Write-Host "接口地址(生产):取 .env.production"
}
& pnpm run build
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
& pnpm exec cap sync android
exit $LASTEXITCODE