目录

Convertor

Overview

Convertor is a code conversion tool supporting Java and Kotlin projects. This tool can help you quickly convert the existing Android app code for calling third-party APIs into the app code integrated with the HMS Core. The tool provides the following functions to help you complete the conversion:

The following figure shows the code conversion process.

  1. Analyze: Analyzes the third-party APIs used by an app project, helping you prepare for integration.
  2. Select conversion policy: Provides a conversion policy. You can select Add HMS API or To HMS API.
    • Add HMS API: Adds the XMS Adapter, an adaption-layer code module, based on the original code for calling third-party APIs. The adapter code can call third-party APIs or HMS APIs based on the scheduling policy that you have determined. If you want the app to run on a Huawei device without third-party services, you can select the scheduling policy for using HMS APIs first.

      One advantage of this conversion policy is that you can use a set of code to call third-party APIs or HMS APIs as well as release the app in third-party app stores and on HUAWEI AppGallery at the same time. The XMS Adapter automatically generated by the tool schedules third-party APIs and HMS APIs, and separates or decouples service code from adapter code. You just pay attention to services, and do not need to spend much time on the differences between third-party APIs and HMS APIs.

      For details about the principle, please refer to Add HMS API Policy.

    • To HMS API: Replaces the third-party API code in the app code with the to-be called HMS API code. The converted code directly calls the HMS APIs.
  3. Converting: Completes the conversion on the page. One-click automatic conversion is supported.
  4. Build: Provides different packaging policies after the conversion to release your APK in different app stores.

The figure below shows the Convertor menu entry.

[h2]Development Environment Requirements

[h2]Usage Restrictions

The Convertor converts selected code, but will not convert the third-party packages on which the project is dependent.

If you import dependencies using the compile keyword in the project, change the keyword to implementation.

The following table lists the HMS API conversion supported by each kit.

Kit

To HMS API

Add HMS API

Account

Supported

Supported

In-App Purchases

Supported

Not supported

Push

Supported

Supported

Ads

Supported

Supported

Analytics

Supported

Supported

Location

Supported

Supported

Map

Supported

Supported

Game Service

Supported

Supported

Drive

Supported

Not supported

Wallet

Supported

Supported

Health

Supported

Supported

ML

Supported

Supported

Awareness

Supported

Supported

Scan

Supported

Supported

Nearby Service

Supported

Supported

Safety Detect

Supported

Supported

Dynamic Tag Manager

Supported

Not supported

Identity

Supported

Supported

Panorama

Supported

Supported

HMS Core base

Supported

Supported

FIDO

Supported

Supported

Remote Configuration

Supported

Supported

Crash

Supported

Supported

Cloud Functions

Supported

Supported

App Linking

Supported

Supported

Auth Service

Supported

Supported

App Messaging

Supported

Supported

APM

Supported

Supported

Cloud Storage

Supported

Supported

For the Add HMS API policy, you will need to make the following modifications:

  • Delete the GMS API annotations in the code.
  • For common serialized and deserialized conversions of classes (currently supported: Intent, Bundle, and Gson), perform manual conversion as required. For details, please refer to Common API Manual Conversion Guide.
  • If switch case statements exist in the code, click Convert. Convertor dynamically generates an enumeration class based on the GMS type in the case and automatically replaces the corresponding case with the enumeration constant. For details, please refer to "How do I enable automatic replacement of constants in a switch case statement?".

New Conversion

New Conversion can automatically convert the GMS APIs called by apps into HMS APIs, using either the To HMS API or Add HMS API conversion policy.
  • To HMS API: Converts GMS APIs into HMS APIs so that your app can run on any Huawei device.
  • Add HMS API: Adds HMS Core (APK) to your app. Through the bridging XMS APIs, your app can call both GMS APIs and HMS APIs. On a device that supports HMS APIs only, your app will call HMS APIs. On a device that supports both HMS APIs and GMS APIs, your app will decide which one to call first based on the preset policy. For more information about the Add HMS API policy, please refer to Add HMS API Policy.

    For the Add HMS API policy, you will need to make the following modifications:

    • Delete the GMS API annotations in the code.
    • For common serialized and deserialized conversions of classes (currently supported: Intent, Bundle, and Gson), perform manual conversion as required. For details, please refer to Common API Manual Conversion Guide.
    • If switch case statements exist in the code, click Convert. Convertor dynamically generates an enumeration class based on the GMS type in the case and automatically replaces the corresponding case with the enumeration constant. For details, please refer to How do I enable automatic replacement of constants in a switch case statement?.

[h2]Prerequisites

[h2]Analyzing a Conversion Project

