Files
ykxmz/android/build_apk.ps1
T
xmz7891 be59a20036 2.1.5:修「Windows 7 装完报 dll 缺失」
用户报:学校的电脑是 Windows 7,装完提示缺 DLL。

== 根因不是漏打包,是 Python 版本越界 ==

    Python 3.8    -> 最后一个支持 Windows 7 的版本
    Python 3.9+   -> 只支持 Windows 8 及以上

Python 3.9 起用到了 api-ms-win-core-path-l1-1-0.dll 里的
PathCchCanonicalizeEx,而这个 DLL **Windows 7 上根本不存在**,
也没法用 api-set 转发(那套机制只在 Win8+ 生效)。

所以之前用 3.11 打出来的包,在 Win7 上必然报缺 DLL ——
**那个 DLL 不是"忘了放进包",是"放进去也没用"。**

顺带说明:包里那 47 个 api-ms-win-core-*.dll 转发库正是
Python 为兼容 Win7 才带上的 —— 上游本来就考虑过 Win7,
只是 3.11 这个版本号越界了。

== 改法 ==

1. **改用 Python 3.8 打包,而且选 32 位**
   32 位在 Win7 的 32 位和 64 位系统上都能跑,学校不用分两种发。
   (64 位的包在 32 位 Windows 上直接起不来。)

   依赖固定到 3.8 能用的版本:
       Pillow==9.5.0(最后一个支持 3.8 的)
       pyinstaller 6.x(保持 _internal 布局,和安装包/测试/卸载逻辑一致;
                       5.x 会把依赖摊在根目录,导致"_internal 在"那一项失败)

2. **打包脚本主动拦**
   windows\build_exe.ps1 打包前先校验 Python:
     * 找不到 3.8 -> 报错并给出安装命令
     * 版本不是 3.8 -> 直接 throw,而不是默默打一个别人装不上的包
     * 不是 32 位 -> 警告
   Python 从 YKXMZ_PY 环境变量或常见路径里找,不写死。

3. **新增 Win7 兼容校验** _build\check_win7_compat.py
   盯三件事:内嵌是不是 3.8 的运行库、**有没有**那个要命的 DLL、
   exe 是不是 32 位。打包时和回归套件(3a9 关)里都会跑,
   不过就拒绝出包。

== 顺手修的 ==

_build/test_installer_cycle.py 里写死了 ykxmz-setup-1.8.2.exe,
于是一直在**装一个不存在的旧包** -> 静默失败 -> 报出一堆
莫名其妙的失败("配对码实际是 xxx"、"版本显示 v1.8.2")。
改成自动找 dist 下版本号最新的包(按数字段排序,不能按字符串,
否则 1.9.0 会排到 2.1.4 前面)。

== 验证(用户要求:自己测一遍,没问题再上传)==

    全量回归 42 个阶段全 PASS(含新的 "Windows 7 兼容" 关)
    安装包完整周期 20/20
    Win7 校验:python38.dll / 无 api-ms-win-core-path / 32 位
    实际启动打包的 EXE:进程活着、TCP 监听、UDP 自动发现在听、
                        连接能建立(日志里有"设备接入")
    产物扫描:三件全部干净(无旧密码、无服务器 IP)
    提交前泄漏闸门通过

安装包体积 14.4 MB -> 9.0 MB。
2026-10-09 13:16:31 +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.1.5.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.1.5" `
--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