liufumingren 228723e0c3 设备端扫码上报 + 桌面端/Android 正式包发布脚手架
AndroidStation(原中控改造为纯设备端)
- 只做三件事:UDP 48899 被桌面端发现、TCP 8087 推送 USB 扫码结果、离线语音播报
- 扫码不再经过 UI:去掉逐键重绘,50ms 自动提交,同一码重复扫码也一律上报
- 离线语音种子(数字/字母/“开始打包”/欢迎语)随包安装,MediaPlayer 播放,启动把音量拉满
- 启动播报「欢迎使用溯源系统,初始化已经完成,扫码即可开始打包」
- 移除 Device Owner / 设备管理员 / lock task / 网络与 IP 设置等非普通应用能力(仅保留开机自启)
- 正式签名 keystore + release 签名配置,versionName/versionCode 可由打包脚本覆盖

发布脚手架
- backend/android_client:Android 正式包构建(build.ps1 + package.config.json)
- backend/scripts/deploy/publish-android.cjs + npm run android:build / android:publish
- 桌面端安装包改为 download/desktop,Android 为 download/android_client;
  version.json 仍走 /download/version.json(老客户端写死),新增 /download/android_client/version.json
- 谷歌插件随桌面端 MSI 安装到 <安装目录>\extension,工具菜单新增「复制谷歌插件地址」
- git-safety-check 增加仓库级禁区(keystore.properties / *.jks / local.properties)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 05:53:27 +08:00

251 lines
9.7 KiB
PowerShell
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.

