Disclaimer
I will not go into the why Huawei has created Huawei Mobile Services nor if it is a good or a bad thing. This is not the purpose of this article. I will not answer to any political comment that may arise from it. I speak about technical stuff and only that :-)
Introduction
You most probably have an existing Xamarin Android application using Google Play Services. Using them with Xamarin is straightforward as Microsoft is doing the job of binding everything. It is not an easy job by the way so thanks a lot to the team!
Let’s say you need to provide a new application or convert an exsisting one to use Huawei Mobile Services.
The first thing you do as a Xamarin developer is to go to Nuget and realise that there are no Microsoft official or Huawei official Nuget packages. I tested some of the third parties one but unfortunately they did not fit my needs as they had some missing methods and the binding source code was not provided to fix it.
As I did not release any Nuget packages yet, as I had the need for those and as I have done (and still do) some pretty complex Xamarin Android bindings I decided to just do it and share everything with you.
The binding
For the project I am working on I needed 4 features:
- Displaying a map with clusterisation
- Receiving push notifications
- Scanning some QrCode
- Getting the location of the user
At the time I am writing those lines, I have created the binding of 5 Huawei SDKs:
I did not need the HiAnalytics SDK for my project. It was a need that emerged from the community so I decided to bind it as well.
Unfortunately, the clustering part of the Huawei Map Kit is not the most performant so I decided to bind a third party library that you can find here:
All of the source code can be found in this GitHub repository:
You can build the packages locally if need by using the cake script at the root of the repository. This script will take care of downloading all the necessary dependencies from Huawei. As such, the GitHub repository is free from big binaries. It will also build and package the code.
Nuget
I have uploaded the packaged bindings to Nuget. You can find them under my published packaged list:
https://www.nuget.org/profiles/JohnThiriet
Please note that those packages are still in preview. I decided to match the version number with the one of the Huawei binaries. It seemed the most logical thing to do. Because of that, as soon as I publish a final version of these packages I cannot update them without breaking this version number relationship. As such, I give some time to the community to find some missing methods etc… At one point I will republish everything in a final version.
Do not forget to activate preview packages in your IDE to use the packages.
If you click on the link above, you will see about 24 packages. You do not need to reference them all and you should not. You should only reference the main ones cited in the binding section of this article.
Packages that should not be referenced directly will have this description: “Xamarin Binding Library - ***. This SDK is a dependency of various Huawei Hms SDKs and is not intended to be used directly.”
Developing a Xamarin Android application
To have a clear example on how to integrate those packages in a Xamarin Android application I have created a sample project:
Xamarin.Android.Huawei.Hms.Demo
Some instructions are present in the repository to help you run the sample.
I still want to put the emphasis on some of most important ones here to run Hms inside your own application.
Running the application
To run the application you will need a phone with the Huawei Mobile Services. The most obvious choice for that is to have a Huawei device with App Gallery up to date.
You can install them on non-Huawei devices by installing the App Gallery on your device. and then the Hms Core from App Gallery. I will let you use your favorite search engine to find instructions on how to do it if needed.
Huawei developer account
Much like with Google Play Services, you will need a developer account that can be created for free on the Huawei developer portal. You will need to declare an application there and to activate the services you want to use.
- Create a project on the Developer portal
- Activate Location, Maps and Push SDKs or whatever is necessary for you.
- Create your keystore key and use it for signing your application.
- Add the Sha256 of the keystore to your project settings in the Huawei Developer portal.
Everything is detailed in the official documentation (link at the end of the article).
The application identifier
To use the Huawei Mobile Services you will need to modify the application manifest. Most importantly, you will need to add this line under the application element to declare your application identifier. And do not forget the replace it with the one that you find on the Huawei developer portal.
<meta-data
android:name="com.huawei.hms.client.appid"
android:value="appid=YOUR_APP_ID" />
You will also need to declare this permission just above the application element of the manifest.
<uses-permission
android:name="com.huawei.appmarket.service.commondata.permission.GET_COMMON_DATA" />
Link with Agconnect
In order for Hms to work with Xamarin there are 3 things to do.
- Download the
agconnect-services.json
found in the Huawei developer portal for application to your project’sAssets
folder and set its build property toAndroidAsset
. - Copy the
HmsLazyInputStream.cs
from the demo sample to your own project. - Copy the
XamarinCustomProvider.cs
as well.
Note the presence of the InitOrder
property in the XamarinCustomProvider
. If you do not set it, the HiAnalytics SDK will not work. It’s fine for all the other ones I used though.
Push
PushKit needs to be initialized. To ease the process simple add this line in the manifest file under the application identifier.
<meta-data
android:name="push_kit_auto_init_enabled"
android:value="true" />
Maps and location
MapKit and LocationKit need some permissions that needs to be added in the manifest as well.
<uses-permission
android:name="android.permission.INTERNET"/>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission
android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"/>
References
You can find lots of documentation including ones for Xamarin in the Huawei developers website:
https://developer.huawei.com/consumer/en/hms
Scroll down and select Cross-Platform
under the Resource Center
section to find everything related to Xamarin development.
Conclusion
Using the Huawei Mobile Services is really similar the Google Play Services, you need to declare an application there, get the Sha of your keystore and sign your application with it. They did not try to reinvent the wheel and in some cases (like the map) the APIs are the same as their Google counterparts with mostly only namespaces changes.
With that being said, it was an interesting challenge for me and I hope it will prove useful for many more people. Please use the GitHub to raise issues if you have some on the binding or the demo project.
And as always, please feel free to read my previous posts and to comment below, I will be more than happy to answer.
Comments