Files
gao-customer-h5/scripts/android-apk.ps1
T
2026-11-18 11:39:22 +08:00

90 lines
3.0 KiB
PowerShell

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:发版包必须用根目录 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())"
}
}
}