#requires -Version 5.1
<#
.SYNOPSIS
打「AndroidStation 设备端」的正式签名 APK,并生成下载用的 version.json。
.DESCRIPTION
1. 按 -Environment 从 package.config.json 取该环境的下载域名 / 目标目录;
2. gradlew assembleRelease:版本号用 -PversionName/-PversionCode 覆盖,
签名读 AndroidStation\keystore.properties(正式 keystore,不入库);
3. 算 MD5 → 写 version.json(version / versionCode / downloadUrl / md5 / size / publishedAt / buildId);
4. APK 复制到 out\;copyToServer=true 时再复制到后端:
APK → app\public\download\android_client\(nginx 长缓存)
version.json → app\public\version\android_client\(由 controller/download.js 关缓存后提供)
5. out\ 与后端目录各只保留最近 keepVersions(默认 5)个安装包。
.PARAMETER Environment
Development(连本机 7300)/ Production(正式域名)。
.PARAMETER Version
版本号 x.y.z(必填;发布脚本会按已有包自动 +1 补丁号)。
.PARAMETER VersionCode
Android versionCode(默认由 x.y.z 换算:major*10000 + minor*100 + patch)。
.PARAMETER ServerBaseUrl
覆盖该环境配置里的域名(生成下载地址用,APK 本体不写死域名)。
.PARAMETER Keep
保留多少个历史安装包(默认读 package.config.json 的 keepVersions = 5)。
.PARAMETER ReleaseNotes
写进 version.json 的更新说明。
.EXAMPLE
.\build.ps1 -Environment Production -Version 1.0.1 -ReleaseNotes "扫码上报更稳"
.NOTES
前置:JDK 17、Android SDK(AndroidStation\local.properties 的 sdk.dir 或 ANDROID_HOME)、
AndroidStation\keystore.properties + keystore\*.jks(正式签名)。
本文件必须保存为 UTF-8 with BOM —— PowerShell 5.1 读无 BOM 脚本会按 GBK 解析,中文注释直接语法报错。
#>
param(
[ValidateSet('Development', 'Production')]
[string]$Environment = 'Development',
[string]$Version,
[int]$VersionCode = 0,
[string]$ServerBaseUrl,
[int]$Keep = 0,
[string]$ReleaseNotes = ''
)
$ErrorActionPreference = 'Stop'
function Read-Utf8([string]$p) {
return [System.IO.File]::ReadAllText($p, (New-Object System.Text.UTF8Encoding($false)))
}
function Write-Utf8NoBom([string]$p, [string]$text) {
[System.IO.File]::WriteAllText($p, $text, (New-Object System.Text.UTF8Encoding($false)))
}
# ---------------- 1. 配置 ----------------
$configPath = Join-Path $PSScriptRoot 'package.config.json'
$cfg = Read-Utf8 $configPath | ConvertFrom-Json
$envProp = $cfg.environments.PSObject.Properties[$Environment]
if (-not $envProp) { throw "package.config.json 里没有环境:$Environment" }
$envCfg = $envProp.Value
if (-not $ServerBaseUrl) { $ServerBaseUrl = $envCfg.serverBaseUrl }
if (-not $ServerBaseUrl) { throw "环境 $Environment 没配 serverBaseUrl" }
$ServerBaseUrl = $ServerBaseUrl.TrimEnd('/')
if ($Keep -le 0) { $Keep = [int]$cfg.keepVersions }
if ($Keep -le 0) { $Keep = 5 }
$projectDir = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot $envCfg.projectDir))
$outDir = Join-Path $PSScriptRoot 'out'
$gradlew = Join-Path $projectDir 'gradlew.bat'
if (-not (Test-Path $gradlew)) { throw "找不到 gradlew.bat:$gradlew(检查 package.config.json 的 projectDir)" }
# ---------------- 2. 版本 ----------------
if (-not $Version) { throw "请用 -Version x.y.z 指定版本号(npm run android:publish 会自动带上)" }
$Version = $Version.Trim().TrimStart('v', 'V')
$null = [version]$Version # 提前校验格式(不合法直接报错)
$parts = $Version.Split('.')
$major = [int]$parts[0]
$minor = 0
if ($parts.Length -gt 1) { $minor = [int]$parts[1] }
$patch = 0
if ($parts.Length -gt 2) { $patch = [int]$parts[2] }
if ($VersionCode -le 0) { $VersionCode = $major * 10000 + $minor * 100 + $patch }
Write-Host "环境:$Environment"
Write-Host "版本:$Version(versionCode $VersionCode)"
Write-Host "工程:$projectDir"
# ---------------- 3. 环境检查 ----------------
$keystoreProps = Join-Path $projectDir 'keystore.properties'
if (-not (Test-Path $keystoreProps)) {
throw "缺少正式签名配置:$keystoreProps`n(keystore.properties 与 keystore/*.jks 不入库,发布机需自行备份)"
}
$localProps = Join-Path $projectDir 'local.properties'
if (-not (Test-Path $localProps) -and -not $env:ANDROID_HOME -and -not $env:ANDROID_SDK_ROOT) {
throw "找不到 Android SDK:请写 $localProps 的 sdk.dir 或设置 ANDROID_HOME"
}
$javaHome = $env:JAVA_HOME
if (-not $javaHome -or -not (Test-Path (Join-Path $javaHome 'bin\java.exe'))) {
$roots = @(
'C:\Program Files\Android\Android Studio\jbr',
(Join-Path $env:LOCALAPPDATA 'Programs\EclipseAdoptium'),
'C:\Program Files\Eclipse Adoptium',
'C:\Program Files\Java'
)
$javaHome = $null
foreach ($root in $roots) {
if (-not (Test-Path $root)) { continue }
if (Test-Path (Join-Path $root 'bin\java.exe')) { $javaHome = $root; break }
$child = Get-ChildItem -Path $root -Directory -ErrorAction SilentlyContinue |
Where-Object { Test-Path (Join-Path $_.FullName 'bin\java.exe') } |
Sort-Object Name -Descending | Select-Object -First 1
if ($child) { $javaHome = $child.FullName; break }
}
}
if (-not $javaHome) { throw '找不到 JDK:请安装 JDK 17 并设置 JAVA_HOME。' }
$env:JAVA_HOME = $javaHome
Write-Host "JAVA_HOME=$javaHome"
# ---------------- 4. 构建 APK ----------------
Push-Location $projectDir
try {
& $gradlew --no-daemon assembleRelease "-PversionName=$Version" "-PversionCode=$VersionCode"
if ($LASTEXITCODE -ne 0) { throw "gradlew assembleRelease 失败($LASTEXITCODE)" }
}
finally {
Pop-Location
}
$builtApk = Join-Path $projectDir 'app\build\outputs\apk\release\app-release.apk'
if (-not (Test-Path $builtApk)) { throw "构建产物不存在:$builtApk" }
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
$apkName = [string]$envCfg.apkName
if (-not $apkName) { $apkName = 'YuncangScan-{version}.apk' }
$apkName = $apkName.Replace('{version}', $Version)
$apk = Join-Path $outDir $apkName
Copy-Item -Path $builtApk -Destination $apk -Force
# ---------------- 5. version.json ----------------
$hash = (Get-FileHash -Algorithm MD5 -Path $apk).Hash.ToLowerInvariant()
$size = (Get-Item $apk).Length
$downloadUrl = "$ServerBaseUrl/download/android_client/$apkName"
$now = Get-Date
$versionJson = [ordered]@{
version = $Version
versionCode = $VersionCode
package = 'com.gztbyc.scancenter'
downloadUrl = $downloadUrl
md5 = $hash
size = $size
publishedAt = $now.ToString('yyyy-MM-ddTHH:mm:sszzz')
buildId = $now.ToString('yyyyMMddHHmmss')
environment = $Environment
releaseNotes = $ReleaseNotes
} | ConvertTo-Json
$versionFile = Join-Path $outDir 'version.json'
Write-Utf8NoBom $versionFile $versionJson
Write-Host "APK:$apk($([math]::Round($size / 1MB, 1)) MB)"
Write-Host "MD5:$hash"
Write-Host "下载地址:$downloadUrl"
# ---------------- 6. 复制到后端 ----------------
$skipVersionJson = $false
$downloadDir = $null
$versionDir = $null
if ($envCfg.copyToServer -ne $false) {
$downloadDir = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot $envCfg.downloadFolder))
$versionDir = [System.IO.Path]::GetFullPath((Join-Path $PSScriptRoot $envCfg.versionFolder))
New-Item -ItemType Directory -Force -Path $downloadDir | Out-Null
New-Item -ItemType Directory -Force -Path $versionDir | Out-Null
# 后端已经是更新版本:说明这次在补打旧版本,不能把 version.json 降回去
$existingVj = Join-Path $versionDir 'version.json'
if (Test-Path $existingVj) {
try {
$old = (Read-Utf8 $existingVj) | ConvertFrom-Json
if ($old.version -and ([version]$old.version -gt [version]$Version)) {
$skipVersionJson = $true
Write-Warning "后端 version.json 已是更新版本($($old.version)),本次不覆盖它"
}
}
catch { /* 旧文件坏了就当没有 */ }
}
$dstApk = Join-Path $downloadDir $apkName
Copy-Item -Path $apk -Destination $dstApk -Force
# 刷新 mtime:即使上游还有一层协商缓存,也能认出文件变了
(Get-Item $dstApk).LastWriteTime = $now
if (-not $skipVersionJson) {
Write-Utf8NoBom $existingVj $versionJson
(Get-Item $existingVj).LastWriteTime = $now
}
Write-Host "已复制到:$downloadDir(APK)、$versionDir(version.json)"
}
else {
Write-Host "(该环境 copyToServer=false,跳过复制到后端)"
}
# ---------------- 7. 只保留最近 N 个安装包 ----------------
function Remove-OldPackages([string]$dir, [int]$keep) {
if (-not (Test-Path $dir)) { return }
$items = @(
Get-ChildItem -Path $dir -Filter 'YuncangScan-*.apk' -File | ForEach-Object {
if ($_.BaseName -match 'YuncangScan-(\d+(\.\d+){1,3})$') {
[pscustomobject]@{ File = $_; Ver = [version]$Matches[1] }
}
} | Sort-Object Ver -Descending
)
$removed = 0
$items | Select-Object -Skip $keep | ForEach-Object {
Remove-Item $_.File.FullName -Force
$removed++
Write-Host " 清理旧包:$($_.File.Name)"
}
if ($removed -eq 0) { Write-Host " $dir:共 $($items.Count) 个,未超过 $keep 个" }
}
Write-Host "保留最近 $Keep 个安装包:"
Remove-OldPackages $outDir $Keep
if ($downloadDir) { Remove-OldPackages $downloadDir $Keep }
# ---------------- 8. 摘要 ----------------
Write-Host ""
Write-Host "完成:$apk" -ForegroundColor Green
Write-Host "版本文件:$versionFile"
Write-Host "下载地址:$downloadUrl"
Write-Host "安装:adb install -r `"$apk`"(设备上无需任何特殊权限)"