We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All

Documents from this version have been archived, and will not continue to be maintained. Please use the latest version.

HMS Core GuidesPush KitApp Development (Archived)Android App (Archived)Preparations (Archived)

Preparations

This section introduces preparations for accessing HUAWEI Push Kit.

The preparations are as follows:

1. Configure app information in AppGallery Connect.

2. Integrate the HMS SDK.

3. Configure the manifest file.

4. Configure obfuscation scripts.

1. Configuring App Information in AppGallery Connect

Before you start developing an app, configure app information in AppGallery Connect.

1.1 Registering as a Developer

Before you get started, you must register as a HUAWEI developer and complete identity verification on the HUAWEI Developer website. For details, please refer to Register a HUAWEI ID.

1.2 Creating an App

Create an app by referring to Creating a Project and Creating an App in the Project. Set the following parameters as described:

  • Platform: Select Android

  • Device: Select Mobile phone

  • App category: Select App or Game

1.3 Generating a Signing Certificate Fingerprint

A signing certificate fingerprint is used to verify the authenticity of an app when the app attempts to access HMS Core (APK) through the HMS SDK. Before using HMS Core (APK), you must locally generate a signing certificate fingerprint and configure it in AppGallery Connect.

Please ensure that the following conditions are met:

Perform the following steps:

1. Open the command line tool (using the cmd command) and run the cd command to go to the directory where keytool.exe is located. In the following example, the Java program is installed in Program Files (x86) in the C drive.

     

2. Run keytool -list -v -keystore <keystore-file> and respond as prompted.

In the preceding command, <keystore-file> is the complete path to the app's signature file.

Example:

Collapse
Word wrap
Dark theme
Copy code
  1. >keytool -list -v -keystore E:\HmsSample\android.keystore

3. Obtain the SHA-256 fingerprint from the result.

1.4 Configuring the Signing Certificate Fingerprint

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

2. Find your project from the project list and click the app on the project card.

3. On the Project Setting page, set SHA-256 certificate fingerprint to the SHA-256 fingerprint from Generating a Signing Certificate Fingerprint.

4. After completing configuration, click . The configuration takes effect in about 15 minutes.

1.5 Setting Data Storage Location

To use HUAWEI Push Kit, you must set the data storage location. For details, please refer to Setting Data Storage Location

If the data storage location is not set, the following functions will be unavailable: topic-based messaging, messaging through the WebPush agent, and messaging to iOS apps. If the data storage location is different from your server location, or is different from the location of a user that the app serves, messaging delay will increase. To reduce the delay caused by messaging across regions around the world, you are advised to set the data storage location based on the location of a user that the app serves. For example, if you are a developer in Latin America and most of your app users are in Latin America, you are advised to set your data storage location to Singapore. If you are a developer in the Chinese Mainland but most of your app users are in Europe, you are advised to set your data storage location to Germany.

1.6 Enabling Required Services

2. Integrating the HMS Core SDK

2.1 Integrating the HMS Core SDK in Your App Project in the Android Studio IDE

If you are using Android Studio, you can integrate your HMS Core SDK by using the Maven repository. Before you start developing an app, integrate the HMS Core SDK into your Android Studio first.

Adding the AppGallery Connect Configuration File of the App

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

b. Find your project from the project list and click the app on the project card.

c. On the Project Setting page, click agconnect-services.json to download the configuration file.

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

Configuring the Maven Repository Address for the HMS Core SDK

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 the HMS Core SDK.

Collapse
Word wrap
Dark theme
Copy code
  1. allprojects {
  2.    repositories {
  3.        google()
  4.        jcenter()
  5.        maven {url 'https://developer.huawei.com/repo/'}
  6.    }
  7. }

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

Collapse
Word wrap
Dark theme
Copy code
  1. buildscript {
  2.    repositories {
  3.        google()
  4.        jcenter()
  5.        maven {url 'https://developer.huawei.com/repo/'}
  6.    }
  7. }

d. Go to buildscript > dependencies and add dependency configurations.

Collapse
Word wrap
Dark theme
Copy code
  1. buildscript {
  2.    dependencies {
  3.        classpath 'com.huawei.agconnect:agcp:1.3.1.300'
  4.    }
  5. }
Note:
The Maven repository address cannot be accessed from a browser. It can only be configured in the IDE.

Adding Build Dependencies

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

b. Configure build dependencies.

Collapse
Word wrap
Dark theme
Copy code
  1. dependencies {
  2. // Retain other existing dependencies.
  3. implementation 'com.huawei.hms:push:{version}'
  4. }
Note:
Replace {version} with the actual SDK version number, for example implementation 'com.huawei.hms:push:4.0.4.301'.

c. Add the authentication configuration to the file header.

Collapse
Word wrap
Dark theme
Copy code
  1. apply plugin: 'com.huawei.agconnect'

Configuring a Signature

