/** * Android APK 一键打包(强制生产编译 + 产物地址校验)。 * * 步骤: * 1. `npm run build:app-plus` —— 生产编译。该命令内部已包含 `scripts/check-build-env.cjs` * 的产物校验(含内网地址或缺少生产域名会直接失败),因此不会把开发地址打进 APK; * 2. `android-offline/pack.ps1` —— 注入 App 资源/图标/启动图 + `gradlew assembleRelease`, * 产物输出到 `release/android/wms-pda_v{版本}_release.apk`。 * * 用法: * npm run build:apk # 前端编译 + 打包 * npm run build:apk -- -Clean # gradle 全量重编 * npm run build:apk -- -NoInject # 跳过资源注入(已注入过时用) * npm run build:apk -- -Timestamp # 产物名带时间戳 * npm run android:pack:raw # 跳过前端编译,直接用现有 dist/build/app-plus 打包 * * 说明: * - 发布上传(scp + latest.json)在 pack.ps1 内,且只有存在 `config/deploy.json` 时才会执行; * 本仓库不提供该文件,故默认不会上传。 * - 版本号唯一来源是根 `package.json` 的 `version`(X.Y.Z),`versionCode` 自动推导为 * X*1000000 + Y*1000 + Z。 */ const { spawnSync } = require('child_process') const fs = require('fs') const path = require('path') const ROOT = path.resolve(__dirname, '..') const APP_BUILD_DIR = path.join(ROOT, 'dist', 'build', 'app-plus') const log = (...args) => console.log('[build-apk]', ...args) function run(cmd, args) { log(`$ ${cmd} ${args.join(' ')}`) const res = spawnSync(cmd, args, { cwd: ROOT, stdio: 'inherit', env: process.env, shell: process.platform === 'win32', }) return res.status === 0 } // ---------- step 1: 生产编译(内部含产物地址校验) ---------- log('step 1/2: build:app-plus(生产编译 + 产物地址校验)...') if (!run('npm', ['run', 'build:app-plus'])) { console.error('[build-apk] ✗ build:app-plus 失败,终止') process.exit(1) } if (!fs.existsSync(APP_BUILD_DIR)) { console.error(`[build-apk] ✗ 未找到 App 构建产物: ${APP_BUILD_DIR}`) process.exit(1) } // ---------- step 2: 离线打包(参数透传) ---------- const extraArgs = process.argv.slice(2) const packArgs = ['run', 'android:pack:raw'] if (extraArgs.length) packArgs.push('--', ...extraArgs) log(`step 2/2: android:pack:raw${extraArgs.length ? ' ' + extraArgs.join(' ') : ''} ...`) if (!run('npm', packArgs)) { console.error('[build-apk] ✗ android:pack:raw 失败') process.exit(1) } log('✓ 打包完成,APK 见 release/android/')