GMS and HMS Location Service Implementation on Single Codebase
On android app development, google services (GMS) need to be used for some features like location, map, push notification, crashlytics and analytics. But Google don’t provide these services to Huawei’s new phones. In that case Huawei’s services (HMS) need to be used instead of gms. There are several ways to implement both hms and gms. In this article we will explain how to integrate both hms and gms services in same codebase using flavors. We are going to implement location services for illustration.
PS: We will not cover whole integration processes for Huawei and Google services. In this article we will mention about how to use two services on single codebase separately.
For detailed information about
Hms integration : Huawei location codelab
Gms integration : Gms location docs
Flavor Structure
In order to manage code compilation process, we use gradle files. Flavor is one of the many structures used by gradle. Using flavors and variants, it gives us more flexibility. Establishing the architecture and implementation would be relatively more time consuming yet it is a clean approach providing a nice isolation of code. Since those ecosystems has different markets (AppGallery for Huawei), with flavors and variants, it is quite handy to establish separate build pipelines. It gives you the ability of maintaining different apk’s for different ecosystems.
To implement both gms and hms services, we need to add a new flavor dimension and two different product flavors to “build.gradle” file.
To use suitable plugin during compile process we need to add control in “build.gradle” file like below
Our flavor structure is ready now! 🤖 We are going to continue with adding dependencies.
Add Dependencies
We can separate dependencies by flavors (and apk’s). In order to achieve this we need to add flavor’s name before “implementation” keyword. Those dependencies will only be loaded when the appropriate BuildVariant is selected.
So hms dependencies won’t be loaded in apk version when gms services are used and apk size wont be increased. This rule is same for other flavors.
So far so good. Now we can add separate classes for flavor based implementation. 🌱
Location Helper for Hms and Gms
We should create hms/java and gms/java folders under src folder
Step 1
Step 2
Final appearance of src folders
And then we create LocationHelper classes per flavor.
General folder structure must be like below after created folders for hms and gms
We need to use same LocationHelper classes with different services dependencies for each flavor (gms and hms). Each class must have same methods’ declaration for single codebase usage.
Now we can write codes to LocationHelper class 👩💻👨💻
Location helper for Gms
Location helper for Hms
Our helper class is isolated from any activity/fragment and it is available to use from any activity/fragment easily. Let’s code an activity to give an example for usage of helper class. 👍
Usage sample on activity
Note:
From now on methods we use for location won’t be imported from gms or hms services, instead we use LocationHelper class. This LocationHelper class run on gms or hms services depends on compile type.
import com.example.hmsgmssinglecodebase.LocationHelper;
That’s all folks!!! Thank you for reading. 🎊 🎉
References:
- https://stackoverflow.com/questions/59974428/have-both-gms-and-hms-in-the-project
- https://medium.com/huawei-developers/guide-to-implement-mobile-services-from-different-providers-in-single-codebase-build-variants-b3610fb77fec
- https://developer.huawei.com/consumer/en/codelab/HMSLocationKit/index.html
- https://developer.android.com/training/location