Since I opened XUpdate in 2018, I have iterated on 14 versions, with 4k+ downloads per month and 800+ stars on Github.
The XUpdate ecosystem includes the Android native SDK, the Flutter plugin, the version update background service, and the version update management system. In order to enrich its ecosystem, I decided to write another react-Native plugin.
I give below the react – native – xupdate – new plug-in address: www.npmjs.com/package/rea…
demo
- The default update
- Support background update
- Screen aspect ratio limits display updates
- Forced to update
- Custom update prompt popover style
Quick Integration Guide
Note that the react-native xupdate was already released by someone else, so I had to change the release name to react-native xupdate-new
Add the dependent
npm install react-native-xupdate-new --save
Copy the code
link
react-native link react-native-xupdate-new
Copy the code
Initialize the
- call
XUpdate.init
Method to initialize. - call
XUpdate.addErrorListener
Method to set error listening.
import {XUpdate} from 'react-native-xupdate-new'; // initialize initXUpdate() {/// set the initialization parameter let args = new InitArgs(); Args. debug = true; // Whether the post request is an upload json args.isPostJson = false; Args. isWifiOnly = false; Args. isAutoMode = false; / / / support silent installation, this need equipment has root access args. SupportSilentInstall = false; Args. EnableRetry = false; args. /// initialize SDK xupdate.init (args). Then (result => {this.setState({_message: 'initialization successful :' + json.stringify (result)}); }).catch(error => { console.log(error); This.setstate ({_message: 'initialization failed :' + error,}); }); // Set custom xupdate.setCustomParser ({parseJson: this.customParser}); // Set the error to listen on xupdate.adderrorListener (this.errorListener); }Copy the code
Directions for use
Version update returns the Json format
{"Code": 0, //0 means the request succeeded, non-0 means the request failed "Msg": "", // request error message "UpdateStatus": 1, //0: no update, 1: version update, no forcible upgrade, 2: version update, forcible upgrade "VersionCode": 3, // Version number (unique) "VersionName": "1.0.2", // Version name (for display) "ModifyContent": "1, Optimize API interface. \r\n2, add use demo demo. \r\n3. Added custom update service API interface. \r\n4, optimize the update prompt interface. // Update the content "DownloadUrl": "Https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk", / / file download address "ApkSize" : 2048, // File size (unit: KB) "ApkMd5": "..." // If there is no MD5 value, there is no guarantee that the APK is complete. The framework uses MD5 encryption by default. }Copy the code
Version update
- The default update
checkUpdateDefault() {
let args = new UpdateArgs(_updateUrl);
XUpdate.update(args);
}
Copy the code
- Default App update + support background update
checkUpdateSupportBackground() {
let args = new UpdateArgs(_updateUrl);
args.supportBackgroundUpdate = true;
XUpdate.update(args);
}
Copy the code
- Adjust aspect ratio to display version updates
checkUpdateRatio() { let args = new UpdateArgs(_updateUrl); The args. WidthRatio = 0.6; XUpdate.update(args); }Copy the code
- Version update in automatic mode, if you need to completely unaided, automatic update, need root permission
checkUpdateAutoMode() {
let args = new UpdateArgs(_updateUrl);
args.isAutoMode = true;
XUpdate.update(args);
}
Copy the code
- Click cancel to switch the download mode when downloading
enableChangeDownLoadType() { let args = new UpdateArgs(_updateUrl); args.overrideGlobalRetryStrategy = true; args.enableRetry = true; Args. RetryContent = 'Github download speed is too slow, would you consider switching to dandelion download? '; args.retryUrl = 'https://www.pgyer.com/flutter_learn'; XUpdate.update(args); }Copy the code
Customize Json parsing
1. Define a custom version update parser
// Set custom xupdate.setCustomParser ({parseJson: this.customParser}); ///Resolve the custom JSON content to the UpdateEntity entity class customParser = (json) => { let appInfo = JSON.parse(json); Return {// Required hasUpdate: appInfo['hasUpdate'], versionCode: appInfo['versionCode'], versionName: AppInfo ['versionName'], updateContent: appInfo['updateLog'], downloadUrl: appInfo['apkUrl'], // select isIgnorable: appInfo['isIgnorable'], apkSize: appInfo['apkSize'], }; };Copy the code
2. Set isCustomParse to true.
CustomJsonParse () {let args = new UpdateArgs(_updateUrl3); args.isCustomParse = true; XUpdate.update(args); }Copy the code
Pass the UpdateEntity directly to update it
/ / / direct incoming UpdateEntity updated tips checkUpdateByUpdateEntity () {let args = new UpdateArgs (); args.supportBackgroundUpdate = true; XUpdate.updateByInfo(args, { hasUpdate: AppInfo['hasUpdate'], versionCode: AppInfo['versionCode'], versionName: AppInfo['versionName'], updateContent: AppInfo['updateLog'], downloadUrl: AppInfo['apkUrl'], // select isIgnorable: AppInfo['isIgnorable'], apkSize: AppInfo['apkSize'], }); }Copy the code
Custom version update prompt pop-up style
Currently, only theme colors and top images can be customized
1. Configure the resource path for the top image.android/app/src/main/res/values/drawable
, do not put in mipmap file, otherwise the resource will not be found. Such as:
2. Set themeColor and topImageRes.
CustomPromptDialog () {let args = new UpdateArgs(_updateUrl); args.themeColor = '#FFFFAC5D'; args.topImageRes = 'bg_update_top'; XUpdate.update(args); }Copy the code
Related links
- XUpdate is a lightweight, highly available framework for updating Android versions
- XUpdate usage instructions document
- XUpdate plugin for Flutter
- XUpdate background management service
- XUpdate background management system
Wechat official account
More information, welcome to wechat search public number: [My Android open source journey]