Home

Awesome

XUpdate

api I Star

English | ChineseVideo tutorial

A lightweight, high availability Android version update framework. Click instruction document to experience it!

Please read 【wisdom of asking questions】 before raising the issue and strictly follow the issue template fill in and save everyone's time.

Please read the instruction document carefully before use, important things are to be repeated for three time!!!

Please read the instruction document carefully before use, important things are to be repeated for three time!!!

Please read the instruction document carefully before use, important things are to be repeated for three time!!!

About me

WeChat public numberjuejinzhihuCSDNjianshusegmentfaultbilibilitoutiao
我的Android开源之旅Click meClick meClick meClick meClick meClick meClick me

Simplify use

If you want to use xupdate faster, reduce the difficulty of integration, support breakpoint continuation download and other expansion functions, you can try to use XUpdateAPI.

Rapid integration of X-Library

In order to facilitate the rapid integration of X-Library, I provide a template project for your reference: https://github.com/xuexiangjys/TemplateAppProject


Features

Stargazers over time

Stargazers over time

Composition structure

This framework refers to AppUpdate some ideas and UI, the various parts of the version update are separated to form the following parts:

In addition, there are two listeners:

Update core manager:

Update process

Process after calling update:

IUpdateProxy/XUpdate --- (update) ---> IUpdateChecker --->(Request the server to get the latest version information)---> IUpdateParser ---> (Parse the data returned by the server, and build the UpdateEntity)---> IUpdateProxy ---> (If there is no latest version, end it directly, otherwise proceed to the following process)

    ---Automatic mode---> IUpdateDownloader ---> (Download the latest app apk) ---> Install application

    ---Non automatic mode---> IUpdatePrompter ---> Prompt for version update

                                                        ---> click Update ---> IUpdateDownloader ---> (Download the latest app apk) ---> Jump to application installation UI

                                                        ---> Click cancel or ignore ---> End

Click to view the framework UML design diagram


1、Demonstration

xupdate_default.png

xupdate_background.png

xupdate_force.png

xupdate_ignore.png

xupdate_custom.png

xupdate_system.png

Demo update background service

Because GitHub is slow to visit recently, if you need to experience xupdate better, you can Click to build a simple version update service.

Demo Download

Pgyer Download

Pgyer Download password: xuexiangjys

downloads

xupdate_download_pugongying.png

GitHub Download

downloads

xupdate_download.png


2、Quick integration guide

At present, it supports the use of the mainstream development tool AndroidStudio and add dependency by configures build.gradle directly.

2.1、Add gradle dependency

1.In the project root directory build.gradle:

allprojects {
     repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}
  1. Then add in the dependencies of build.gradle of the application project (usually app):

The following is the version description. Choose one.

dependencies {
  ...
  // androidx project
  implementation 'com.github.xuexiangjys:XUpdate:2.1.5'
}
dependencies {
  ...
  // support project
  implementation 'com.github.xuexiangjys:XUpdate:1.1.6'
}

2.2、Initialization

Initialize settings at the top of the application:

XUpdate.get()
    .debug(true)
    .isWifiOnly(true)                                               // By default, only version updates are checked under WiFi
    .isGet(true)                                                    // The default setting uses Get request to check versions
    .isAutoMode(false)                                              // The default setting is non automatic mode
    .param("versionCode", UpdateUtils.getVersionCode(this))         // Set default public request parameters
    .param("appKey", getPackageName())
    .setOnUpdateFailureListener(new OnUpdateFailureListener() {     // Set listening for version update errors
        @Override
        public void onFailure(UpdateError error) {
            if (error.getCode() != CHECK_NO_NEW_VERSION) {          // Handling different errors
                ToastUtils.toast(error.toString());
            }
        }
    })
    .supportSilentInstall(true)                                     // Set whether silent installation is supported. The default is true
    .setIUpdateHttpService(new OKHttpUpdateHttpService())           // This must be set! Realize the network request function.
    .init(this);                                                    // This must be initialized

【note】: if there is any problem, you can open debug mode to track the problem. If you also need to log on disk, you can implement the following interface.

XUpdate.get().setILogger(new ILogger() {
    @Override
    public void log(int priority, String tag, String message, Throwable t) {
        // Realize the function of logging
    }
});

2.3、Version update entity information

(1) UpdateEntity

Field nameTypeDefault valueDescription
mHasUpdatebooleanfalseWhether have the latest version
mIsForcebooleanfalseForce installation: app cannot be used without installation
mIsIgnorablebooleanfalseWhether the version can be ignored
mVersionCodeint0Latest version code
mVersionNameStringunknown_versionLatest version name
mUpdateContentString""Update content
mDownloadEntityDownloadEntityDownload information entity
mIsSilentbooleanfalseWhether to download silently: when there is a new version, do not prompt to download directly
mIsAutoInstallbooleantrueWhether to automatic install app when the download is completed

(2) DownloadEntity

