[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum

Original article link: https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201326096858500005&fid=0101187876626530001

Xamarin with HMS

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812074535.69381856618405481235135598207477:50510828023529:2800:6E41861DD70C40B8FFEF7683154E01D1BC41F236AC78BE1E716D79F49FBB9CF9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812074535.69381856618405481235135598207477:50510828023529:2800:6E41861DD70C40B8FFEF7683154E01D1BC41F236AC78BE1E716D79F49FBB9CF9.png[/IMG]​

Xamarin : Cross-platform. Open source. An app platform for building Android and iOS apps with .NET and C#.

For now we don't have an official nuget plugin for Xamarin so you need to use the native Android sdk so you need to know about :

Binding a Java Library

Overview

The third-party library ecosystem for Android is massive. Because of this, it frequently makes sense to use an existing Android library than to create a new one. Xamarin.Android offers two ways to use these libraries:

Create a Bindings Library that automatically wraps the library with C# wrappers so you can invoke Java code via C# calls.

Use the Java Native Interface (JNI) to invoke calls in Java library code directly. JNI is a programming framework that enables Java code to call and be called by native applications or libraries.

so in briefwe can say if we used the Binding Native library we will convert the sdk from the java code to c# code to being used in your xamarin project , the binding project will contains a wrapper that will convert the jave code to the C# code .

The question now as we know there is a lot of different between C# and Java so should i have any issue after the wrapper job ??

the answer for this yes because you will find some language rules will not be included in C# or maybe you will find something can be implement in Java but in C# have different way to be implement and in some case multiple way ,so the wrapper will make a lot of hard work to you but in some times you need to modify the wrapper result to make the code build-able and compiled with no issues ,

When binding an existing Android library, it is necessary to keep the following points in mind:

Are there any external dependencies for the library? – Any Java dependencies required by the Android library must be included in the Xamarin.Android project as a ReferenceJar or as an EmbeddedReferenceJar. Any native assemblies must be added to the binding project as an EmbeddedNativeLibrary.

What version of the Android API does the Android library target? – It is not possible to "downgrade" the Android API level; ensure that the Xamarin.Android binding project is targeting the same API level (or higher) as the Android library.

What version of the JDK was used to compile the library? – Binding errors may occur if the Android library was built with a different version of JDK than in use by Xamarin.Android. If possible, recompile the Android library using the same version of the JDK that is used by your installation of Xamarin.Android.

Now from where can i got the SDK

You will find a direct link for some SDKs at our website with (.arr) or (jar) extensions some times you need multiple jars and arr for one SDK for example if you want to use the Ads Kit you will find the following files to get the full SDK:

Ads Banner (.arr) or (jar) extensions

Ads Base (.arr) or (jar) extensions

Ads Consent (.arr) or (jar) extensions

Ads Interstitial (.arr) or (jar) extensions

Ads Lan (.arr) or (jar) extensions

Ads Lite (.arr) or (jar) extensions

Ads Native (.arr) or (jar) extensions

Ads Reward (.arr) or (jar) extensions

Ads Splash (.arr) or (jar) extensions

Ads Template (.arr) or (jar) extensions

after you get these files you need to create Bindings Native Library for each project and then add the file (.arr) or (jar) inside the project and 　　then compile it to get the SDK as DLL as following :

1.Create new project

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812083155.45069111179463446219336540565969:50510828023529:2800:A8248873E9E0C787DB9569A00634DFC21586C33A48D049272983A4BC8AE18748.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812083155.45069111179463446219336540565969:50510828023529:2800:A8248873E9E0C787DB9569A00634DFC21586C33A48D049272983A4BC8AE18748.png[/IMG]

2.Select android bindings library project

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812083542.36676713922935319117923537421372:50510828023529:2800:EEF0E7D987EB8B76EE5CAA88CF4E6C474411BDCE3FF3F0E6DED69D11B404CCAF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812083542.36676713922935319117923537421372:50510828023529:2800:EEF0E7D987EB8B76EE5CAA88CF4E6C474411BDCE3FF3F0E6DED69D11B404CCAF.png[/IMG]

3.Configure your new project

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812083752.42204227707046356183620219159142:50510828023529:2800:019B57378319C7F088D72B012C306B62DA7A3723B1F746F8E2AD722826B745CF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812083752.42204227707046356183620219159142:50510828023529:2800:019B57378319C7F088D72B012C306B62DA7A3723B1F746F8E2AD722826B745CF.png[/IMG]

4.Finally you will have the following project structures

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812084031.98180830769008883860941927666020:50510828023529:2800:486A68C7E45B5D779FA346A91C3DE71A0842B5671BDC9475D323C929F0D80194.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812084031.98180830769008883860941927666020:50510828023529:2800:486A68C7E45B5D779FA346A91C3DE71A0842B5671BDC9475D323C929F0D80194.png[/IMG]

You can add all the projects in one solution like below

by right click on the soultion => add new project and re-follow all the steps before

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812084739.62311214007494793424175439449633:50510828023529:2800:C15D35045A6BA6870FE3EBEC35267F1E452E3C066F79AD58C80F3C2A19C77E90.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812084739.62311214007494793424175439449633:50510828023529:2800:C15D35045A6BA6870FE3EBEC35267F1E452E3C066F79AD58C80F3C2A19C77E90.png[/IMG]

After doing this you need to take the (.arr) or (jar) and adding it to the Jars folder and change the Build Actions as following :

EmbeddedJar – Embeds the .jar into the resulting Bindings Library DLL as an embedded resource. This is the simplest and most commonly-used build action. Use this option when you want the .jar automatically compiled into byte code and packaged into the Bindings Library.

InputJar – Does not embed the .jar into the resulting Bindings Library .DLL. Your Bindings Library .DLL will have a dependency on this .jar at runtime. Use this option when you do not want to include the .jar in your Bindings Library (for example, for licensing reasons). If you use this option, you must ensure that the input .jar is available on the device that runs your app.

LibraryProjectZip – Embeds an .AAR file into the resulting Bindings Library .DLL. This is similar to EmbeddedJar, except that you can access resources (as well as code) in the bound .AAR file. Use this option when you want to embed an .AAR into your Bindings Library.

ReferenceJar – Specifies a reference .jar: a reference .jar is a .jar that one of your bound .jar or .AAR files depends on. This reference .jar is used only to satisfy compile-time dependencies. When you use this build action, C# bindings are not created for the reference .jar and it is not embedded in the resulting Bindings Library .DLL. Use this option when you will make a Bindings Library for the reference .jar but have not done so yet. This build action is useful for packaging multiple .jars (and/or .AARs) into multiple interdependent Bindings Libraries.

EmbeddedReferenceJar – Embeds a reference .jar into the resulting Bindings Library .DLL. Use this build action when you want to create C# bindings for both the input .jar (or .AAR) and all of its reference .jar(s) in your Bindings Library.

EmbeddedNativeLibrary – Embeds a native .so into the binding. This build action is used for .so files that are required by the .jar file being bound. It may be necessary to manually load the .so library before executing code from the Java library. This is described below.

but usually we will use EmbeddedJar and LibraryProjectZip .

you can change build action like this

right click at the file it self like this and click on properties

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812100438.96241387724206135896303695925551:50510828023529:2800:9B88E3B54615E9477DAC5CF8044A8748A465A6536414E05014EAE3106EE1B9EB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812100438.96241387724206135896303695925551:50510828023529:2800:9B88E3B54615E9477DAC5CF8044A8748A465A6536414E05014EAE3106EE1B9EB.png[/IMG]

2.then you will note the properties tab like this

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812100817.33012607400340919515728198872217:50510828023529:2800:08B845DEC08ECCB561F82EB1CBFCDCDBE706F4CE31BFCEFA3F70B9164BAFC8EA.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812100817.33012607400340919515728198872217:50510828023529:2800:08B845DEC08ECCB561F82EB1CBFCDCDBE706F4CE31BFCEFA3F70B9164BAFC8EA.png[/IMG]

And then you should choose from the build action a LibraryProjectZip for aar files and EmbeddedJar for jar files.

After this you need to build the project by right click on the project and build as below.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812101242.62640266024650951546862711034592:50510828023529:2800:69C6E71EBAA52C375D92D47B921E6758E2C2FC6FA27C2331308F7F775AC50518.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812101242.62640266024650951546862711034592:50510828023529:2800:69C6E71EBAA52C375D92D47B921E6758E2C2FC6FA27C2331308F7F775AC50518.png[/IMG]

After the build completed without errors you should go to the obj folder it will be hiden from the solution so maybe you need to open the folder from by the path and you will find the obj folder like this

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812110010.58451404229376758309884530679582:50510828023529:2800:2E8DDE268BCB02BB0A05163D7782E4E84F141CDF669B210B2066375E93C0E5B7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812110010.58451404229376758309884530679582:50510828023529:2800:2E8DDE268BCB02BB0A05163D7782E4E84F141CDF669B210B2066375E93C0E5B7.png[/IMG]

and you will find the DLL file inside your debug folder with the same name of your project that contains the SDK in C# code and then you can go to you main project and add this Dll as a reference and if you want to know if it compiled fine you can go to objDebuggeneratedsrc and this folder shouldn't be empty like the below

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812110748.87862129411225306123312950660524:50510828023529:2800:408E52CFC6FEB903BE9EA07498E0067D89347F5F588432DB4CF6B909B78CF4AF.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812110748.87862129411225306123312950660524:50510828023529:2800:408E52CFC6FEB903BE9EA07498E0067D89347F5F588432DB4CF6B909B78CF4AF.png[/IMG]

In case you have a build errors and you follow these steps in correct way it could be for tow reason

you have error related to the Java rules can't be done from the wrapper it self like name of object or return type ..ect then you need to fix these error using the metadata.xml file

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812111450.39914188287930606396049377499928:50510828023529:2800:27E551A27DA08D3E6EB74215DEF14890CA86FD2036F14EE6125510146E5E24C7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/000/364/721/2640962000000364721.20200812111450.39914188287930606396049377499928:50510828023529:2800:27E551A27DA08D3E6EB74215DEF14890CA86FD2036F14EE6125510146E5E24C7.png[/IMG]

to know more about these error and how to fix them you need to the following link its a line of code to manage how the wrapper handle this code https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/customizing-bindings/java-bindings-metadata

the second one your project depends on another dll to be complete for example when you use the ADs kit the Ads Banner need a refrance from Ads Base if not you will see error display can't find class with name so you need to know how to refernace your projects together

Conclusion

you need to setup your SDK on C# to enable you access the functions and method by using Binding a Java Library

Reference

https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/customizing-bindings/java-bindings-metadata

https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/

https://developer.huawei.com/consumer/en/doc/HMS-Plugin-Library-V1/xamarin-sdk-download-0000001050175494-V1

https://developer.huawei.com/consumer/en/doc/HMS-Plugin-Guides-V1/creating-xamarin-android-binding-libraries-0000001050267972-V1