preface

Since I opened XUpdate in 2018, I’ve been iterating over 14 versions, downloaded 4k+ monthly, and got 700+ Stars on Github.

Recently, I was studying The Flutter, so I wrote a little project to practice my hand. While writing, I found that there was no very good version to update the Flutter plugin. I tried to update the plugin with Bugly’s version, but the effect was very bad. And then baidu, basically are the following solution:

  • 1. Usepackage_infoPlug-in Obtains the version information about the current application
  • 2. UsedioThe plug-in makes a network request to obtain the latest version information
  • Use 3.flutter_downloaderDownload the latest APP and install it

I thought about it for a second, and it was a lot of trouble, considering that my XUpdate had done all of this in a single line of code, and there was no need to implement this almost universal feature in our own application.

I took a close look at the development of the Flutter plugin and implemented most of the XUpdate features on the Plugin in less than a day. Now I was able to comfortably update the version with a single line of code!

Below I give the address of flutter_xUpdate plugin:

Pub. Dev/packages/fl…


demo

  • Default version update

  • Support background update

  • Screen aspect ratio limit display updated

  • Forced to update

  • Custom update prompt popover style

Quick Integration Guide

Adding a reference dependency

Add a flutter_xUpdate dependency to the pubspec.yaml file in your Flutter project.

  • Method 1: Pub integration
Dependencies: flutter_xupdate: ^ 0.0.3Copy the code
  • Method two: Github integration
dependencies:
  flutter_xupdate:
    git:
      url: git://github.com/xuexiangjys/flutter_xupdate.git
      ref: master
Copy the code

The Android Settings

Modify Android project for AppCompat theme, the theme of the file path: Android/app/SRC/main/res/values/styles. The XML, such as:

<resources>
    <style name="LaunchTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
</resources>
Copy the code

Initialize the

  • callFlutterXUpdate.initMethod is initialized.
  • callFlutterXUpdate.setErrorHandlerMethod to set the error listener.
import 'package:flutter_xupdate/flutter_xupdate.dart'; Void initXUpdate() {if (platform.isandroid) {flutterxupdate. init(// whether to output log debug: true, /// whether to use post request isPost: IsPostJson: false, /// Whether to enable the automatic mode isWifiOnly: false, /// Whether to enable the automatic mode isAutoMode: SupportSilentInstall: false, supportSilentInstall: false, supportSilentInstall: false, enableRetry: Then ((value) {updateMessage(" successfully initialized: $value"); }).catchError((error) { print(error); }); FlutterXUpdate.setErrorHandler( onUpdateError: (Map<String, dynamic> message) async { print(message); setState(() { _message = "$message"; }); }); } else {updateMessage("ios does not support XUpdate "); }}Copy the code

Directions for use

Version update returns Json format

{"Code": 0, "Code": 0, "Msg": "", "UpdateStatus": 1, //0 indicates that the version is not updated, 1 indicates that the version is updated, which does not need to be forcibly upgraded, 2 indicates that the version is updated, which needs to be forcibly upgraded. "VersionCode": 3, // Compile version number (unique) "VersionName": "ModifyContent": "1, optimize API interface. \r\n2, add use demo. \r\n3. Added custom update service API. \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 the md5 value is not included, the apK is not guaranteed to be complete and will be re-downloaded every time. The framework uses MD5 encryption by default. }Copy the code

Version update

  • The default update
FlutterXUpdate.checkUpdate(url: _updateUrl);
Copy the code
  • Default App update + support background update
FlutterXUpdate.checkUpdate(url: _updateUrl, supportBackgroundUpdate: true);
Copy the code
  • Adjust the aspect ratio displayed for version updates
FlutterXUpdate. CheckUpdate (url: _updateUrl widthRatio: 0.6);Copy the code
  • Automatic mode version update, if you need to completely unassisted, automatic update, need root permission [silent installation required]
FlutterXUpdate.checkUpdate(url: _updateUrl, isAutoMode: true);
Copy the code
  • Click Cancel when downloading to allow switching download mode
FlutterXUpdate.checkUpdate( url: _updateUrl, overrideGlobalRetryStrategy: true, enableRetry: true, retryContent: "Github downloads are too slow, are you considering switching to dandelion downloads?" , retryUrl: "https://www.pgyer.com/flutter_learn");Copy the code

Custom Json parsing

1. Define a custom version of the update parser

FlutterXUpdate. SetCustomParseHandler (onUpdateParse: (String json) async {/ / this is custom json parsing return customParseJson (json); }); UpdateEntity customParseJson(String JSON) {AppInfo AppInfo = AppInfo.fromJSON (json); return UpdateEntity( hasUpdate: appInfo.hasUpdate, isIgnorable: appInfo.isIgnorable, versionCode: appInfo.versionCode, versionName: appInfo.versionName, updateContent: appInfo.updateLog, downloadUrl: appInfo.apkUrl, apkSize: appInfo.apkSize); }Copy the code

2. Call the checkUpdate method and set the isCustomParse parameter to true.

FlutterXUpdate.checkUpdate(url: _updateUrl3, isCustomParse: true);
Copy the code

Pass UpdateEntity directly to update

/ / / direct incoming UpdateEntity updated tips void checkUpdate8 () {FlutterXUpdate. UpdateByInfo (UpdateEntity: customParseJson(_customJson)); }Copy the code

Custom version update prompt popover style

Currently, only theme colors and top image customization are supported

1.Configure top picture, Path: android/app/src/main/res/values/drawable, For example:

2. Call checkUpdate and set themeColor and topImageRes parameters.

/ / / custom update pop-up window style void customPromptDialog () {FlutterXUpdate. CheckUpdate (url: _updateUrl, themeColor: '#FFFFAC5D', topImageRes: 'bg_update_top'); }Copy the code

Related links

  • XUpdate is a lightweight, highly available framework for updating Android versions
  • XUpdate documentation
  • XUpdate’s Flutter plugin
  • XUpdate background management service
  • XUpdate background management system

Wechat official account

For more information, welcome to wechat search official account: [My Android Open Source Journey]