目录

1 Service Introduction

App update refers to the process in which HMS SDK downloads a later version of the app from HUAWEI AppGallery and installs it. An app update can be automatically triggered by the app background or manually by the user after successful app startup.

Please refer to the following for the interactions:

  1. The user starts the app or triggers the update check in the app.

  2. The app detects an update and displays the update confirmation page.

  3. The user confirms the update and is directed to the app update page in Huawei AppGallery to perform the update.

2 Development Preparation

2.1 Registering as a Developer

Before developing an app, you will need to register as a HUAWEI developer and complete identity verification on the HUAWEI Developer website. For details, please refer to Account Registration and Verification.

2.2 Creating an App

To create a game app, please refer to Creating an App in AppGallery Connect and upload an interim APK file.

2.3 Configuring the Signing Certificate Fingerprint

1. Sign in to AppGallery Connect and select My apps.

2. Find your app from the list, and click the link under Android App in the Mobile phone column.

3. Go to Development > Overview > App information and set SHA-256 certificate fingerprint to the SHA-256 fingerprint of your app. For details about how to generate a SHA-256 fingerprint, please refer to Generating a Signing Certificate Fingerprint.

4. After completing configuration, click.

2.4 Integrating HMS SDK

The update capability of HMS SDK is integrated in Game Service. To use the update capability of HMS SDK, integrate the Game Service SDK.

Note:
After integrating the Game Service SDK, either apps or games can use the update capability of HMS SDK.

2.4.1 Integrating the HMS SDK into Your App Project in the Android Studio IDE

For AndroidStudio, Huawei provides an HMS SDK that can be integrated by using the Maven repository. 

1. Add the AppGallery Connect configuration file of the current app to your AndroidStudio.

    a. Sign in to AppGallery Connect and select My apps.

    b. Find your app from the list, and click the app name 

    c. Go to Develop > Project setting.

    d. In the App information section, click agconnect-services.json to download the configuration file.

    e. Copy the agconnect-services.json file to the app root directory.

2. Configure the Maven repository address for HMS SDK and the AppGallery Connect plug-in address.

     a. Open the build.gradle file in the root directory of your AndroidStudio project.

     b. Go to allprojects -> repositories and configure the Maven repository address for HMS SDK.

  1. allprojects {
  2.            repositories {
  3.                google()
  4.                jcenter()
  5.                maven {url 'https://developer.huawei.com/repo/'}
  6.            }
  7.        }
Note:
The Maven repository address cannot be visited from a browser. It can only be configured in the IDE.

    c. Go to buildscript->repositories and configure the Maven repository address for HMS SDK.

  1. buildscript {
  2.            repositories {
  3.                google()
  4.                jcenter()
  5.                maven {url 'https://developer.huawei.com/repo/'}
  6.            }
  7. }

     d. Go to buildscript->dependencies and configure the AppGallery Connect plug-in address.

  1. buildscript {
  2.            dependencies {
  3.             classpath 'com.huawei.agconnect:agcp:1.3.1.300'
  4.            }
  5. }

3. Add compile dependencies.

    a. Open the build.gradle file in the app directory.

    b. Add the AppGallery Connect plug-in dependency to the bottom of the file.

  1. apply plugin: 'com.huawei.agconnect'

     c. Configure compile dependencies.

  1. dependencies {  
  2.  implementation 'com.huawei.hms:base:4.0.4.301'
  3.  implementation 'com.huawei.hms:game:4.0.3.301'
  4. }

4. Re-open the modified build.gradle file. You will find a Sync Now link in the upper right corner of the page. Click Sync Now and wait until synchronization has completed.

2.4.2 Integrating the HMS SDK into Your App Project in the Eclipse IDE

Before starting your development in the Eclipse Integrated Development Environment (IDE), download the HMS SDK and integrate it into your app project.

Downloading the SDK

1. Download the .zip packages of the HMS Base SDK and HMS Core Game Service SDK at the following URLs, and decompress the packages.

HMS Base SDK: HMS Base SDK

HUAWEI Game Service SDK: HUAWEI Game Service SDK

SDK

Project

Description

Remarks

HMS Base SDK

HMSSdkBase

HMS SDK core function package

The SDK is mandatory.

Agconnect-core

HMS SDK dependency package

HMSSdkAvailableUpdate
HMSSdkDevice
HMSSdkLog
HMSSdkStats
HMSSdkUI

Network-common

Network-grs

Tasks

Update

HUAWEI Game Service SDK

Hmssdk-game

Provides the upgrading functions.

The SDK is mandatory.

2. Import all SDK projects to your workspace.

    a. Go to File > Import > Android > Existing Android Code Into Workspace and click Next.

    

    b. Click Browse and navigate to the directory where your projects are located. Select the projects to be imported under Projects and click Finish. By default, all projects are selected. You can change as needed.

