- package.json 与 src/manifest.json 版本同步为 1.0.122 / versionCode 1000122 - prepare.cjs 增加版本一致性校验(两处不一致时告警) - 文档补充:版本号单一来源与云打包 versionCode 差异、图标/启动图来源、appKey 与证书指纹匹配
179 lines
7.5 KiB
JavaScript
179 lines
7.5 KiB
JavaScript
/**
|
||
* Android 离线打包准备脚本
|
||
* 职责:校验 config/android.json、拷贝 App 资源(www)、覆盖图标/启动图。
|
||
* 包名/版本/签名/应用名/appid/appKey 等由 simpleDemo/build.gradle 在构建时动态读取
|
||
* config/android.json 与 package.json,本脚本不再写入工程文件。
|
||
*
|
||
* 用法:
|
||
* node android-offline/prepare.cjs
|
||
* node android-offline/prepare.cjs <wwwSource> # 可选:覆盖 www 资源目录
|
||
*/
|
||
const fs = require('fs')
|
||
const path = require('path')
|
||
|
||
const ROOT = path.resolve(__dirname, '..')
|
||
const OFFLINE_DIR = __dirname
|
||
const CONFIG_PATH = path.join(ROOT, 'config', 'android.json')
|
||
|
||
const log = (...args) => console.log('[prepare]', ...args)
|
||
const warn = (...args) => console.warn('[prepare][警告]', ...args)
|
||
|
||
// ---------- 1. 读取配置 ----------
|
||
let config
|
||
try {
|
||
config = JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8'))
|
||
} catch (error) {
|
||
if (error.code === 'ENOENT') {
|
||
console.error(`[prepare][错误] 未找到配置文件: ${CONFIG_PATH}`)
|
||
console.error('请复制 config/android.example.json 为 config/android.json 并填写(appKey、signing 等),详见 config/README.md')
|
||
} else {
|
||
console.error(`[prepare][错误] 配置文件解析失败 ${CONFIG_PATH}: ${error.message}`)
|
||
}
|
||
process.exit(1)
|
||
}
|
||
|
||
const required = ['appId', 'appName', 'packageName']
|
||
for (const key of required) {
|
||
if (!config[key]) {
|
||
console.error(`[prepare][错误] config.json 缺少必填项: ${key}`)
|
||
process.exit(1)
|
||
}
|
||
}
|
||
|
||
// ---------- 1.5 版本一致性校验 ----------
|
||
// 离线包的 versionName/versionCode 由 simpleDemo/build.gradle 从 package.json 推导
|
||
// (versionCode = X*1000000 + Y*1000 + Z);而 src/manifest.json 的 versionName/versionCode
|
||
// 是 HBuilderX 云打包使用的值。两者必须一致,否则同一版本会出现两个 APK 版本号,
|
||
// 应用内更新提示、版本排查都会对不上号。
|
||
// 注:编译产物 www 内不含版本字段(实测),APK 版本完全由原生工程决定,故此处只做一致性校验。
|
||
try {
|
||
const pkgVersion = JSON.parse(fs.readFileSync(path.join(ROOT, 'package.json'), 'utf-8')).version
|
||
const [x, y, z] = String(pkgVersion).split('.').map((n) => parseInt(n, 10))
|
||
const expectedCode = x * 1000000 + y * 1000 + z
|
||
|
||
const manifestText = fs.readFileSync(path.join(ROOT, 'src', 'manifest.json'), 'utf-8')
|
||
const mVersion = (manifestText.match(/"versionName"\s*:\s*"([^"]*)"/) || [])[1]
|
||
const mCode = Number((manifestText.match(/"versionCode"\s*:\s*(\d+)/) || [])[1])
|
||
|
||
log(`版本: package.json=${pkgVersion} (versionCode ${expectedCode}) | src/manifest.json=${mVersion} (versionCode ${mCode})`)
|
||
|
||
if (mVersion !== pkgVersion || mCode !== expectedCode) {
|
||
warn('package.json 与 src/manifest.json 版本号不一致,请两处同步(否则云打包/离线包会出现不同版本号)')
|
||
}
|
||
} catch (error) {
|
||
warn(`版本一致性校验已跳过: ${error.message}`)
|
||
}
|
||
|
||
// ---------- 2. 定位 SDK 工程 ----------
|
||
const AS_DIR = path.join(OFFLINE_DIR, 'HBuilder-Integrate-AS')
|
||
const DEMO_DIR = path.join(AS_DIR, 'simpleDemo')
|
||
|
||
if (!fs.existsSync(AS_DIR)) {
|
||
console.error('[prepare][错误] 未找到离线 SDK 工程: ' + AS_DIR)
|
||
console.error('请先下载离线 SDK 并解压到 android-offline/ 目录,详见 README.md')
|
||
process.exit(1)
|
||
}
|
||
if (!fs.existsSync(DEMO_DIR)) {
|
||
console.error('[prepare][错误] 未找到 simpleDemo 工程: ' + DEMO_DIR)
|
||
process.exit(1)
|
||
}
|
||
|
||
let failed = false
|
||
|
||
// ---------- 3. 签名配置预检(Gradle 构建时从 config 动态读取) ----------
|
||
if (!config.signing || !config.signing.keystore) {
|
||
warn('config/android.json 中 signing.keystore 为空,Gradle 构建时将因缺少签名配置而失败')
|
||
} else if (!fs.existsSync(path.resolve(ROOT, config.signing.keystore))) {
|
||
warn(`签名文件不存在: ${path.resolve(ROOT, config.signing.keystore)}`)
|
||
}
|
||
if (!config.appKey) {
|
||
warn('config/android.json 中 appKey 为空,请到 https://dev.dcloud.net.cn 申请后填写')
|
||
}
|
||
|
||
// ---------- 4. 拷贝 App 资源到 assets/apps/{appId}/www ----------
|
||
const wwwTarget = path.join(DEMO_DIR, 'src', 'main', 'assets', 'apps', config.appId, 'www')
|
||
|
||
// 可选外部 www 源覆盖:node prepare.cjs <wwwSource>;其次用 config.android.json 的 wwwSource
|
||
const wwwOverride = process.argv[2] ? path.resolve(ROOT, process.argv[2]) : ''
|
||
|
||
const candidates = [
|
||
...(wwwOverride ? [wwwOverride] : []),
|
||
...(config.wwwSource ? [path.resolve(ROOT, config.wwwSource)] : []),
|
||
path.join(ROOT, 'unpackage', 'resources', config.appId, 'www'),
|
||
path.join(ROOT, 'dist', 'build', 'app-plus'),
|
||
]
|
||
|
||
const wwwSource = candidates.find((dir) => fs.existsSync(dir))
|
||
if (!wwwSource) {
|
||
failed = true
|
||
console.error('[prepare][错误] 未找到 App 资源,请先执行 npm run build:app-plus(或 HBuilderX 生成本地打包资源)')
|
||
} else {
|
||
fs.rmSync(wwwTarget, { recursive: true, force: true })
|
||
fs.cpSync(wwwSource, wwwTarget, { recursive: true })
|
||
log(`✓ 拷贝 App 资源: ${path.relative(ROOT, wwwSource)} -> assets/apps/${config.appId}/www`)
|
||
}
|
||
|
||
// ---------- 4.5 对齐编译器版本(隐藏"HTML5+ Runtime 版本不一致"提示) ----------
|
||
// CLI 构建资源时写入的 compilerVersion(如 5.07)与离线 SDK Runtime 版本不一致时,
|
||
// 启动会弹"本应用使用HBuilderX %s 编译,而手机端SDK版本是 %s"对话框。
|
||
// 此处把 www 内 manifest.json / app-config-service.js 的 compilerVersion 对齐到
|
||
// config/android.json 的 runtimeVersion(默认 5.24,与离线 SDK 一致),即可消除提示。
|
||
const runtimeVersion = config.runtimeVersion || '5.24'
|
||
function alignCompilerVersion (filePath) {
|
||
try {
|
||
if (!fs.existsSync(filePath)) return
|
||
let text = fs.readFileSync(filePath, 'utf-8')
|
||
const re = /"compilerVersion"\s*:\s*"[^"]*"/g
|
||
text = text.replace(re, '"compilerVersion": "' + runtimeVersion + '"')
|
||
fs.writeFileSync(filePath, text, 'utf-8')
|
||
log(`✓ 对齐 compilerVersion -> ${runtimeVersion}: ${path.relative(ROOT, filePath)}`)
|
||
} catch (e) {
|
||
warn(`对齐 compilerVersion 失败 ${filePath}: ${e.message}`)
|
||
}
|
||
}
|
||
alignCompilerVersion(path.join(wwwTarget, 'manifest.json'))
|
||
alignCompilerVersion(path.join(wwwTarget, 'app-config-service.js'))
|
||
|
||
// ---------- 9. 覆盖图标与启动图 ----------
|
||
function findResFiles(pattern) {
|
||
const resDir = path.join(DEMO_DIR, 'src', 'main', 'res')
|
||
const hits = []
|
||
if (!fs.existsSync(resDir)) return hits
|
||
const walk = (dir) => {
|
||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||
const full = path.join(dir, entry.name)
|
||
if (entry.isDirectory()) walk(full)
|
||
else if (pattern.test(entry.name)) hits.push(full)
|
||
}
|
||
}
|
||
walk(resDir)
|
||
return hits
|
||
}
|
||
|
||
function applyImage(cfgPath, patterns, desc) {
|
||
if (!cfgPath) return
|
||
const image = path.resolve(ROOT, cfgPath)
|
||
if (!fs.existsSync(image)) {
|
||
warn(`${desc}不存在,跳过: ${image}`)
|
||
return
|
||
}
|
||
const targets = findResFiles(patterns)
|
||
if (targets.length === 0) {
|
||
warn(`${desc}: 未在 res 下找到目标文件(正则: ${patterns}),请手动替换`)
|
||
return
|
||
}
|
||
for (const target of targets) {
|
||
fs.copyFileSync(image, target)
|
||
log(`✓ 覆盖${desc}: ${path.relative(ROOT, target)}`)
|
||
}
|
||
}
|
||
|
||
applyImage(config.icon, /^(icon\.png|ic_launcher(\.png)?)$/i, '应用图标')
|
||
applyImage(config.splash, /^splash(\.png|_screen\.png)?$/i, '启动图')
|
||
|
||
// ---------- 5. 总结 ----------
|
||
console.log('\n[prepare] 完成' + (failed ? '(部分失败,请查看上方错误)' : ''))
|
||
console.log('后续:包名/版本/签名/应用名/appid/appKey 由 Gradle 构建时从 config/android.json 与 package.json 动态读取')
|
||
if (!failed) process.exit(0)
|
||
process.exit(1)
|