在HBuilderX使用wgt包热更新,小白配置指南
-
准备阶段
- 确保已安装最新版HBuilderX
- 拥有可用的uni-app项目
-
配置manifest.json
{ "app-plus": { "versionCode": "增加版本号", //必须大于当前版本 "versionName": "x.x.x", //新版本号 "distribute": { "android": { "enable": true //启用热更新 }, "ios": { "enable": true //启用热更新 } } } } -
生成wgt包
- 菜单栏选择"发行" -> "制作移动App资源升级包"
- 生成.wgt文件(位于/unpackage/release目录)
-
服务器部署
- 将.wgt文件上传至服务器
- 保持文件URL路径固定不变
-
客户端检测更新代码(建议放在App.vue)
// 检查更新函数 checkUpdate() { const platform = uni.getSystemInfoSync().platform const serverUrl = 'https://yourdomain.com/path/latest.wgt' plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => { uni.request({ url: serverUrl + '?t=' + Date.now(), success: (res) => { if (res.statusCode === 200) { this.downloadAndInstall(serverUrl) } } }) }) },
// 下载安装函数
downloadAndInstall(url) {
uni.showLoading({title: '下载中...'})
plus.downloader.createDownload(url, {}, (download, status) => {
if (status === 200) {
plus.runtime.install(download.filename, {}, () => {
uni.showModal({
title: '提示',
content: '更新完成,需要重启应用',
success: () => {
plus.runtime.restart()
}
})
}, (error) => {
uni.showToast({title: '安装失败', icon: 'none'})
})
}
uni.hideLoading()
}).start()
}
6. 触发更新时机
- App启动时调用
- 定时检查(如每天一次)
- 用户手动触发
7. 注意事项
- 每次更新必须增加versionCode
- iOS需要配置https
- 测试时先打基座包
- wgt包大小建议不超过10M
- 更新失败需有重试机制所有内容均由人工智能模型生成,其生成内容的准确性和完整性无法保证,不代表我们的态度或观点。