introduce
In the process of mobile development, application upgrade is an essential link, so, r_upgrade application upgrade plug-in appears, here is the first applause to welcome 👏👏, the following is the introduction to use
r_upgrade
Update app for Android and IOS == Update app for Flutter
- Webpage link form upgrade
- Upgrade in apK download form
Use Service or DownloadManager
- Jump to the App Store for upgrade
Android
Hot updateAndroid
The incremental upgrade
Let’s start
1. Use plug-ins:
Add the following code to the pubspec.yaml file
dependencies:
r_upgrade: last version
Copy the code
2. Update by opening links (Android
andIOS
Gm)
void upgradeFromUrl()async{
bool isSuccess =await RUpgrade.upgradeFromUrl(
'https://www.baidu.com',);print(isSuccess);
}
Copy the code
The Android platform
1. Go to the App Store to upgrade
void upgradeFromAndroidStore(){
bool isSuccess = await RUpgrade.upgradeFromAndroidStore(AndroidStore.BAIDU);
print('${isSuccess?'Jump successful':'Jump failed'}');
}
Copy the code
2. Download APK from the download link
Note that in Android applications, make sure that the following permissions are declared in androidmanifest.xml and dynamically authorized on 6.0, otherwise the upgrade method will be called and a permission exception will be thrown
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Copy the code
1. Add the listening for the upgrade download progress
RUpgrade.stream.listen((DownloadInfo info){
/ / /...
});
Copy the code
Info contains the following information:
field | meaning |
---|---|
(int) id | Id of the current download task |
(int) max_length | Total size of downloads required (bytes) |
(int) current_length | Current downloaded size (bytes) |
(double) percent | Current download progress (0-100) |
(double) planTime | Planned download completion time in seconds (requirement.toStringasFixed (0)) |
(String) path | Path to the downloaded file |
(double) speed | Current download speed KB /s |
(DownloadStatus) status | Current Download StatusSTATUS_PAUSED Download suspendedSTATUS_PENDING Waiting for downloadSTATUS_RUNNING In the downloadSTATUS_SUCCESSFUL Download successfulSTATUS_FAILED Download failedSTATUS_CANCEL Download the cancel |
Note: Some HTTP download links may return max_length = -1, use your own judgment
2. Upgrade your app now
Currently divided into two parts useDownloadManager:
true
: Invokes the systemDownloadManager
To download- Advantages: Easy access, no need to worry about operation, download all managed by the system
- Disadvantages: HTTP cannot be used for downloading, you cannot click the notification bar to pause the downloading process, you cannot pause or continue downloading according to whether there is a network, and there are problems with adaptation models
- Supported methods:
RUpgrade.stream
,install
,cancel
false
: callService
Download (default)- Advantages: Full functions, support HTTP/HTTPS download, automatically suspend the download after the network is disconnected, continue downloading after the connection, support breakpoint continuation, support to query the last download, etc
- Disadvantages: Not found yet, welcome to issue if found bug.
- Supported methods: default all
// [isAutoRequestInstall] automatically pops up after downloading
// [apkName] The name of the installation package (.apk must be included)
// [notificationVisibility] Specifies the notification display mode
// [useDownloadManager] specifies whether to useDownloadManager. It is not used by default. (DownloadManager does not support HTTP downloads.
// [upgradeFlavor] upgradeFlavor, default full upgrade (default)
void upgrade() async {
int id = await RUpgrade.upgrade(
'https://raw.githubusercontent.com/rhymelph/r_upgrade/master/apk/app-release.apk',
apkName: 'app-release.apk',isAutoRequestInstall: true);
}
Copy the code
New upgrade flavor :(not supported using DownloadManager)
enum RUpgradeFlavor {
normal, // Full upgrade
hotUpgrade, / / hot update
incrementUpgrade, // Incremental upgrade
}
Copy the code
3. Cancel the download
void cancel() async {
bool isSuccess=await RUpgrade.cancel(id);
}
Copy the code
4. Install the application
void install() async {
bool isSuccess=await RUpgrade.install(id);
}
Copy the code
5. Pause the download
void pause() async {
bool isSuccess=await RUpgrade.pause(id);
}
Copy the code
6. Continue downloading
void pause() async {
bool isSuccess=await RUpgrade.upgradeWithId(id);
// Return false to indicate that this ID never exists
/ / return true
// The status before calling this method is [STATUS_PAUSED], [STATUS_FAILED], [STATUS_CANCEL], and the download will continue
// The state before calling this method is [STATUS_RUNNING], [STATUS_PENDING], and nothing changes
// The state before calling this method is [STATUS_SUCCESSFUL], and the application will be installed
// When the file is deleted, download it again
}
Copy the code
7. Obtain the ID of the last download
This method only looks for the current app version name and the ids that have been downloaded under the version number
void getLastUpgradeId() async {
int id = await RUpgrade.getLastUpgradedId();
}
Copy the code
8. Obtain the download status corresponding to the ID
void getDownloadStatus()async{
DownloadStatus status = await RUpgrade.getDownloadStatus(id);
}
Copy the code
9. Incremental upgrade
- 1. Download the BSDIff tool to the local PC
- 2. Prepare two installation packages, one for the upgrade (old.apk) and one for the update (new.apk).
- 3. Switch to the command line interface
bsdiff
In the directory, run the command./bsdiff old.apk new.apk increment.patch
- 4. Generate the above
increment.patch
Uploading to the server - 5. Call
RUpgrade. Upgrade (... , upgradeFlavor: RUpgradeFlavor. IncrementUpgrade)
Method to download - 6. Call
RUpgrade.install(id)
To install
The code is as follows:
int id;
void incrementUpgrade(){
id = await RUpgrade.upgrade(
'https://mydata-1252536312.cos.ap-guangzhou.myqcloud.com/r_upgrade.patch',
fileName: 'r_upgrade.patch',
useDownloadManager: false,
isAutoRequestInstall: false,
upgradeFlavor: RUpgradeFlavor.incrementUpgrade,
);
}
void install(){
try {
await RUpgrade.install(id);
} catch (e) {
_state.currentState
.showSnackBar(SnackBar(content: Text('Incremental update failed! '))); }}Copy the code
10. Hot update
- You can use the upgrade to return
id
For hot updates, the downloaded files need to be generated by the new versionisolate_snapshot_data
,kernel_blob.bin
,vm_snapshot_data
To download the zip file, perform the following steps:- run
flutter clean
Clean the Build file - run
flutter build bundle
Generate the required artifacts, marked below with an asterisk as a required file
- run
|- AssetManifest.json
|- FontManifest.json
|- fonts
|- ...
|- isolate_snapshot_data *
|- kernel-blob.bin *
|- LICENSE
|- packages
|- ...
|- vm_snapshot_data *
Copy the code
- Package the file marked with an asterisk into a ZIP file and upload it to the server - call 'rupgrade.upgrade (... , upgradeFlavor: RUpgradeFlavor. ` hotUpgrade) method to download - when the download is complete, will get hot update to the id of the above, the following codeCopy the code
bool isSuccess = await RUpgrade.install(id);
if (isSuccess) {
_state.currentState
.showSnackBar(SnackBar(content: Text('Hot update successful, exit application in 3s, please re-enter')));
Future.delayed(Duration(seconds: 3)).then((_){
SystemNavigator.pop(animated: true);
});
}else{
_state.currentState
.showSnackBar(SnackBar(content: Text('Hot update failed, please wait for update pack download to complete')));
}
Copy the code
- Restart the applicationCopy the code
Note: Hot updates only support changes to Flutter code, not resource files, etc. The author of the plugin is not responsible for any consequences caused by hot updates.
Android platform notification bar
If you want to customize the content of the notification bar display, can do it, modify, or add a file path for the project/android/app/main/res/r_upgrade_value XML, add the following code
<?xml version="1.0" encoding="utf-8"? >
<resources>
<string name="r_upgrade_download_speech">%.2fkb/s</string>
<string name="r_upgrade_download_planTime">It is expected to be completed in %.0f seconds</string>
<string name="r_upgrade_download_finish">The download is complete</string>
<string name="r_upgrade_download_paused">Download suspended</string>
<string name="r_upgrade_download_failed">Download failed</string>
</resources>
Copy the code
Then, when you use the upgrade method, you should set the parameter notificationStyle, which defaults to show the expected completion time.
/// Notification show style about content text
enum NotificationStyle {
speechAndPlanTime, // 100KB /s is expected to be completed in 1 second
planTimeAndSpeech, // It is expected to complete 100KB /s in 1 second
speech,// 100kb/s
planTime, // Expected to complete in 1 second
none, //
}
Copy the code
The IOS platform
1. Jump to the AppStore for updates
void upgradeFromAppStore() async {
bool isSuccess =await RUpgrade.upgradeFromAppStore(
'your AppId'.// For example, wechat AppId:414478124
);
print(isSuccess);
}
Copy the code
2. Get the last version name of your app in the AppStore
void getVersionFromAppStore() async {
String versionName = await RUpgrade.getVersionFromAppStore(
'your AppId'.// For example, wechat AppId:414478124
);
print(versionName);
}
Copy the code
Conclusion: Dart Inn has released a series of Aqueduct framework tutorial album. Welcome to 👏👏 if you like to learn Dart language development