Basic steps to follow to integrate HMS capabilities in B4A

singlebubble1 profile image singlebubble ・4 min read
  • Downloading the AAR Packages and JSON File
    o Sign in to HUAWEI Developer and download the AAR packages required.
    o AAR packages related to HMS Push kit is displayed as below
    Alt Text
    Alt Text
    Figure 1-1 Downloading AAR packages
    Alt Text
    Table 1-1 Links to the AAR packages

  • Configure app information in App Gallery Connect by referring to Figure 1-2 and ensure that the required services are enabled. Refer below link for the steps.  https://developer.huawei.com/consumer/en/codelab/HMSPreparation/index.html#0
    Alt Text
    Figure 1-2 Configuring app information in App Gallery Connect

  • Open AAR packages with rar tool and rename the class.jar and AndroidManifest.xml files. (It is recommended that the two files be renamed consistently with the AAR package names.)
    Alt Text
    Figure 1-3 Renaming files

o Copy required permissions in the section in B4A IDE
Alt Text
Figure 1-4 Permissions in the section

  • Copy all configurations in the section
    Alt Text
    Figure 1-5 All configurations in the section

  • Change ${applicationId} to $PACKAGE$.
    ${applicationId} is the field for automatically obtaining the package name in Android Studio. Its counterpart on B4A is $PACKAGE$
    Alt Text

  • Download the latest configuration file(agconnect-services.json) from App Gallery Connect
    Alt Text
    Figure 1-6 Downloading the JSON file

  • Adding the JSON File to the assets Folder
    Alt Text
    Figure 1-7 Adding the JSON file

B4A will automatically incorporate files in the assets folder of an AAR package to the assets folder of your main project.

  • Encapsulating Java Files Using SLC

Configure the Java project.

SLC wraps Java project and adds it to the B4A library. For demonstration purposes, we have created a Java project.
Alt Text
It is recommended that you name the first folder b4x in src. Otherwise, the library generated with SLC may not function properly.
Alt Text
Figure 1-8 Java project

  • Develop the Java project.
    Name the Java files based on the src path.
    Alt Text
    Figure 1-9 Naming the files based on the src path

  • Import the following two lines of code to each Java file
    import anywheresoftware.b4a.BA;
    import anywheresoftware.b4a.BA.*;

  • Add necessary annotations to the Java files
    Alt Text
    Figure 1-10 Example annotations

  • Modify the context.
    B4A does not recognize the Context class. Therefore, when parsing a class that contains the @Version(1.0f) annotation, it will report an error if a method of the class has referenced Context. In this case, you need to change Context to the B4A object BA
    Alt Text
    Figure 1-11 Context example

The following describes the @Events annotation.
This annotation is used to report specific events in the Java method to the listener registered on B4A.

The code on B4A is also required to implement this function.

Take the **getPushToken** method in **PushWork** as an example. If you want to pass the push token in the Java method to a listener on B4A, do as follows:

− Set a listening event on B4A.
Alt Text
Figure 1-12 Setting a listening event

  • Add the @Event annotation to the Java files. The code is shown in the following figure.
    Alt Text

  • The value of NewToken in the red box must be the same as xxxx of Listener_xxxx in the listening event name added to B4A in the previous step.

  • Call the ba. raiseEventFromUI method in the method that needs to report the push token. The code is shown in the following figure.
      Event reporting
    Alt Text

  • Select the project folder, define the library name, and click Compile
    Alt Text
    Figure 1-13 Generating a B4A library
    Alt Text
    Alt Text
    Figure 1-14 Compilation successful

  • Select the Addition folder and add AAR packages to it.
    Alt Text
    Figure 1-15 Tools-Configure Paths

  • Go to Project > Build Configurations and change the B4A project package name to the project package name configured in App Gallery Connect
    Alt Text
    Figure 1-16 Changing the B4A project package name

  • Go to Tools > Private Sign Key and select the SHA256 signature file specified in AppGallery Connect as the B4A project signature file
    Alt Text
    Figure 1-17 Configuring the signature file

  • Declare MainApplication in the Manifest file.
     The path of MainApplication must also be the path to the src folder where MainApplication is located.
    Alt Text
    Figure 1-18 MainApplication

  • Add the #AdditionalJar field to Project Attributes to reference the AAR packages
    Alt Text
    Figure 1-19 #AdditionalJar

The value of #AdditionalJar is the name of the AAR package (with a file name extension) in the Addition folder

Posted on by:

singlebubble1 profile



live, laough and love coding, cooking and skiing


markdown guide