Cordova

Cordova android平台开发三(热更新cordova-

2018-01-09  本文已影响289人  西瓜皮TWO

在config.xml中chcp节点下配置cordova-hot-code-push-plugin插件

1、config.xml 配置项

chcp config-file url="https://5027caf9.ngrok.com/chcp.json"//chcp
chcp auto-download enabled="false" //chcp
chcp auto-install enabled="false" //chcp

2、热更新插件必备的两个自有配置文件 chcp.json 和 chcp.manifest.

①、app热更新配置文件 www/chcp.json

release版本号,最低需要的外壳app版本号。文件置于www文件夹下,需要被打包到外壳app中,此文件可以用cordova-hcp(Cordova-hot-code-push-plugin)命令行半自动生成,在项目根目次下,执行:cordova-hcp init 可以生成此文件,之后发布新的release版本时,只要执行 cordova-hcp build 即可,详细命令参见:
https://github.com/nordnet/cordova-hot-code-push-cli
例:

{ "autogenerated": true,
  "release": "2016.09.30-10.15.04",
  "content_url": "https://179dc874.ngrok.io", 
  "update": "now"}
widget id="com.xxxxxx.hybrid"
version="0.0.1"
xmlns="http://www.w3.org/ns/widgets"
xmlns:cdv="http://cordova.apache.org/ns/1.0"
android-versionCode="12"
ios-CFBundleVersion="3"

version — app字符串版本号,既用户在APP应用商店上看见的版本号
android-versionCode — 安卓的build版本号,用于 min_native_interface
ios-CFBundleVersion — IOS的build版本号,用于 min_native_interface
例:app增加了新的插件,需要更新外壳app,为避免用户下载了不适应如今外壳版本的web内容,需要设置chcp.json 中 min_native_interface的值,例如我们app中chcp.json如次:

{ "autogenerated": true,
   "release": "2016.09.30-10.15.04", 
   "content_url": "https://132ec098.ngrok.io", 
   "update": "now",
   "min_native_interface":10}

此时外壳app的build版本是12(参见config.xml 中android-versionCode的值),若热更新的时候,web内容的release发布,

{ "autogenerated": true,
   "release": "2016.10.01-10.15.04", 
   "content_url": "https://132ec068.ngrok.io", 
   "update": "now",
   "min_native_interface":15}

此时加载chcp.json时,发现min_native_interface要求外壳app的版本是15,而外壳app现时build版本是12,显然不能满足web内容更新后外壳app最低版本要求15,(min_native_interface比外壳app的build版本高)此时,不会下载web内容,而是发出一个chcp_updateLoadFailed错误事件,告知用户需要升班外壳app版本。

②、web内容清单文件chcp.manifest

包含所有web内容文件的名称和md5值。

[
{
"file": "css/style.css",
"hash": "84a33cf00d95bc1e3617cc35262f7e94"
},
{
"file": "img/adam.jpg",
"hash": "b445da3cc203f97bce534fdad93b3931"
},
{
"file": "img/ben.png",
"hash": "2d983b2362f6db11855af918c7f95aee"
}
]

依据此文件来对比web内容文件的删除、新增、更新。
更新阶段,从服务器下载所有web文件
安装阶段,删除服务器端不存在的文件
此文件在放在www下,chcp.manifest 需要打包到外壳app中,同时也要放在服务器端www文件夹下(chcp.json中content_url指定的过访url)。可以经过命令 cordova-hcp build 生成 chcp.manifest 清单文件。

3、Build operations配置文件

不一样于在config.xml文件中修改插件配置,假如想在执行build命令行的时候修改插件配置,就需要用到chcpbuild.options文件。文件务必位于项目根目次下,文件采用json款式,此文件只有build命令执行时生效,不影响config.xml中的配置,无需改动,变更的是特定平台下的config.xml(cordova build 过程的after_perpare阶段)。
例如:config.xml文件中

chcp config-file url="https://company_server.com/mobile/www/chcp.json" //chcp

在 项目根目次下开创 chcpbuild.options 文件,内容如次:

{
   "dev": { "config-file": "https://dev.company_server.com/mobile/www/chcp.json" }, 
   "production": { "config-file": "https://company_server.com/mobile/www/chcp.json" }, 
   "QA": { "config-file": "https://test.company_server.com/mobile/www/chcp.json" }
}

build app 的时分,转为开发要用的服务器,可执行:

cordova build -- chcp-dev

结果就是, 特定平台下的 config.xml 文件(譬如, /Cordova/TestProject/platforms/android/res/xml/config.xml) 变成:

chcp config-file url="https://dev.company_server.com/mobile/www/chcp.json"//chcp

你可能注意到 - 我们用的命令有个 chcp-. 这个是必须的, 这样插件才知道, 这个参变量是为它设置的. 而且, 不会和其他插件的命令参变量冲突.
假如你的app可以测试了 - 你可以用下边的命令build, 就指定了测试服务器:

cordova build -- chcp-QA

特定平台下的 config.xml 便会成为:

chcp config-file url="https://test.company_server.com/mobile/www/chcp.json"//chcp

当我们需要上架app的时候 (Google Play, App Store) - 我们正常build:

cordova build --release

这样config.xml 是不会变更的,假如没有设置 chcpbuild.options - 插件会使用 config.xml 里面默认的值.

上一篇下一篇

猜你喜欢

热点阅读