NOTE: The projects to be imported must be located in the same drive as the workspace, for example, D:\. If they are not in the same drive, select Copy projects into workspace to copy the project file to the workspace.

    

Adding Project Dependencies

1. Right-click your app project and choose Properties from the shortcut menu.

2. Choose Resource > Android from the navigation tree, click Add, select the imported projects, and click Apply and Close.

3. Right-click the HMSSdkAvailableUpdate project and choose Properties from the shortcut menu.

4. Choose Android from the navigation tree. Then, click Add in the Library area, select the HMSSdkUI project, and click Apply and Close.

5. As the XML file in the Eclipse IDE does not support the dollar sign ($), replace ${applicationId} in the AndroidManifest.xml file of the HMSSdkAvaliableUpdate and Agconnect-core projects with your app package name.

  1. <application>
  2.    <provider
  3.        android:name="com.huawei.agconnect.core.provider.AGConnectInitializeProvider"
  4.        android:authorities="${applicationId}.AGCInitializeProvider"
  5.        android:exported="false" />
  6.    <service
  7.        android:name="com.huawei.agconnect.core.ServiceDiscovery"
  8.        android:exported="false" />
  9. </application>

Copying the HMSSdkStats Properties Files to Your App Project Directory

Copy the properties files in the assets directory of the HMSSdkStats project to the assets directory of your app project.

NOTE: If any other kits are involved, copy the properties files of these kits to your app project directory.

Configuring the project.properties File

Find and open the file in your project directory and add the following code to merge the manifest files in the subprojects:

  1. manifestmerger.enabled=true
Note:
The ADT version must be 17 or later.

Adding the AppGallery Connect Configuration File

1. Sign in to AppGallery Connect and select My apps.

2. Find your app from the list and click the app name.

3. Go to Develop > Project setting > App information.

4. Click agconnect-services.json to download the configuration file.

5. The Eclipse IDE does not support the AppGallery Connect plug-in. Therefore, you need to place the agconnect-services.json file in the assets directory of your project and add the following code to the Application subclass of the project. If the project does not have an Application subclass, you need to create a subclass that is inherited from the Application subclass. For example, create a MyApplication class.

  1. public class MyApplication extends Application{
  2.    @Override
  3.    public void onCreate() {
  4.        super.onCreate();
  5.    }
  6.    // TODO: Add the following code:
  7.    @Override
  8.    protected void attachBaseContext(Context context) {
  9.        super.attachBaseContext(context);
  10.        AGConnectServicesConfig config = AGConnectServicesConfig.fromContext(context);
  11.        config.overlayWith(new LazyInputStream(context) {
  12.            public InputStream get(Context context) {
  13.                try {
  14.                    return context.getAssets().open("agconnect-services.json");
  15.                } catch (IOException e) {
  16.                    return null;
  17.                }
  18.            }
  19.        });
  20.    }
  21.    // TODO: End
  22. }

You still need to import the following classes:

  1. import java.io.IOException;
  2. import java.io.InputStream;
  3. import com.huawei.agconnect.config.AGConnectServicesConfig;
  4. import com.huawei.agconnect.config.LazyInputStream;
  5. import android.app.Application;
  6. import android.content.Context;
  7. import android.util.Log;

6. Open the AndroidManifest.xml file of your project and add the Application subclass name to the application section. If the subclass name has been added, skip this step. In the following example, add the MyApplication class name.

  1. <application
  2.    android:name=".MyApplication"
  3.    ...
  4.    >
  5. ...

7. In the Eclipse IDE menu bar, click Project, then Clean.

2.5 Configuring Obfuscation Scripts

Do not obfuscate HMS SDK before compiling the APK. Otherwise, function errors may occur.

The configuration file in Android Studio is proguard-rules.pro, and that in Eclipse is proguard-project.txt.

1. Open the obfuscation configuration file of your project.

2. Add configuration to ensure that HMS SDK is not obfuscated.

  1. -ignorewarnings
  2. -keepattributes *Annotation*
  3. -keepattributes Exceptions
  4. -keepattributes InnerClasses
  5. -keepattributes Signature
  6. -keepattributes SourceFile,LineNumberTable
  7. -keep class com.hianalytics.android.**{*;}
  8. -keep class com.huawei.updatesdk.**{*;}
  9. -keep class com.huawei.hms.**{*;}
  10. -keep class com.huawei.gamebox.plugin.gameservice.**{*;}
  11. -keep interface com.huawei.hms.analytics.type.HAEventType{*;}
  12. -keep interface com.huawei.hms.analytics.type.HAParamType{*;}
  13. -keep class com.huawei.hms.analytics.HiAnalyticsTools{
  14.        public static void enableLog();
  15.        public static void enableLog(int);
  16. }
  17. -keep class com.huawei.hms.analytics.HiAnalyticsInstance{*;}
  18. -keep class com.huawei.hms.analytics.HiAnalytics{*;}

