👨‍💻Android Mastering the Integration: Coexisting Google and Huawei Mobile Services in a Single Codebase

How to handle Google and Huawei Mobile Services in same code base? 🕵️‍♀️

Hüseyin Özkoç
Huawei Developers
4 min readJun 20, 2023

--

How to use GMS and HMS at the same time with one code base in Android?

Managing HMS and GMS libraries within our Android application can make our application more complex over time. So we will talk about how to overcome this situation easily!

Inroduction

Hello dear Android developers, today I will give you information about using HMS and GMS libraries over a single code base. We will also make an example of using Google Maps and Huawei Map Kit in the same project as an example. If you’re ready, let’s start!

First of all, we need to add our dependencies to our project.

We need to add our dependencies to our project.

Then we need to create our related functions that check whether the services of GMS and HMS are active on our phone.

Checks if Google Services is available on our phone.
Checks if Huawei Services is available on our phone.

According to the true and false values returned from the above functions, we can decide which services will work on our phone or not. Now we will try our functions only on phones with Google services and only Huawei services.

As you can see, when we tried it on a phone with only Google services, the Google service returned true, while the Huawei services function returned false. (Pixel 3 was used as the phone.)
Also, when we tried it on a phone with only Huawei services, the Huawei service returned true, while the Google services function returned false. (P40 Lite was used as the phone.)

As seen in the examples above, we can perform operations with our relevant functions depending on which service is active or not on the phone at that moment.

When we do the relevant checks, we can add Google Maps and Huawei Map Kit to our project. Let’s start by preparing the XML side first.

Defining the Google and Huawei map that will appear on the screen on the XML side.

Then we add our OnMapReadyCallback for both Google Maps and Huawei Map to our class in the relevant Activity. Additionally, we should add our onMapReady functions for the two map services to our class.

Add OnMapReadyCallback for both Google Maps and Huawei Map to our class in the relevant Activity.
We should add our onMapReady functions for the two map services to our class.

Finally, you can use the service you need in your project, depending on the working status of the relevant Google and Huawei services on your phone.

Use the service you need in your project, depending on the working status of the relevant Google and Huawei services on your phone.

Now let’s look at the results. First, let’s try our application on the Pixel 3 phone with only Google Services. We will also try it later on the P40 Lite phone that only includes Huawei services.

As you can see, we only use Google Maps on our phone, which includes Google services.
We use Huawei Map Kit in the same application and only on the phone that includes Huawei services.

Key Points

  • Do not forget to create a separate logic for phones that use both Huawei and Google services.
  • Do not forget to activate the console for Google Maps and Huawei Maps. (Google Console for Google Map service, App Gallery Connect for Huawei Map service)
  • Don’t forget to put your API Key for Google Maps in AndroidManifest. Likewise, don’t forget to call your MapsInitializer.setApiKey(“YOUR_API_KEY”) function in Huawei Map Kit.
  • For Huawei Map Kit, versions above ‘com.huawei.hms:maps:5.0.5.301’ may have a map not loading error. Make sure you choose your version correctly or use this version.
  • When working with Google Map and Huawei Map values, do not forget to import values from the relevant class for the relevant service. Because most of the value and function names are similar in the two services.
  • Never forget to add the SHA1 and SHA-256 values of your application to the console sides of Google and Huawei services. Also, create SHA1 and SHA-256 again for release for services to work properly in release packages. (Execute gradle signinReport from Android Studio terminal to generate SHA1 and SHA-256.)
  • Make sure that the most up-to-date versions of the service JSON files you will download from the console are attached to the project.

Conclusion

As a result, the ability to confirm the availability of Google and Huawei services for specific phones has brought about a significant leap in our productivity. These features ensure harmonious integration, empowering us to leverage pertinent services using a single code base effortlessly. Furthermore, they grant us the liberty to incorporate and modify the use of Google and Huawei services within our application as needed.

References

--

--