B4A Library HMS - Huawei SDK

Huawei is the number one manufacturer of phones (https://www.statista.com/statistics...by-smartphone-vendors-since-4th-quarter-2009/). Due to USA restrictions, the new phones that they sell do not include Google Play, Google Play Services and other Google components.
They have created their own ecosystem and as developers we shouldn't ignore this ecosystem. There might be opportunities here.

Currently this library supports analytics, push notifications, DRM check, ads and in app purchases.
I want to thank @Ferdari and other developers who already built wrappers for some of HMS features. Their work did help me while building this library.

The implementation is built in such way that it will not add the HMS SDK unless the build configuration includes the HMS conditional symbol (Ctrl + B):



  1. Register as a Huawei developer: https://developer.huawei.com/consumer/en/
  2. Download the attached zip and www.b4x.com/android/files/hms_sdk.zip. Unzip them and copy all files to the additional libraries folder.
  3. Open the AppGallery Connect page and create a new project.
  4. Find the Manage APIs page and make sure that Analytics Kit and Push Kit (if needed) are enabled.


  5. Go to the General Information tab. Set the SHA-256 fingerprint. You can find it in the IDE under Tools - Private Sign Key:

  6. Download agconnect-services.json and put it in the project folder (where the b4a file is located).
  7. Add the HMS symbol to the build configuration (Ctrl + B) and make sure that the package name matches the project package name defined in AppGallery Connect.
  8. Add to main module:

    #Region HMS Libs+
    #if HMS
    #additionaljar: hms-push-
    #additionaljar: hms-agconnect-core-
    #additionaljar: hms-agconnect-credential-
    #additionaljar: hms-agconnect-https-
    #additionaljar: hms-availableupdate-
    #additionaljar: hms-base-
    #additionaljar: hms-datastore-core-
    #additionaljar: hms-device-
    #additionaljar: hms-hianalytics-
    #additionaljar: hms-Log-
    #additionaljar: hms-network-common-
    #additionaljar: hms-network-grs-
    #additionaljar: hms-opendevice-
    #additionaljar: hms-security-encrypt-
    #additionaljar: hms-security-ssl-
    #additionaljar: hms-security-base-
    #additionaljar: hms-stats-
    #additionaljar: hms-tasks-
    #additionaljar: hms-ui-
    #additionaljar: hms-update-
    #AdditionalJar: hms-hianalytics-core-
    #AdditionalJar: hms-hianalytics-framework-
    #AdditionalJar: hms-greendao-3.3.0.jar
    #AdditionalJar: hms-greendao-api-3.3.0.jar
    #AdditionalJar: hms-datastore-annotation-
    #AdditionalJar: hms-drm-
    #AdditionalJar: hms-apptouch-
    #AdditionalJar: hms-ads-banner-
    #AdditionalJar: hms-ads-base-
    #AdditionalJar: hms-ads-interstitial-
    #AdditionalJar: hms-ads-lang-
    #AdditionalJar: hms-ads-lite-
    #AdditionalJar: hms-ads-native-
    #AdditionalJar: hms-ads-reward-
    #AdditionalJar: hms-ads-splash-
    #AdditionalJar: hms-ads-template-
    #AdditionalJar: hms-iap-
    #AdditionalJar: hms-scan-
    #AdditionalJar: hms-dynamic-api-
    #AdditionalJar: hms-ml-computer-agc-inner-
    #AdditionalJar: hms-ml-computer-camera-inner-
    #AdditionalJar: hms-ml-computer-commonutils-inner-
    #AdditionalJar: hms-ml-computer-ha-inner-
    #AdditionalJar: hms-ml-computer-sdkbase-inner-
    #CustomBuildAction: folders ready, %WINDIR%\system32\cmd.exe, /c copy ..\agconnect-services.json bin\extra\assets\
    #End If
    #End Region
  9. Add to manifest editor:
    #if HMS
    CreateResourceFromFile(Macro, hms.hms)
    CreateResourceFromFile(Macro, hms.hms_push)
    CreateResourceFromFile(Macro, hms.hms_drm)
    CreateResourceFromFile(Macro, hms.hms_ads)
    SetApplicationAttribute(android:usesCleartextTraffic, "true")
    #end if
  10. Add to B4XMainPage (or the Activity for non-B4XPages projects):
    Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Private hms As HMSManager
    End Sub
    Public Sub Initialize
    End Sub
    Private Sub B4XPage_Created (Root1 As B4XView)
    Root = Root1
    End Sub
  11. Make sure that Huawei AppGallery app is installed on the target device.
  12. Run the project in debug mode. It will show analytics related logs. Move the app to the background and to the foreground a few times. You should see a message similar to:
    10-30 11:05:02.794 20298 21440 I HiAnalyticsSDK: SendTask=> events PostRequest sendevent TYPE : oper, TAG : _openness_config_tag, resultCode: 200 ,reqID:7d328467a831341dcfb442174a5c8208
  13. You should also see the traffic in:


Tips & Notes

  • The analytics logs are only redirected in debug mode.
  • In debug mode you might see an "application not responding" message from time to time. It is related to these logs. It doesn't happen in release mode.

Push notifications: https://www.b4x.com/android/forum/threads/hms-huwaei-push-kit.124100/#content


Update both the attached HMS.zip and www.b4x.com/android/files/hms_sdk.zip
  • V1.05 - HMS.ScanBitmap - scans a bitmap for known barcode types.
  • V1.04 - Support for barcode scanning: https://www.b4x.com/android/forum/threads/hms-barcode-qr-scan.133334/
    Make sure to update the dependencies list, hms_sdk and hms.zip.
  • V1.03 - Support for in app purchases. The dependencies list was updated. See this tutorial:
  • V1.02 - Support for banner and interstitial ads. The dependencies and manifest editor snippets were updated.
  • V1.01 - Missing CheckDRM method added.
  • V1.00 - Support for DRM (verification of paid apps): https://www.b4x.com/android/forum/threads/hms-huawei-drm-verification.124725/


  • hms.zip
    8.3 KB · Views: 39
Last edited:


Active Member
Licensed User