From 9ee957419f7d3231639907753202b782b8ad3c96 Mon Sep 17 00:00:00 2001 From: Timi Date: Wed, 18 Nov 2026 11:26:45 +0800 Subject: [PATCH] build: android:apk build prod release --- .gitignore | 1 + NATIVE_UPGRADE.md | 13 +++-- android/app/build.gradle | 21 +++++++ android/keystore.properties.example | 6 ++ package.json | 2 +- scripts/android-apk.ps1 | 89 +++++++++++++++++++++++++++++ scripts/android-sync.ps1 | 2 +- 7 files changed, 128 insertions(+), 6 deletions(-) create mode 100644 android/keystore.properties.example create mode 100644 scripts/android-apk.ps1 diff --git a/.gitignore b/.gitignore index 7ba21cc..8d317c5 100644 --- a/.gitignore +++ b/.gitignore @@ -154,3 +154,4 @@ release-note.txt # Android Studio 本地配置及发布构建产物 /android/.idea/ /android/app/release/ +/android/keystore.properties diff --git a/NATIVE_UPGRADE.md b/NATIVE_UPGRADE.md index 8f23739..351596f 100644 --- a/NATIVE_UPGRADE.md +++ b/NATIVE_UPGRADE.md @@ -45,10 +45,15 @@ grep versionCode android/app/build.gradle 写进 `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:…`)。 - 签名不一致时系统会拒绝覆盖安装,老用户只能卸载重装 -- `pnpm run android:apk` 走的是 `assembleDebug`,签的是本机 debug key,**不能用于发版** +- **一条命令出正式包**:`pnpm run android:apk` + = `android:sync:prod` + `assembleRelease` + 用 `twa/android.keystore` 签名 + 复制到 + `android/app/release/app-release.apk`(已 gitignore),末尾会打印 versionCode、大小、 + sha256 和证书指纹 +- 签名凭证放在 `android/keystore.properties`(已 gitignore),格式见同目录 + `keystore.properties.example`;缺密码时脚本会直接拦住,不会产出 debug 包 +- 只重新打包、不重建 web:`pnpm run android:apk -- -SkipSync` +- 仍然可以用 Android Studio 打包,但**签名必须是 `twa/android.keystore`** + (alias `gao-customer`,`CN=yeyu` / SHA-256 `3E:9C:CC:8A:…`),否则系统拒绝覆盖安装 ### 3. 提高 CI 的兼容基线 diff --git a/android/app/build.gradle b/android/app/build.gradle index f5f1e69..124fd1a 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -1,5 +1,13 @@ apply plugin: 'com.android.application' +// 发版签名凭证放在 android/keystore.properties(已 gitignore),不进版本库。 +// 缺失时 assembleRelease 会产出未签名包,脚本会拦住并提示补全。 +def keystorePropertiesFile = rootProject.file("keystore.properties") +def keystoreProperties = new Properties() +if (keystorePropertiesFile.exists()) { + keystorePropertiesFile.withInputStream { keystoreProperties.load(it) } +} + android { namespace = "com.imyeyu.gao.customer" compileSdk = rootProject.ext.compileSdkVersion @@ -16,10 +24,23 @@ android { ignoreAssetsPattern = '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~' } } + signingConfigs { + release { + if (keystorePropertiesFile.exists()) { + storeFile rootProject.file(keystoreProperties['storeFile']) + storePassword keystoreProperties['storePassword'] + keyAlias keystoreProperties['keyAlias'] + keyPassword keystoreProperties['keyPassword'] + } + } + } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' + if (keystorePropertiesFile.exists()) { + signingConfig signingConfigs.release + } } } } diff --git a/android/keystore.properties.example b/android/keystore.properties.example new file mode 100644 index 0000000..20e23c3 --- /dev/null +++ b/android/keystore.properties.example @@ -0,0 +1,6 @@ +# 发版签名凭证(复制为 android/keystore.properties 后填写,该文件已 gitignore) +# storeFile 相对于 android/ 目录 +storeFile=../twa/android.keystore +keyAlias=gao-customer +storePassword= +keyPassword= diff --git a/package.json b/package.json index bc468e6..cd419c1 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "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:prod && cd android && gradlew.bat assembleDebug" + "android:apk": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/android-apk.ps1" }, "dependencies": { "@capacitor/android": "^8", diff --git a/scripts/android-apk.ps1 b/scripts/android-apk.ps1 new file mode 100644 index 0000000..ad570ae --- /dev/null +++ b/scripts/android-apk.ps1 @@ -0,0 +1,89 @@ +param( + [switch]$SkipSync +) + +$ErrorActionPreference = "Stop" + +$root = Split-Path -Parent $PSScriptRoot +$propertiesPath = Join-Path $root "android\keystore.properties" + +if (-not (Test-Path $propertiesPath)) { + Write-Host "缺少 android\keystore.properties:发版包必须用 twa\android.keystore 签名" -ForegroundColor Red + Write-Host "照着同目录的 keystore.properties.example 补全后重试" + exit 1 +} + +$properties = @{} +Get-Content $propertiesPath | ForEach-Object { + $line = $_.Trim() + if ($line -and -not $line.StartsWith("#")) { + $index = $line.IndexOf("=") + if (0 -lt $index) { + $properties[$line.Substring(0, $index).Trim()] = $line.Substring($index + 1).Trim() + } + } +} + +if (-not $properties["storePassword"] -or -not $properties["keyPassword"]) { + Write-Host "android\keystore.properties 里的 storePassword / keyPassword 还没填" -ForegroundColor Red + exit 1 +} + +$storeFile = [System.IO.Path]::GetFullPath((Join-Path (Join-Path $root "android") $properties["storeFile"])) +if (-not (Test-Path $storeFile)) { + Write-Host "找不到签名文件:$storeFile" -ForegroundColor Red + exit 1 +} + +if (-not $SkipSync) { + & pnpm run android:sync:prod + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } +} + +Push-Location (Join-Path $root "android") +try { + & .\gradlew.bat :app:assembleRelease --console=plain + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } +} finally { + Pop-Location +} + +$outputDir = Join-Path $root "android\app\build\outputs\apk\release" +$apk = Get-ChildItem $outputDir -Filter "app-release*.apk" -ErrorAction SilentlyContinue | + Where-Object { -not $_.Name.Contains("unsigned") } | + Select-Object -First 1 +if (-not $apk) { + Write-Host "没有产出已签名的 release 包,检查 android\app\build.gradle 的 signingConfigs" -ForegroundColor Red + exit 1 +} + +$targetDir = Join-Path $root "android\app\release" +New-Item -ItemType Directory -Force -Path $targetDir | Out-Null +$target = Join-Path $targetDir "app-release.apk" +Copy-Item $apk.FullName $target -Force + +$gradle = Get-Content (Join-Path $root "android\app\build.gradle") -Raw +$versionCode = [regex]::Match($gradle, 'versionCode\s+(\d+)').Groups[1].Value +$versionName = [regex]::Match($gradle, 'versionName\s+"([^"]+)"').Groups[1].Value +$file = Get-Item $target + +Write-Host "" +Write-Host "APK android\app\release\app-release.apk" +Write-Host "version $versionName ($versionCode)" +Write-Host "size $($file.Length)" +Write-Host "sha256 $((Get-FileHash $target -Algorithm SHA256).Hash.ToLower())" + +# 顺手把签名证书打出来,对不上 3E:9C:CC:8A 就说明钥匙用错了 +$sdk = $env:ANDROID_HOME +if (-not $sdk) { $sdk = $env:ANDROID_SDK_ROOT } +if (-not $sdk) { $sdk = Join-Path $env:LOCALAPPDATA "Android\Sdk" } +$buildTools = Join-Path $sdk "build-tools" +if (Test-Path $buildTools) { + $latest = Get-ChildItem $buildTools -Directory | Sort-Object Name | Select-Object -Last 1 + $signer = Join-Path $latest.FullName "apksigner.bat" + if (Test-Path $signer) { + & $signer verify --print-certs $target | Select-String "SHA-256 digest" | ForEach-Object { + Write-Host "cert $($_.Line.Trim())" + } + } +} diff --git a/scripts/android-sync.ps1 b/scripts/android-sync.ps1 index 9b5c70c..87a3a79 100644 --- a/scripts/android-sync.ps1 +++ b/scripts/android-sync.ps1 @@ -10,7 +10,7 @@ if ($Mode -eq "dev") { $env:VITE_API_BASE_URL = if ($env:ANDROID_API_BASE_URL) { $env:ANDROID_API_BASE_URL } else { - "http://192.168.11.14:18091" + "http://192.168.11.18:18091" } Write-Host "接口地址(开发):$env:VITE_API_BASE_URL" } else {