Field nameTypeDefault valueDescription
mDownloadUrlString""Download address
mCacheDirString""File download directory
mMd5String""The encrypted check value of the downloaded file (MD5 encryption is used by default), which is used to verify and prevent the downloaded APK file from being replaced (the latest demo has a tool for calculating the check value). Note that the MD5 value here is not the MD5 value of the application signature file!
mSizelong0Size of download file【unit: KB】
mIsShowNotificationbooleanfalseWhether to show download progress in the notification bar

(3) PromptEntity

Field nameTypeDefault valueDescription
mThemeColorintR.color.xupdate_default_theme_colorTheme colors (background colors for progress bars and buttons)
mTopResIdintR.drawable.xupdate_bg_app_topTop background image resource ID
mTopDrawableTagString""Top background image drawable tag
mButtonTextColorint0Button text color
mSupportBackgroundUpdatebooleanfalseWhether background updates are supported
mWidthRatiofloat-1(Unconstrained)The ratio of the width of the version update prompter to the screen
mHeightRatiofloat-1(Unconstrained)The ratio of the height of the version update prompter to the screen
mIgnoreDownloadErrorbooleanfalseWhether to ignore the download exception (the update prompt box will not disappear if the download fails)

2.4、File encryption verification method

The default file encryption verification method used in this framework is MD5 encryption. Of course, if you don't want to use MD5 encryption, you can also customize the File Encryptor IFileEncryptor. The following is the implementation of MD5 File Encryptor for reference:

/**
 * The default file encryption calculation uses MD5 encryption
 *
 * @author xuexiang
 * @since 2019-09-06 14:21
 */
public class DefaultFileEncryptor implements IFileEncryptor {
    /**
     * Encrypted files
     *
     * @param file
     * @return
     */
    @Override
    public String encryptFile(File file) {
        return Md5Utils.getFileMD5(file);
    }

    /**
     * Verify that the file is valid (whether the encryption is consistent)
     *
     * @param encrypt The encrypted value, is considered to be valid if encrypt is empty.
     * @param file    File to be verified
     * @return Whether the document is valid
     */
    @Override
    public boolean isFileValid(String encrypt, File file) {
        return TextUtils.isEmpty(encrypt) || encrypt.equalsIgnoreCase(encryptFile(file));
    }
}

Finally, call the XUpdate.get().setIFileEncryptor method, settings will take effect.


3、Version update

3.1、Default version update

You can directly call the following code to complete the version update operation:

XUpdate.newBuild(getActivity())
        .updateUrl(mUpdateUrl)
        .update();

It should be noted that with the default version update, the JSON format returned by the request server should include the following contents:

{
  "Code": 0,
  "Msg": "",
  "UpdateStatus": 1,
  "VersionCode": 3,
  "VersionName": "1.0.2",
  "ModifyContent": "1、优化api接口。\r\n2、添加使用demo演示。\r\n3、新增自定义更新服务API接口。\r\n4、优化更新提示界面。",
  "DownloadUrl": "https://raw.githubusercontent.com/xuexiangjys/XUpdate/master/apk/xupdate_demo_1.0.2.apk",
  "ApkSize": 2048,
  "ApkMd5": ""
}

Field description:

3.2、Automatic version update

Automatic version update: auto check version + auto download APK + auto install APK (silent install).

You only need to set isAutoMode(true). However, if the device does not have root permission, it will not be able to complete automatic update (because the silent installation requires root permission).

XUpdate.newBuild(getActivity())
        .updateUrl(mUpdateUrl)
        .isAutoMode(true) // If you need to be completely unattended and update automatically, you need root permission【required for silent installation】
        .update();

3.3、Support background update

After enabling the background update, users can enter the background update after clicking the "background update" button.

XUpdate.newBuild(getActivity())
        .updateUrl(mUpdateUrl)
        .supportBackgroundUpdate(true)
        .update();

3.4、Force version update

If the user does not update, the program will not work normally. The server only needs to return the UpdateStatus field to 2.

Of course, if you customize the request return API, you only need to set the mIsForce field of UpdateEntity to true.

3.5、Custom version update prompt pop-up theme

By setting the update top picture, theme color, button text color, width to height ratio, etc

XUpdate.newBuild(getActivity())
        .updateUrl(mUpdateUrl)
        .promptThemeColor(ResUtils.getColor(R.color.update_theme_color))
        .promptButtonTextColor(Color.WHITE)
        .promptTopResId(R.mipmap.bg_update_top)
        .promptWidthRatio(0.7F)
        .update();

3.6、Custom version update parser

The implementation of IUpdateParser interface can realize the user-defined parser.

XUpdate.newBuild(getActivity())
        .updateUrl(mUpdateUrl3)
        .updateParser(new CustomUpdateParser()) // Set up a custom version update parser
        .update();

public class CustomUpdateParser implements IUpdateParser {
    @Override
    public UpdateEntity parseJson(String json) throws Exception {
        CustomResult result = JsonUtil.fromJson(json, CustomResult.class);
        if (result != null) {
            return new UpdateEntity()
                    .setHasUpdate(result.hasUpdate)
                    .setIsIgnorable(result.isIgnorable)
                    .setVersionCode(result.versionCode)
                    .setVersionName(result.versionName)
                    .setUpdateContent(result.updateLog)
                    .setDownloadUrl(result.apkUrl)
                    .setSize(result.apkSize);
        }
        return null;
    }
}

