The official APP Upgrade Center has been released, which supports the upgrade of native APP packages and WGT resource packages. As shown in theUniapp. Dcloud. IO/uniCloud/up…

Note: This article is a hot update for front-end code resources. If it is the whole package upgrades, see also document ask.dcloud.net.cn/article/349…

From HBuilderX 1.6.5, UNI-App supports the generation of app resource upgrade packs.

Generate an App resource upgrade package

Changing the Version number

First, update the version number in manifest.json. For example, if the previous version was 1.0.0, the new version should be 1.0.1 or 1.1.0.

issue

The upgrade pack (WGT) is then generated in HBuilderX. Menu -> Release -> Native App- Make mobile App resource upgrade pack

At the end of the build, the console tells you the output location of the upgrade package.

Install the resource upgrade package

The server and client must work together to upgrade an application. The following uses the local test as an example:

Storage resources

Will % % appid. The WGT static files stored in server directory, i.e. http://www.example.com/static/ UNI * * * * 832 d722 WGT.

Server interface

Agreed test upgrade interface, address is: http://www.example.com/update/

Incoming parameters

Parameter names type The default value instructions
name String ‘ ‘ The application name read by the client. This parameter is defined to facilitate the reuse of interfaces by multiple applications.
version String ‘ ‘ The version number read by the client

Returns the parameter

Parameter names type The default value instructions
update Boolean false Is there any update?
wgtUrl String ‘ ‘ Download address of the WGT package, used for WGT update.
pkgUrl String ‘ ‘ Download address of apK/IPA package or AppStore address for the whole package upgrade mode.

Code sample

The following is a simple example of server judgment, only for reference, actual development according to their own business needs.

var express = require('express'); var router = express.Router(); var db = require('./db'); Function checkUpdate(params, callback) {db.query(' SQL', function(error, callback); Result) {// the result is not equal to the update. var currentVersions = params.appVersion.split('.'); var resultVersions = result.appVersion.split('.'); If (currentVersions[0] < resultVersions[0]) {callback({update: true, wgtUrl: ", pkgUrl: Result.pkgurl})} else {callback({update: true, wgtUrl: result.wgturl, pkgUrl: ''})}}); } router.get('/update/', function(req, res) { var appName = req.query.name; var appVersion = req.query.version; checkUpdate({ appName: appName, appVersion: appVersion }, function(error, result) { if (error) { throw error; } res.json(result); }); });Copy the code

Matters needing attention

  • The above agreement is for reference only.
  • The specific decision logic of the server can be handled flexibly according to its own service logic.
  • Application paths should not contain special symbols

The client detects the upgrade

Check the upgrade in the onLaunch of app. vue with the following code:

// #ifdef APP-PLUS plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) { uni.request({ url: 'http://www.example.com/update/', data: { version: widgetInfo.version, name: widgetInfo.name }, success: (result) => { var data = result.data; if (data.update && data.wgtUrl) { uni.downloadFile({ url: data.wgtUrl, success: (downloadResult) => { if (downloadResult.statusCode === 200) { plus.runtime.install(downloadResult.tempFilePath, { force: false }, function() { console.log('install success... '); plus.runtime.restart(); }, function(e) { console.error('install fail... '); }); }}}); }}}); }); // #endifCopy the code

Unsupported situations

  • There are some adjustments in the SDK, such as the addition of Maps module, etc., which cannot be upgraded in this way, but must be upgraded in the whole package.
  • The same cannot be done with native add-ons. For the old non-custom component compilation pattern, this pattern has been phased out. However, it is important to note that the old non-custom component compilation mode can not be used if the nvUE file is added in the update and the previous project did not have it. Since non-custom component compilation mode does not pack weeX engine without NVUE file, native engine cannot be added dynamically. The custom component mode includes the WEEX engine by default, regardless of nvUE files in the project.

Matters needing attention

  • Conditional compilation, which only performs this upgrade logic on the App platform.
  • Appid and version information, etc. are the information of the HBuilder application during the development of the HBuilderX real machine, so you need to pack the custom base or formal package to test the upgrade function.
  • Plus.runtime.version or uni.getSystemInfo() reads the version number of the APK/IPA package instead of the version information in the manifest.json resource. So plus.runtime.getProperty() is used to get the information.
  • After the WGT resource package is successfully installed, you must run plus.runtime.restart(). Otherwise, the new content does not take effect.
  • If the native engine of the App is not upgraded, only the WGT package should be upgraded to test the compatibility of THE WGT resources and the native base. Platform will default to remind does not match the version, if the test is no problem, can be configured in the manifest ignore the prompt, see ask.dcloud.net.cn/article/356…
  • www.example.com is only used as an example. In actual applications, the address must be a real IP address or a valid domain name. Do not copy and paste the address.

About whether hot updates affect app launch

In order to prevent developers from providing illegal content to users without approval from the market, the app market is largely averse to hot updates.

But hot updates are actually very common, both in native development and across platforms.

Apple has banned Jspatch in the past, but has not cracked down on other hot updates, including Cordovar, React Native, and DCloud. Jspatch was banned because it had a serious security flaw that could be exploited by hackers, allowing third-party hackers to tamper with other apps’ data.

Notes for using hot updates:

  • Do not pop up hot update prompt during shelf review
  • Hot updates are downloaded using HTTPS to avoid being hijacked by third-party networks
  • Do not update illegal content, do not destroy the benefits of the app market through hot updates, such as iOS virtual payments to pay Apple honestly