cordova热更新

热更新原理

通过添加热更新插件生成当前文件时间戳,程序启动后与服务器代码进行比较,如果服务器时间戳大于app时间戳,就从服务器下载文件, 实现自动更新。

安装Cordova-hcp Cli

npm install cordova-hot-code-push-cli -g

安装Cordova-hcp 插件

cordova plugin add cordova-hot-code-push-plugin

配置cordova-hcp.json文件

在项目根目录新建文件cordova-hcp.json

1
2
3
4
5
{
"autogenerated": true,
"content_url": "http://192.168.31.112/www", //服务器路径
"update": "start"
}

配置config.xml文件

1
2
3
4
5
<chcp>
<auto-download enabled="true" />
<auto-install enabled="true" />
<config-file url="http://172.16.1.245:8080/cordova-hot-update/cordova/www/chcp.json" />
</chcp>

运行

cordova-hcp build