探索:Uniapp 安卓热更新

发布于:2025-08-04 ⋅ 阅读:(6) ⋅ 点赞:(0)


效果

在这里插入图片描述

在这里插入图片描述


快速验证是否可行

  • 首先打包一个版本比当前安卓高的 wgt 版本,可以在首页写一些更新
    在这里插入图片描述

  • 然后把 wgt 资源放到服务器中,让前端拿到下载地址

  • 主要的热更新逻辑是如下的代码,看看这个是否能生效即可

const updateApp = async () => {
	console.log('updateApp:>>')
    console.log('updateInfo:>>', updateInfo.value);
    if (!updateInfo.value.wgtUrl) {
        uni.showToast({
            title: '更新包不存在',
            icon: 'none'
        });

        return
    }
    updateLoading.value = true
    const downloadTask = uni.downloadFile({
        url: updateInfo.value.wgtUrl,
        success: (res) => {
            if (res.statusCode === 200) {
				console.log('downloadTask, res', res)
                updateLoading.value = false
                installUpdate(res.tempFilePath);
            }
        },
        fail: (err) => {
            uni.showToast({ title: '下载失败', icon: 'none' });
            console.error('下载失败:', err);
        }
    });

    // 进度更新
    downloadTask.onProgressUpdate((res) => {
        updateProgress.value = res.progress;
    });
};
// 安装更新
const installUpdate = (tempFilePath: string) => {
    // #ifdef APP-PLUS
    plus.runtime.install(
        tempFilePath,
        { force: false },
        () => {
            restartFlag.value = true;
            setTimeout(() => {
                plus.runtime.restart();
            }, 1500);
        },
        (err) => {
            uni.showToast({ title: '安装失败', icon: 'none' });
            console.error('安装失败:', err);
        }
    );
    // #endif
};

实现步骤

  • wgt 的包版本要比当前安装的版本高,否则会报如下错误
{
  "code": -1205,
  "message": "WGT installation package manifest.json the version of the file does not match"
}

参考文章


网站公告

今日签到

点亮在社区的每一天
去签到