WMS_PDA/scripts/build-apk.cjs
liufumingren 06d58a5a71 chore(apk): 移植 Android 离线打包流水线(命令行出 APK,无需 HBuilderX)
- android-offline/:DCloud 离线集成工程(simpleDemo/libs 含 5+ Runtime aar)+ pack.ps1 + prepare.cjs
- 包名 com.gztbyc.pda_wms、appId __UNI__0B3DAD3、BootReceiver 迁到新包名目录
- AndroidManifest 补齐权限(与 src/manifest.json 对齐)、新增蓝牙权限与 usesCleartextTraffic(生产接口为 http)
- prepare.cjs 默认资源目录改为 dist/build/app-plus,注入到 assets/apps/__UNI__0B3DAD3/www 并对齐 compilerVersion
- config/:android.example.json 模板 + README(appKey 与 appId/包名/证书指纹绑定说明);android.json 与 keystore 均不提交
- scripts/build-apk.cjs + npm run build:apk / android:pack:raw:生产编译(含地址校验)→ gradle 出包到 release/android
- .gitignore 忽略 keystore、config/android.json、SDK、生成的 www 与 release
2026-10-02 21:12:15 +08:00

64 lines
2.6 KiB
JavaScript
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.

/**
* 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/')