3. If you have used AndResGuard, add the AndResGuard allowlist to the obfuscation configuration file.

  1. "R.string.agc*",
  2. "R.string.hms*",
  3. "R.string.connect_server_fail_prompt_toast",
  4. "R.string.getting_message_fail_prompt_toast",    
  5. "R.string.no_available_network_prompt_toast",    
  6. "R.string.third_app_*",    
  7. "R.layout.hms*",      
  8. "R.drawable.upsdk*",    
  9. "R.color.upsdk*",  
  10. "R.dimen.upsdk*",  
  11. "R.style.upsdk*",
  12. "R.id.scroll_layout",
  13. "R.id.divider",
  14. "R.id.name_textview",
  15. "R.id.version_textview",
  16. "R.id.appsize_textview",
  17. "R.id.allsize_textview",
  18. "R.id.content_textview",
  19. "R.id.third_app_dl_progress_text",
  20. "R.id.cancel_bg",
  21. "R.id.third_app_dl_progressbar",
  22. "R.id.upsdk*",
  23. "R.string.upsdk*",
  24. "R.layout.upsdk*"

3. Updating an App

3.1 Scenario

After an app is launched or when a user proactively checks whether there is a later version, call the related API of HMS SDK to check whether there is a later version available in HUAWEI AppGallery. If yes, display a pop-up asking the user whether to update the app.

3.2 Service Process

  1. The user triggers an update check, for example, by starting the app or manually performing the check on the update check page.

  2. The app calls JosApps.getAppUpdateClient to request to initialize the AppUpdateClient instance.

  3. HMS SDK returns the AppUpdateClient instance of the current app to the app.

  4. The app calls the AppUpdateClient.checkAppUpdate method to request an update check.

  5. HMS SDK queries the latest app version information in Huawei AppGallery.

  6. HUAWEI AppGallery sends the app version information back to HMS SDK.

  7. HMS SDK sends the check result to the app by means of callback.

  8. The app checks the ApkUpgradeInfo instance returned by the onUpdateInfo method in the callback result and checks whether an update is available.
    ● If an update is available, proceed to the next step.
    ● If no update is available, the process ends.

  9. The app calls the AppUpdateClient.showUpdateDialog method to request to display the update pop-up for the user.

  10. HMS SDK displays the update pop-up to the user.

  11. The user acknowledges the app to be updated on the update confirmation page.

  12. HMS SDK sends a request to HUAWEI AppGallery to download the latest app installation package.

  13. HUAWEI AppGallery sends the app installation package back to HMS SDK.

  14. HMS SDK downloads the installation package and installs it.

3.3 Development Procedures

The key steps for updating a app are as follows:

1.Call JosApps.getAppUpdateClient to initialize the AppUpdateClient object.

  1. AppUpdateClient client = JosApps.getAppUpdateClient(this);

2. Call AppUpdateClient.checkAppUpdate to check updates.

(1) Call the checkAppUpdate method.

  1. public void checkUpdate() {
  2.    AppUpdateClient client = JosApps.getAppUpdateClient(this);
  3.    client.checkAppUpdate(this, new UpdateCallBack(this));
  4. }

(2) Define the callback method for checking updates and obtain the ApkUpgradeInfo object from the callback result.

  1. private static class UpdateCallBack implements CheckUpdateCallBack {
  2.    private AppApiActivity apiActivity;
  3.    private UpdateCallBack(AppApiActivity apiActivity) {
  4.        this.apiActivity = apiActivity;
  5.    }
  6.    public void onUpdateInfo(Intent intent) {
  7.        if (intent != null) {
  8.            int status = intent.getIntExtra(UpdateKey.STATUS, DEFAULT_VALUE);
  9.            int rtnCode = intent.getIntExtra(UpdateKey.FAIL_CODE, DEFAULT_VALUE);
  10.            String rtnMessage = intent.getStringExtra(UpdateKey.FAIL_REASON);
  11.            Serializable info = intent.getSerializableExtra(UpdateKey.INFO);
  12.            if (info instanceof ApkUpgradeInfo) {
  13.                apiActivity.showLog("There is a new update");
  14.                apiActivity.apkUpgradeInfo = (ApkUpgradeInfo) info;
  15.            }
  16.            apiActivity.showLog("onUpdateInfo status: " + status + ", rtnCode: " + rtnCode + ", rtnMessage: " + rtnMessage);
  17.        }
  18.    }
  19. }

3. When detecting that an update is available, the app calls AppUpdateClient.showUpdateDialog in the callback method of checkAppUpdate to display the update pop-up.

  1. public void checkUpdatePop(boolean forceUpdate) {
  2.    AppUpdateClient client = JosApps.getAppUpdateClient(this);
  3.    client.showUpdateDialog(this, apkUpgradeInfo, forceUpdate);
  4.    Log.i(TAG, "checkUpdatePop success");
  5. }