The animation below illustrates the analysis and conversion process for a project.

  1. You can choose any of the following methods to start New Conversion:
    • Go to HMS > Convertor > New Conversion.
    • Click the icon on the following toolbar.

    • Press Ctrl+Alt+K.
    • Right-click the code area and choose Convertor > New Conversion from the shortcut menu.
  2. Set Project Type, Analysis Path, Excluded Path, and Backup Path as follows, click Next, and wait until the analysis is complete.

    The parameters are described as follows:
    • Project Type: type of the project to be converted.
      • App: project that can be independently compiled and executed.
      • Library: independent library that cannot run independently.
    • Analysis Path: specifies the root path of the project to be analyzed.
    • Excluded Path: specifies the path under which the files do not need to be scanned. Retain the default value, which generally includes the .svn, .git, .gradle, .idea, build, and gradle paths.
    • Backup Path: specifies whether to back up the project file before the conversion for subsequent project restoration. Only a path not in this project can be used. Files in the excluded path will not be backed up.
    • Comment out original code during conversion: comment mode. After this option is selected, comments to the original code are added after the automatic conversion in the code section.
  3. Set the conversion policy. Select To HMS API or Add HMS API as required and click Analyze to perform conversion analysis.

    If Project Type is set to App, XMS Adapter code is generated in the XMS Adapter module. You will see the following results after the analysis.

    If Project Type is set to Library, you will see the following results after the analysis.

    Key Field

    Description

    Update the following items before analysis

    Before the conversion starts, make sure to:

    1. Use JDK 1.8.
    2. Add relevant configuration if AndroidX features are involved.
    3. Check whether minSdkVersion and targetSdkVersion of the Android SDK meet the requirements of the HMS Core service currently used. For details about the version required, please refer to Gradle Information Maintenance.
    4. Update the GMS SDK to a compatible version to support conversion (no prompt is displayed if update is not required). For the minimum GMS API version required for each kit, please refer to GMS API Version Required for Each Kit.

    Analysis Result

    Statistics on GMS APIs used in the project. Click Details to view the GMS API usages in the current project.

    Add HMS API

    • Add HMS Core to your app. Through the bridging XMS APIs, your app can call both GMS APIs and HMS APIs.
    • On a device that supports HMS APIs only, your app will call HMS APIs.
    • On a device that supports both HMS APIs and GMS APIs, your app will decide which one to call first based on the preset policy.

    To HMS API

    Convert GMS APIs into HMS APIs so that your app can run on any Huawei device.

    HMS API First

    Configure your routing policy to preferentially call HMS APIs when your app is installed on devices that support HMS APIs but do not support GMS APIs.

    GMS API First

    Configure your routing policy to preferentially call GMS APIs when your app is installed on devices that support GMS APIs but do not support HMS APIs.

    Dependent GMS APIs

    GMS APIs and GMS methods used in the current project.

    Convertible GMS Methods

    The number of GMS methods that can be converted automatically and manually, and the automatic conversion rate, under the two conversion policies.

    Unconvertible GMS APIs

    GMS APIs that are not compatible under each conversion policy. Select an appropriate conversion policy based on this result.

    Unconvertible GMS Methods

    The number of GMS methods that are not supported by HMS. Click Details to view more information.

    Select a conversion policy

    Conversion policy. In the Add HMS API policy, you can set to prioritize HMS APIs or GMS APIs.

    Generate code for creating app dependent only on GMS SDK

    Under the Add HMS API policy, the ability to generate differentiated adapter code is provided to help developers build APKs dependent only on GMS. If this option is selected, a prompt will be displayed. Click the link in the prompt to view details in the document.

    Generate code for creating app dependent only on HMS SDK

    Under the Add HMS API policy, the ability to generate differentiated adapter code is provided to help developers build APKs dependent only on HMS. If this option is selected, a prompt will be displayed. Click the link in the prompt to view details in the document.

    Path for storing the generated code (for Add HMS API only)

    Under the Add HMS API policy, XMS Adapter code is stored in the XMS Adapter module in the root path of the project.

    Export analysis result

    Statistics on GMS APIs used in the project. Click Export analysis result to export the GMS API usages in the current project.

  4. (For Add HMS API) Initialize the route in the project.

    Based on the HMS API or GMS API routing policy selected during conversion, you need to call the GlobalEnvSetting.init method in the project to determine whether the app will run in the HMS or GMS environment. For details about how to set the route, please refer to Route Initialization Policy in the Add HMS API Scenario.

    This method can be used to check whether the current mobile phone can run HMS or GMS.

  5. Wait until the result is displayed, as shown in the figure below. The following table describes key fields in the result.

    Key Field

    Description

    Show Converted

    • Selected: displays all records of GMS APIs including converted and unconverted ones.
    • Deselected: displays records of GMS APIs that have not been converted.
    NOTE:

    Only the results for the current analysis are displayed. If you scan the project again after processing it, the records of the last scan cannot be viewed.

    Convert

    Converts the selected records.

    Revert

    Undoes automatic conversion of selected items. Currently, pressing Ctrl+Z to undo the conversion is not supported.

    Total

    Total number of items to be processed.

    Converted

    Total number of records of GMS APIs that have been converted.

    Conversion Type

    Conversion types available for the scanning results. The options are as follows:

    • Auto: This item will be automatically converted.
    • Dummy: HMS does not support this method. The XMS Adapter encapsulates only the GMS method. You need to confirm whether to retain or delete this method in or from HMS. After the confirmation, you can start an automatic conversion.
    • Manual: This item requires manual conversion.

    Content

    Original content to be converted.

    Description

    Conversion description.

  6. View the detailed analysis result of each conversion item in either of the following ways:
    • Method 1: Click the items to be converted to go to the corresponding code in the project.
    • Method 2: Double-click the item to be automatically converted. The code differences before and after the conversion are displayed.

