An overview of the

Recently, we need to make another APK upgrade function, which basically needs to be rewritten every time we make a new APP. In order to facilitate the subsequent use, the upgrade function is encapsulated now. Because the orientation is not a single application, it needs to be highly abstract and extensible to fit all apps.

configuration

build.gradle

Implementation 'com. Zhangzheng. Versionupgrade: library: 1.0.0'Copy the code

AndroidMainfest.xml

  <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="com.zhangzheng.versionupgrade.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/filepath" />
        </provider>
Copy the code

Since the default apK installation function is provided in the upgrade library, it is necessary to use this part of the function (can be re-implemented by itself), and provider needs to be configured in Mainfest to configure permissions.

Android :authorities: (your package name + FileProvider)

Simple to use

If you simply want to use an upgrade feature, you just need to provide an interface to pull version information. The UpgradeRequest abstract class is available in the upgrade library for inheritance. You only need to provide the corresponding KEY, and it will handle itself, as shown in the following example:

The sample

Class TestUpgradeRequest :UpgradeRequest() {/** * config URL */ Override Fun Url () = "http://qa-yapi.amh-group.com/mock/481/columbus-app/advert/banner/workbench" / * * * do you have the new version to update, Override fun isNeedUpdateKey()="isNeedUpdate" / Override fun isForceUpdateKey()="isForceUpdate" /* override fun updateUrlKey()="url" }Copy the code

use

VersionUpgrade(TestUpgradeRequest()).requestUpdate(this)
Copy the code

Highly extended

Five latitude abstractions are made here to facilitate expansion. Each is provided with a default implementation to simplify usage. If the requirements cannot be met, you need to expand them one by one as follows:

   VersionUpgrade(
            upgradeRequest = TODO(),
            upgradeView = TODO(),
            upgradeControl = TODO(),
            upgradeDown = TODO(),
            upgradeInstall = TODO(),
            uiScope = TODO()
        ).requestUpdate(this)
Copy the code

IUpgradeRequest

Responsible for managing network requests, you need to pay attention to whether to upgrade, upgrade, upgrade apkUrl, and upgrade copy

*/ abstract suspend fun requestIsNeedUpdate(): Boolean /** * whether to force upgrade */ abstract suspend fun requestIsForceUpdate(): Boolean /** * update APK url */ abstract suspend fun requestUpdateUrl(): String /** * update copy */ abstract suspend Fun requestUpdateMessage():StringCopy the code

The default implementation provided is UpgradeRequest, which can be directly inherited to extend, or IUpgradeRequest if the change is large

IUpgradeControl

Local policy to control whether or not to upgrade, sometimes need to do not display the upgrade popup after the current rejection:

Interface IUpgradeControl{/** * Specifies whether the upgrade is allowed. */ fun allowUpdate():Boolean /** * Upgrade cancelled to */ fun notifyUpdateDismiss() /** * Click ok to upgrade */ fun NotifyStartUpdate () /** * Upgrade package download complete */ fun notifyUpdateDownSuccess()}Copy the code

The default implementation is no restriction

IUpgradeView

Responsible for the management of the upgrade popup, pay attention to: popup style, “refuse to upgrade” and “agree to upgrade” event notification

interface IUpgradeView {

    fun showDialog(context: Context, forceUpdate: Boolean, message: String)

    fun setUpdateListener(callBack: () -> Unit)

    fun setDismissListener(callBack: () -> Unit)

}
Copy the code

Default implementation: UpgradeView

IUpgradeDown

Responsible for apK download, focus: repeated trigger logic, download success, download failure, already downloaded logic

Abstract class IUpgradeDown {/** * requestDownUrl(context: context, URL: String): File? }Copy the code

The default implementation is UpgradeDown, which uses the system’s DownloadManager for downloading, and can be extended based on UpgradeDown to provide overloaded functions:

/** * downloading */ protected Open fun onDownloading(context: context) {toaster. MakeText (context, "downloading......" , toast.length_short).show()} /** * download failed */ protected open fun onDownLoadFail(context: Context) {toast. makeText(Context, "download failed ", Toast.length_short).show()} /** * download success */ protected open fun onDownLoadSuccess(context: /** * protected open fun apkFilePath(Context: Context) = File( context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) , /** * apk name */ protected open fun apkName(context: Context) = if (apkName.isNotEmpty()) { apkName } else getAppName(context) + "_" + getVerName(context) + ".apk"Copy the code

IUpgradeInstall

Responsible for installation logic

Interface IUpgradeInstall{/** * Install APK */ fun install(context: context,file: file)}Copy the code

The default implementation is provided: UpgradeInstall. Generally this part does not need to be extended.

The last

Thank you for reading, if you feel helpful, I hope you can pay attention to + like + favorites github address: github.com/long8313002…