Files
ykxmz/android/build_apk.ps1
T
xmz7891 1d368979aa 2.2.0:手表端连上后先到「远控 / 静默更新被控端」选择页
== 用户要求 ==

"安卓那个逻辑也要改一下,就是你输入配对码后,进入一个页面,
 页面有两个按钮,第一个按钮是远控,第二个按钮是静默更新被控端"

== 做成什么样 ==

新增 choicePanel,连接成功后先进这一页:

    已连接「电脑名」
    电脑端 2.1.5 → 可更新到 2.2.0

    [        远控        ]   <- 第一个按钮
    [   静默更新被控端    ]   <- 第二个按钮

按钮顺序按用户说的来(远控在前)。

页面顶上显示电脑名和**电脑端当前版本** —— 用户之前要求
"手表端连接那个电脑端也可以查看电脑端目前所处的版本",
这里是最自然的展示位置(不弹框,只显示一行)。

为什么不让它直接进控制页:老版本的被控端可能缺功能或有 bug,
"先更新再远控"是更合理的顺序 —— 但不强制,给用户自己选。

== 一个刻意不改的地方 ==

自动切到中转服务器**恢复**时的 `showControlPanel()` **没动** ——
用户本来就在远控页,中途弹一层选择页会打断他。
测试里专门断言了这一点,免得以后有人"顺手统一一下"改坏。

== 顺手修的几个问题 ==

1. **忘了定义 lastDeviceName** —— 写选择页时凭直觉用了这个变量,
   编译报"找不到符号"。补上字段并在连接回调里赋值。
   (教训:写代码时要用的变量,先确认它存在。)

2. **泄漏闸门拦下我自己** —— 这两天写的一批服务器诊断/配置脚本里
   硬编码了服务器 IP(26 处,5 个文件)。规则是 IP 不能进任何
   被跟踪的文件。已全部改成占位符,真实值仍从
   _build/.lightsail_host(已 gitignore)读。

3. **verify_all.ps1 删掉「锁屏时记 SKIP」的逻辑** ——
   用户明确要求删。那个好意带来一个更糟的后果:锁屏时几关根本不跑,
   回归"全绿"了但关键测试其实一跳而过。
   **"因为环境不满足所以不测"比"测了发现失败"更危险。**
   删掉之后果然暴露出 3 个 FAIL —— 屏幕解锁后重跑全部通过,
   确认是环境问题而不是代码问题。

== 服务器这边(不在这份提交里,但同一天做的)==

  * nginx 按用户要求重排:
      IP(80) 任何路径 -> 404(没东西)
      域名 / -> 官网;/zhongzhuanzhan -> 中转站设置
      9100 -> 同样的内容(等 AWS 开防火墙)
  * 删掉早年 Cloudflare Tunnel 的 systemd 残留
  * **补上 ACME 挑战例外** —— 这是最要紧的一条:
    80 端口原来把所有请求都 301 到 https,包括
    /.well-known/acme-challenge/,**续期时会失败**。
    而且它是延迟爆发的(最长 90 天后证书到期才暴露)。
  * 清理 1Panel 安装包残留(223MB)

== 验证 ==

    全量回归 21 关全部 PASS(零 FAIL,桌面可用状态)
    Java 编译通过(android-34, --release 8)
    623+ Python 文件语法通过
    泄漏闸门通过
    全部产物重建,Win7 兼容校验通过
2026-10-10 17:13:24 +08:00