[h2]Processing Analysis Results

The animation below illustrates how to process analysis results.

According to the analysis results, some GMS APIs can be automatically converted by the IDE, while others require manual conversion.

The Auto and Dummy types can be converted in one of the following ways:

Conversion method for the Manual type: Click the item to be converted to have the corresponding code displayed, then modify based on the description in the scanning results. After the modification, manually change the check box of the item from to .

After all the items are converted and the status changes to or , a message is displayed, asking you whether to check the integrity. Click Jump to go to the integrity check page.

Manual Instruction (currently, only Map Kit and Location Kit support this function):

In the Add HMS API scenario, you will need to implement the code logic conversion based on the service context. The following APIs are used in Add HMS API scenarios, and may be involved during the conversion of all kits. For details about how to use the APIs, please refer to the manual conversion guide of each kit, which can be obtained by clicking the Description link in the manually modified item.

API

Description

org.xms.g.utils.GlobalEnvSetting.isHms()

Determines the route during manual conversion.

org.xms.g.utils.GlobalEnvSetting.useGms()

Sets the GMS route for dynamic route switchover after sign-in.

org.xms.g.utils.GlobalEnvSetting.useHms()

Sets the HMS route for dynamic route switchover after sign-in.

Object getGInstance()

Obtains the corresponding GMS or HMS instance from the Adapter instance.

Object getHInstance()

org.xms.g.utils.Utils.getXmsObjectWithGmsObject(Object object)

Obtains the corresponding Adapter instance from the GMS instance.

  • Input parameter: GMS instance
  • Output parameter: corresponding Adapter instance

org.xms.g.utils.Utils.getXmsObjectWithHmsObject(Object object)

Obtains the corresponding Adapter instance from the HMS instance.

  • Input parameter: HMS instance
  • Output parameter: corresponding Adapter instance

isInstance

Called by the IDE plug-in to replace the instanceof keyword in Java and convert obj instanceof G to X.isInstance(obj).

dynamicCast

Called by the IDE plug-in to forcibly convert the Java type from (G)obj to X.dynamicCast(obj).

[h2]Compiling the Project

After all records are processed, compile the file. After the compilation is successful, the conversion is complete.

Open Last Conversion

Go to HMS > Convertor > Open Last Conversion to open the last conversion results, that is, the last conversion analysis results.

If you have not finished processing all conversion items, but have closed Android Studio or the project, and reopened the project, you can load the project records in this way and go on with the conversion. It is not recommended that you use this method in other scenarios.

Save All

You can save the project and conversion information.

For a large-scale project, if you have performed conversion for some items, and wish to back up the project code in progress and conversion list, click Save All. You can then click Restore Project to restore the project from the backup progress and continue conversion.

The backup path is set during the last scan, and the backup .zip file is named in format of project name+timestamp+comment mode+conversion policy+conversion progress.

Restore Project

Go to HMS > Convertor > Restore Project to open the restoration page of the backup project, and restore the file in the archive path to the required path.

After restoring the backup file, manually select the current project path in the work tree, right-click the path and click Reload from Disk to update it. There are two types of backup files:

Compare XMS Code

If you have modified XMS Adapter code or added kits, Convertor will generate new XMS Adapter code based on the new build.gradle file and is also able to show you the differences between the old and new XMS Adapter code. The following illustrates the two scenarios where new XMS Adapter code is generated.

[h2]Scenario 1

Modify XMS Adapter code and generate new XMS Adapter code. New XMS Adapter code is generated in the XMS Adapter module, and the old code is backed up. Additionally, Convertor can compare the old and new code.

  1. Use New Conversion to regenerate XMS Adapter code and specify the path where the code will be generated.

  2. If the following information is displayed, the XMS Adapter code is regenerated successfully. Click OK.

  3. Click Open backup folder to open the path where the old XMS Adapter code is backed up, and click Open new folder to open the path where the new code is generated. The comparisons between old files and new files are displayed.
  4. Double-click a file to view the content differences.
  5. Manually merge the two sets of code to the XMS Adapter as needed.

[h2]Scenario 2

Add a kit in your project using Repository and generate XMS Adapter code.

You can use Repository only after the project initialization is complete.

  1. Select the kit you want to add.
  1. Manually merge the old and new code, just like in scenario 1.