build: android:apk build prod release

This commit is contained in:
Timi
2026-11-18 11:26:45 +08:00
parent 43a64acc12
commit 9ee957419f
7 changed files with 128 additions and 6 deletions
+1
View File
@@ -154,3 +154,4 @@ release-note.txt
# Android Studio 本地配置及发布构建产物 # Android Studio 本地配置及发布构建产物
/android/.idea/ /android/.idea/
/android/app/release/ /android/app/release/
/android/keystore.properties
+9 -4
View File
@@ -45,10 +45,15 @@ grep versionCode android/app/build.gradle
写进 `dist` 和 `android/app/src/main/assets/public`,直接拿去打包就会打出一个连内网的正式包 写进 `dist` 和 `android/app/src/main/assets/public`,直接拿去打包就会打出一个连内网的正式包
- 打完核对:`aapt2 dump badging` 的 versionCode、`apksigner verify --print-certs` 的证书, - 打完核对:`aapt2 dump badging` 的 versionCode、`apksigner verify --print-certs` 的证书,
以及解包 grep 接口地址(内网地址必须为 0 次) 以及解包 grep 接口地址(内网地址必须为 0 次)
- 用 Android Studio 打包,**签名必须与线上包一致**:线上 1.0.0 用的是 - **一条命令出正式包**:`pnpm run android:apk`
`twa/android.keystore`(alias `gao-customer`,证书 `CN=yeyu` / SHA-256 `3E:9C:CC:8A:…`)。 = `android:sync:prod` + `assembleRelease` + 用 `twa/android.keystore` 签名 + 复制到
签名不一致时系统会拒绝覆盖安装,老用户只能卸载重装 `android/app/release/app-release.apk`(已 gitignore),末尾会打印 versionCode、大小、
- `pnpm run android:apk` 走的是 `assembleDebug`,签的是本机 debug key,**不能用于发版** 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 的兼容基线 ### 3. 提高 CI 的兼容基线
+21
View File
@@ -1,5 +1,13 @@
apply plugin: 'com.android.application' 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 { android {
namespace = "com.imyeyu.gao.customer" namespace = "com.imyeyu.gao.customer"
compileSdk = rootProject.ext.compileSdkVersion compileSdk = rootProject.ext.compileSdkVersion
@@ -16,10 +24,23 @@ android {
ignoreAssetsPattern = '!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~' 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 { buildTypes {
release { release {
minifyEnabled false minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
if (keystorePropertiesFile.exists()) {
signingConfig signingConfigs.release
}
} }
} }
} }
+6
View File
@@ -0,0 +1,6 @@
# 发版签名凭证(复制为 android/keystore.properties 后填写,该文件已 gitignore)
# storeFile 相对于 android/ 目录
storeFile=../twa/android.keystore
keyAlias=gao-customer
storePassword=
keyPassword=
+1 -1
View File
@@ -18,7 +18,7 @@
"android:sync:prod": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/android-sync.ps1 -Mode prod", "android:sync:prod": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/android-sync.ps1 -Mode prod",
"android:sync": "pnpm run android:sync:prod", "android:sync": "pnpm run android:sync:prod",
"android:open": "powershell -NoProfile -ExecutionPolicy Bypass -File scripts/android-open.ps1", "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": { "dependencies": {
"@capacitor/android": "^8", "@capacitor/android": "^8",
+89
View File
@@ -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())"
}
}
}
+1 -1
View File
@@ -10,7 +10,7 @@ if ($Mode -eq "dev") {
$env:VITE_API_BASE_URL = if ($env:ANDROID_API_BASE_URL) { $env:VITE_API_BASE_URL = if ($env:ANDROID_API_BASE_URL) {
$env:ANDROID_API_BASE_URL $env:ANDROID_API_BASE_URL
} else { } else {
"http://192.168.11.14:18091" "http://192.168.11.18:18091"
} }
Write-Host "接口地址(开发):$env:VITE_API_BASE_URL" Write-Host "接口地址(开发):$env:VITE_API_BASE_URL"
} else { } else {