Copy the signature file generated in Generating a Signing Certificate Fingerprint to the app directory of your project and configure the signature in the build.gradle file.

Collapse
Word wrap
Dark theme
Copy code
  1. android {
  2.     signingConfigs {
  3.         config {
  4.             keyAlias 'xxx'
  5.             keyPassword '123456'
  6.             storeFile file('xxx.jks')
  7.             storePassword '123456'
  8.         }
  9.     }
  10.     buildTypes {
  11.         debug {
  12.             signingConfig signingConfigs.config
  13.         }
  14.         release {
  15.             signingConfig signingConfigs.config
  16.             minifyEnabled false
  17.             proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  18.         }
  19.     }
  20. }

Synchronizing the Project

Open the modified build.gradle file again. You will find a Sync Now link in the upper right corner of the page. Click Sync Now and wait until synchronization is complete.

Note:
If an error occurs, check the network connection and the configurations in the build.gradle file.

Defining Multi-language Settings

● By default, your app supports all languages provided by the HMS Core SDK. If your app uses all these languages, skip the operation procedure in this section.

● If your app uses only some of these languages, follow the operation procedure in this section to complete the required configuration.

Open the build.gradle file in the app directory of your project.

Go to android > defaultConfig and add the resConfigs configuration. en (English) and zh-rCN (Simplified Chinese) are mandatory. For example, if your app supports only English and Simplified Chinese, the configuration is as follows:

Collapse
Word wrap
Dark theme
Copy code
  1. android {
  2.        defaultConfig {
  3.                ...
  4.                resConfigs "en", "zh-rCN"
  5.        }
  6. }

For details about the languages supported by the HMS SDK, please refer to Languages Supported by HMS SDK.

2.1 Integrating the HMS Core SDK into Your App Project in the Eclipse IDE

If you are using Eclipse, you can download the HMS Core SDK and integrate it into your Eclipse project.

Downloading the SDK

1. Download and decompress the ZIP packages of the HMS Base SDK and SDKs of the required kits. For details, please refer to Eclipse SDK Download.

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, selectCopy projects into workspaceto 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 Android from the navigation tree. Then click Add in the Library area, 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. Because 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.

Collapse
Word wrap
Dark theme
Copy code
  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 Resource Files to Your Project Directory

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

Note:

If any other kits are involved, also copy their resource files to your project directory.

Configuring the project.properties File

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

Collapse
Word wrap
Dark theme
Copy code
  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 click My projects.

2. Find your app project and click the app that needs to integrate the HMS Core SDK.

3. Go to Project Setting > Convention. In the App information area, download the agconnect-services.json file.

4. Because the Eclipse IDE does not support the AppGallery Connect plug-in, 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, create a subclass that extends the Application subclass, for example, MyApplication.

Collapse
Word wrap
Dark theme
Copy code
  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. }

After that, import the following classes:

Collapse
Word wrap
Dark theme
Copy code
  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;

5. 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.

Collapse
Word wrap
Dark theme
Copy code
  1. <application
  2.    android:name=".MyApplication"
  3.    
  4.    >

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

3. Configuring the Manifest File

After integrating the HMS Core Push SDK of the latest version, register Service under application in the AndroidManifest.xml file so that devices can receive the push token and messages sent by the HUAWEI Push Kit server.

Register DemoHmsMessageService:

Define the DemoHmsMessageService class (the class name is only for reference and can be customized) by extending com.huawei.hms.push.HmsMessageService, and implement its methods.

Collapse
Word wrap
Dark theme
Copy code
  1. <service
  2.      android:name=".DemoHmsMessageService"
  3.      android:exported="false">
  4.      <intent-filter>
  5.            <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
  6.      </intent-filter>
  7. </service>
Notice:

Set exported to false.

4. Configuring Obfuscation Scripts

Configure obfuscation scripts before compiling the APK to prevent the HMS SDK from being obfuscated. If obfuscation arises, the HMS SDK may not function properly.

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

1. Open the obfuscation script file of your project.

2. Add configuration to exclude the HMS SDK from obfuscation.

Collapse
Word wrap
Dark theme
Copy code
  1. -ignorewarning
  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.**{*;}

3. If you have used AndResGuard, add it to the allowlist in the obfuscation script file.

Collapse
Word wrap
Dark theme
Copy code
  1. "R.string.hms*",
  2. "R.string.connect_server_fail_prompt_toast",
  3. "R.string.getting_message_fail_prompt_toast",
  4. "R.string.no_available_network_prompt_toast",
  5. "R.string.third_app_*",
  6. "R.string.upsdk_*",
  7. "R.layout.hms*",
  8. "R.layout.upsdk_*",
  9. "R.drawable.upsdk*",
  10. "R.color.upsdk*",
  11. "R.dimen.upsdk*",
  12. "R.style.upsdk*",
  13. "R.string.agc*"


This page may contain third-party content. For details, click here.
Search in Guides
Enter a keyword.