134 lines
5.2 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.
# 用 aapt2 + javac + d8 + zipalign + apksigner 手工构建 APK(不需要 Gradle)
# 用法: powershell -File android\build_apk.ps1
#
# 注意:刻意不用 $ErrorActionPreference="Stop"。
# 原生工具的告警(javac 的 deprecation note 等)会写 stderr,
# 在 Stop 模式下会被 PowerShell 当成致命错误中断构建。
# 改为每一步显式检查 $LASTEXITCODE。
$ErrorActionPreference = "Continue"
$android = $PSScriptRoot # LanRemote/android(本脚本所在目录)
$root = Split-Path -Parent $android # LanRemote
$tc = Join-Path $root "..\starklink\toolchain"
$jdk = Join-Path $tc "jdk17"
$sdk = Join-Path $tc "android-sdk"
$bt = Join-Path $sdk "build-tools\34.0.0"
$androidJar = Join-Path $sdk "platforms\android-34\android.jar"
$keystore = Join-Path $tc "debug.keystore"
$javac = Join-Path $jdk "bin\javac.exe"
$keytool = Join-Path $jdk "bin\keytool.exe"
$jar = Join-Path $jdk "bin\jar.exe"
$aapt2 = Join-Path $bt "aapt2.exe"
$zipalign = Join-Path $bt "zipalign.exe"
$apksigner= Join-Path $bt "apksigner.bat"
$d8 = Join-Path $bt "d8.bat"
$work = Join-Path $android "build"
$outDir = Join-Path $root "dist"
$apkName = "ykxmz-watch-2.2.0.apk"
function Step($text) { Write-Host "`n=== $text ===" -ForegroundColor Cyan }
# 前置检查
foreach ($tool in @($javac, $aapt2, $d8, $zipalign, $apksigner)) {
if (-not (Test-Path $tool)) { throw "缺少工具: $tool" }
}
if (-not (Test-Path $androidJar)) { throw "缺少 android.jar: $androidJar" }
if (-not (Test-Path $keystore)) { throw "缺少签名用 keystore: $keystore" }
$env:JAVA_HOME = $jdk
$env:PATH = "$(Join-Path $jdk 'bin');$env:PATH"
if (Test-Path $work) { Remove-Item -Recurse -Force $work }
New-Item -ItemType Directory -Force -Path $work | Out-Null
New-Item -ItemType Directory -Force -Path $outDir | Out-Null
# ---------------- 1. 编译资源 ----------------
Step "1/6 aapt2 compile 资源"
$compiled = Join-Path $work "res.zip"
& $aapt2 compile --dir (Join-Path $android "res") -o $compiled
if ($LASTEXITCODE -ne 0) { throw "aapt2 compile 失败" }
# ---------------- 2. 链接资源 + 生成 R.java ----------------
Step "2/6 aapt2 link 生成资源表与 R.java"
$genDir = Join-Path $work "gen"
New-Item -ItemType Directory -Force -Path $genDir | Out-Null
$unsigned = Join-Path $work "app-unsigned.apk"
& $aapt2 link `
-o $unsigned `
-I $androidJar `
--manifest (Join-Path $android "AndroidManifest.xml") `
-R $compiled `
--java $genDir `
--min-sdk-version 24 `
--target-sdk-version 34 `
--version-code 5 `
--version-name "2.2.0" `
--auto-add-overlay
if ($LASTEXITCODE -ne 0) { throw "aapt2 link 失败" }
# ---------------- 3. 编译 Java ----------------
Step "3/6 javac 编译 Java 源码"
$classesDir = Join-Path $work "classes"
New-Item -ItemType Directory -Force -Path $classesDir | Out-Null
$javaSources = @()
$javaSources += Get-ChildItem -Recurse -Filter *.java -Path (Join-Path $android "src") | ForEach-Object { $_.FullName }
$javaSources += Get-ChildItem -Recurse -Filter R.java -Path $genDir -ErrorAction SilentlyContinue | ForEach-Object { $_.FullName }
if ($javaSources.Count -eq 0) { throw "没有找到 Java 源文件" }
Write-Host "源文件 $($javaSources.Count) 个"
& $javac -source 8 -target 8 -nowarn -Xlint:none -encoding UTF-8 `
-bootclasspath $androidJar `
-d $classesDir `
$javaSources 2>&1 | Out-String | Write-Host
if ($LASTEXITCODE -ne 0) { throw "javac 编译失败" }
# ---------------- 4. d8 转 dex ----------------
Step "4/6 d8 转换为 classes.dex"
$classesJar = Join-Path $work "classes.jar"
& $jar cf $classesJar -C $classesDir .
if ($LASTEXITCODE -ne 0) { throw "打包 classes.jar 失败" }
$dexDir = Join-Path $work "dex"
New-Item -ItemType Directory -Force -Path $dexDir | Out-Null
& $d8 --lib $androidJar --min-api 24 --output $dexDir $classesJar 2>&1 | Out-String | Write-Host
if ($LASTEXITCODE -ne 0) { throw "d8 失败" }
$dex = Join-Path $dexDir "classes.dex"
if (-not (Test-Path $dex)) { throw "未生成 classes.dex" }
Write-Host ("classes.dex 大小 {0:N1} KB" -f ((Get-Item $dex).Length / 1KB))
# ---------------- 5. 组装 APK ----------------
Step "5/6 组装 APK"
& python (Join-Path $root "_build\pack_apk.py") $unsigned $dex
if ($LASTEXITCODE -ne 0) { throw "写入 classes.dex 失败" }
$aligned = Join-Path $work "app-aligned.apk"
& $zipalign -f 4 $unsigned $aligned
if ($LASTEXITCODE -ne 0) { throw "zipalign 失败" }
# ---------------- 6. 签名 ----------------
Step "6/6 apksigner 签名"
$final = Join-Path $outDir $apkName
if (Test-Path $final) { Remove-Item $final -Force }
& $apksigner sign `
--ks $keystore `
--ks-pass pass:android `
--key-pass pass:android `
--ks-key-alias androiddebugkey `
--v1-signing-enabled true `
--v2-signing-enabled true `
--out $final `
$aligned
if ($LASTEXITCODE -ne 0) { throw "签名失败" }
Step "验证签名"
& $apksigner verify --print-certs $final
if ($LASTEXITCODE -ne 0) { throw "签名校验失败" }
Step "构建完成"
Write-Host ("APK: {0}" -f $final) -ForegroundColor Green
Write-Host ("大小: {0:N2} MB" -f ((Get-Item $final).Length / 1MB)) -ForegroundColor Green