3.7、Custom version update checker + version update parser + version update prompter

XUpdate.newBuild(getActivity())
        .updateUrl(mUpdateUrl3)
        .updateChecker(new DefaultUpdateChecker() {
            @Override
            public void onBeforeCheck() {
                super.onBeforeCheck();
                CProgressDialogUtils.showProgressDialog(getActivity(), "查询中...");
            }
            @Override
            public void onAfterCheck() {
                super.onAfterCheck();
                CProgressDialogUtils.cancelProgressDialog(getActivity());
            }
        })
        .updateParser(new CustomUpdateParser())
        .updatePrompter(new CustomUpdatePrompter(getActivity()))
        .update();


public class CustomUpdatePrompter implements IUpdatePrompter {

    private Context mContext;

    public CustomUpdatePrompter(Context context) {
        mContext = context;
    }

    @Override
    public void showPrompt(@NonNull UpdateEntity updateEntity, @NonNull IUpdateProxy updateProxy, @NonNull PromptEntity promptEntity) {
        showUpdatePrompt(updateEntity, updateProxy);
    }

    /**
     * Show custom version update prompter
     *
     * @param updateEntity
     * @param updateProxy
     */
    private void showUpdatePrompt(final @NonNull UpdateEntity updateEntity, final @NonNull IUpdateProxy updateProxy) {
        String updateInfo = UpdateUtils.getDisplayUpdateInfo(mContext, updateEntity);

        new AlertDialog.Builder(mContext)
                .setTitle(String.format("是否升级到%s版本?", updateEntity.getVersionName()))
                .setMessage(updateInfo)
                .setPositiveButton("升级", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        updateProxy.startDownload(updateEntity, new OnFileDownloadListener() {
                            @Override
                            public void onStart() {
                                HProgressDialogUtils.showHorizontalProgressDialog(mContext, "下载进度", false);
                            }

                            @Override
                            public void onProgress(float progress, long total) {
                                HProgressDialogUtils.setProgress(Math.round(progress * 100));
                            }

                            @Override
                            public boolean onCompleted(File file) {
                                HProgressDialogUtils.cancel();
                                return true;
                            }

                            @Override
                            public void onError(Throwable throwable) {
                                HProgressDialogUtils.cancel();
                            }
                        });
                    }
                })
                .setNegativeButton("暂不升级", null)
                .setCancelable(false)
                .create()
                .show();
    }

3.8、Only use the downloader function to download APK

XUpdate.newBuild(getActivity())
        .apkCacheDir(PathUtils.getAppExtCachePath())  // Set the root directory of the download cache
        .build()
        .download(mDownloadUrl, new OnFileDownloadListener() {   // Set the download address and download listener
            @Override
            public void onStart() {
                HProgressDialogUtils.showHorizontalProgressDialog(getContext(), "下载进度", false);
            }

            @Override
            public void onProgress(float progress, long total) {
                HProgressDialogUtils.setProgress(Math.round(progress * 100));
            }

            @Override
            public boolean onCompleted(File file) {
                HProgressDialogUtils.cancel();
                ToastUtils.toast("apk下载完毕,文件路径:" + file.getPath());
                return false;
            }

            @Override
            public void onError(Throwable throwable) {
                HProgressDialogUtils.cancel();
            }
        });

3.9、Only use the APK installed features of XUpdate

_XUpdate.startInstallApk(getContext(), FileUtils.getFileByPath(PathUtils.getFilePathByUri(getContext(), data.getData()))); // Set the path where the file is located

If your APK installation is different, you can implement your own APK installer. You only need to implement the OnInstallListener interface and use the XUpdate.setOnInstallListener set it to take effect.

3.10、International

Due to the limited level of the author, only Chinese and English are supported【the default language is English】. If you need to support other languages, you only need to create a new corresponding language file under the res of your own project to translate in multiple languages.

Please refer to xupdate_strings.xml

What? You don't know Android's multilingual configuration yet? I suggest you take a look at this article:Android项目国际化多国语言适配

Proguard

-keep class com.xuexiang.xupdate.entity.** { *; }

// Note: if you use a custom API parser for parsing, you need to add confusion to your custom API entities. Here are the custom API entity obfuscation rules configured in this demo:
-keep class com.xuexiang.xupdatedemo.entity.** { *; }

Related links


Thanks

https://github.com/WVector/AppUpdate

Sponsor

Your support is the driving force of my maintenance. I will list the list of all the reward personnel at the bottom as the voucher. Please leave the notes of the support items before rewarding!

pay.png

Thank you for your sponsorship:

NameMoneyPlatform
*天100¥WeChat
*航10¥Alipay
X*?18.88¥WeChat
*网1¥WeChat
Joe88.88¥WeChat

WeChat Subscription

More information, please scan my personal WeChat Subscription:【我的Android开源之旅】

Contact