Photo by Alex Escu on Unsplash

Developing HarmonyOS Application as Android Developer

Add HMS to support your android application in Huawei devices

Jimly Asshiddiqy
ProAndroidDev
Published in
6 min readJan 13, 2022

--

Background

As of 2019, new Huawei devices are banned from using Google services. This ban means that Huawei devices can no longer access API provided by Google like Maps API, or using Firebase as platform for developing Huawei Devices.

This doesn’t mean Huawei can no longer manufacture devices with Android Operating System because Android itself is open-source. But a year later, Huawei announced that they are releasing a new operating system called HarmonyOS for Huawei devices.

What does that mean for us as Android Developer? Do we have to support new operating system? Or how do we maintain our android application to support HarmonyOS?

Let’s learn more about it together!

“Brand New” Operating System

It’s safe to presume that Huawei will no longer releasing new mobile device with Android because they have their own operating system now. But developing a new operating system is hard, how do they able to do that in one year?

Android x HarmonyOS

Well, the easy answer is they don’t… But kinda is. So far as I can tell, it really looks like HarmonyOs is forked version of Android. HarmonyOS is built on top of AOSP (Android Open Source Project), so it have some similarities with Android OS. My company sent me HarmonyOS device to test, so I just run simple Android program on it and it works.

So my hypothesis is:

Because HarmonyOS works the same as Android and basically other operating system based on AOSP. All your UI/Presentation code will work as intended in HarmonyOS devices. Things like View configuration, or Activity and Fragment , you can still use all of that.

Knowing that, my goal is supporting my already-released Android Application for HarmonyOS with little modification as possible. Which basically mean, no creating new project or repo or branch (in git) for HarmonyOS, no downloading new software if possible.

Speaking of downloading new software, Huawei have their own IDE for developing HarmonyOS apps called HUAWEI DevEco Studio. But I’m not going to install it, but if you do… You can download it here:

Or if you’re being lazy, you can install their plugin called HMS Tookit from settings in Android Studio.

HMS Toolkit plugin from marketplace

This plugin enables you to develop application using Huawei Mobile Service (HMS), and have feature like:

  • Cloud Debugging, If you don’t have Huawei device… You can reserve time to run your app in remote Huawei device
  • Location Kit Tool, Mock location for Huawei location kit (Google’s Maps API alternative)

Firebase Alternative

As said in the beginning, some Firebase features might be not supported in HarmonyOS because it used Google Mobile Services. You can read more about that here.

This table represent which Firebase feature might be affected in HarmonyOS devices:

Firebase features that require Google Play Service

The other features like Crashlytics or Performance Monitoring should working just fine in HarmonyOS devices.

But again, how do we still accomplish the same functionality without Firebase?

It turns out Huawei also releasing platform for developing mobile and web application called AppGallery Connect. Which after some research and readin their documentation, it’s basically the same feature as Firebase but with different name.

In this table, I provided some features of Firebase and AppGallery Connect:

Features in Google and Huawei

Okay, so we already know about AppGallery Connect. And now, how to start a project in there? Well, you have to register an account first for HMS console. Register your account here.

Developer Type

There’s two type of developer type there:

  • Individual, if you are an indie developer
  • Enterprise, if you are releasing the app as a company
Upload identification

Another things to prepare when registering a developer account is they ask you for identification like:

  • ID Card released by the government or Driver’s license
  • Bank Document

I don’t know why they need all of those things for registering an account, but this is the way it is. Then, it will take up to two days for them to validate your information. And only then, you can start using the feature of AppGallery Connect.

AppGalleryConnect console

That is the console of your project. Maintaining the project will somewhat easy if you already familiar with Firebase console. Project hierarchy is not so different with Firebase where in a project you can have multiple apps registered to it.

Registering your app to your project feels familiar, like adding your SHA-256 signing, downloading a JSON file and add it to your project.

If Firebase use google-services.json , AppGallery Connect use agconnect-services.json . Just put the agconnect-services.json where you put your google-services.json.

Implementation

Platforms supported by AppGallery Connect

Any feature in AppGallery Connect brings SDK to be implemented to your app project. But as we can see here, there’s different SDK for HarmonyOS and Android. What do we choose?

I predicted that we can just use the Android SDK to run in HarmonyOS. And I was right, the app will run just fine in HarmonyOS even using the Android version of the SDK.

But comes the question, how do you support two services for the same app without breaking too much changes?

Some developers use build flavor to differ it, like explained here:

But it takes too much time, because we will have to create two classes for each flavors in the same functionality. And it also mean we have to build different APKs for Android and HarmonyOS

My solution is to use abstraction and inheritance, where we can just define the functionality from the SDK in super class. And implementing in sub class separately by their services. Read more about that here:

Conclusion

Adding HMS to your Android Project is not as hard as you might think. Although navigate through the documentation and console took times, me personally took two days to understand which menu to click for the action I want.

Developing is easy because your presentation layer basically doesn’t change, the thing to consider is which Google Service used in your app and know the Huawei alternative to implement.

--

--

𝗶 𝗲𝗱𝗶𝘁 𝘁𝗲𝘅𝘁 𝗳𝗶𝗹𝗲𝘀 𝗳𝗼𝗿 𝗮 𝗹𝗶𝘃𝗶𝗻𝗴.