Skip to content

Latest commit

 

History

History
179 lines (100 loc) · 7.8 KB

04.dotNETMAUIBinding.md

File metadata and controls

179 lines (100 loc) · 7.8 KB

.NET MAUI binding Native Libraries

A good application needs to integrate a lot of scenarios, such as payment, e-commerce, maps, etc. As a development team, there is no need to repeatedly build these functions , and it can directly call different SDKs to complete related operations. .NET MAUI solves the cross-platform application interface, and most of the page logic and business logic problems. But when we use a third-party SDK, you will find that there is a lack of native libraries related to .NET MAUI, only libraries such as Obj-C/Swfit that support iOS/macOS, and jar/aar libraries developed by Android. How to adapt these libraries to .NET MAUI applications?

iOS / macOS Native Library Bindings

1. Know dynamic library and static library

The easiest way to understand is that in iOS, the static library ends with the .a suffix, and the dynamic library ends with the .dylib suffix. Both static and dynamic libraries can be packaged into Framework.

The difference between static library and dynamic library

The characteristic of the static library is that the library file will be copied directly to the target application during compilation, and this copy resides in the target application, so after the compilation is completed, the file of the static library is useless. But there is a disadvantage that, because of the need to copy, the capacity of the generated application will be larger

The dynamic library and the static library are just the opposite. They will not be copied into the target application when compiling, so the size of the generated application is small, and a dynamic library can be shared by multiple applications. However, the generated application depends on the dynamic library, which often leads to the situation that the dynamic library cannot be found.

2. Generate C# call interface through Sharpie tool

Shapie is a very useful conversion tool that supports conversion of Objective-C libraries under macOS. Through Sharpie, the header file given by the library file can be converted to complete the binding of C#. The Shapie tool, the predecessor of MAUI, already existed, and I often use this tool for conversion.

It should be noted here that you need to install Xcode, it is recommended to install to the latest, and correspond to the latest iOS SDK, of course, you can also bind different versions of iOS SDK according to your needs, you can use this command to view the environment. If you have not installed Sharpie, please refer to the development environment configuration

sharpie xcode -sdks

3. Create

Create iOS native library bindings via the command line

dotnet new iosbinding

Android Native Library aar / jar Binding

The Android library is packaged in .jar, and you can bind multiple .jars through tools, and then call the native Java library through C#. Compared with iOS, Android's library binding is much simpler.

From the image above you can see that Xamarin.Android / .NET for Android implements the binding by using a Managed Callable Wrapper (MCW). MCW is a JNI bridge that is used when managed code needs to call Java code. Managed callable wrappers also support subclassing Java types and overriding virtual methods of Java types. Likewise, whenever Android Runtime (ART) code needs to call managed code, it does so through another JNI bridge called the Android Callable Wrapper (ACW).

Create Android Native Library bindings via the command line

dotnet new android-bindinglib

Example: Enable DJI Ryze Tello to have real-time image transmission by binding libVLC

  1. In Terminal, create a folder TelloApp.Bindings

  2. Enter the TelloApp folder and create projects based on iOS and Android bindings through the folders

dotnet new iosbinding -o VLCSharp.iOS

dotnet new android-bindinglib -o VLCSharp.Droid

  1. Download the libVLC native library for iOS and the libVLC native library for Android from Baidu Cloud Disk

    Link: https://pan.baidu.com/s/1kaFZOhU4DxveLHGJ0YMv2A?pwd=qnhh Extraction code: qnhh

  2. Convert the libVLC native library of iOS through the command line Sharpie command in the terminal

sharpie bind -framework ./MobileVLCKit.framework --namespace MobileVLCKit -sdk iphoneos16.1

Note: The iOS library depends on the compiled SDK, this version is based on iphoneOS 16.1, namely Xcode 14.1

And add StructsAndEnums.cs and ApiDefinitions.cs and MobileVLCKit.framework to the iOS binding project VLCSharp.iOS

Modify .csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0-ios</TargetFramework>
    <RootNamespace>MobileVLCKit</RootNamespace>
    <Nullable>enable</Nullable>
    <ImplicitUsings>true</ImplicitUsings>
    <IsBindingProject>true</IsBindingProject>
	<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
	<NoBindingEmbedding>false</NoBindingEmbedding>
  </PropertyGroup>

  <ItemGroup>
    <ObjcBindingApiDefinition Include="ApiDefinition.cs" />
    <ObjcBindingCoreSource Include="StructsAndEnums.cs" />
  </ItemGroup>

  <ItemGroup>
    <NativeReference Include="MobileVLCKit.framework">
      <Kind>Framework</Kind>
      <IsCxx>True</IsCxx>
      <ForceLoad>True</ForceLoad>
      <SmartLink>True</SmartLink>
      <Frameworks>MediaPlayer Accelerate AssetsLibrary AVFoundation CoreMedia AudioToolbox CoreData CoreMedia CoreSpotlight MobileCoreServices CoreAudio OpenGLES CFNetwork CoreText QuartzCore CoreGraphics UIKit Security StoreKit SystemConfiguration VideoToolbox</Frameworks>
      <LinkerFlags>-lbz2 -liconv -lstdc++</LinkerFlags>
    </NativeReference>
  </ItemGroup>
</Project>

Compile, and get errors , please download from GitHub https://github.com/kinfey/dotNETMauiHOL/tree/main/code/apps/02.Binding/TelloApp.Bindings/VLCSharp.iOS and replace StructsAndEnums.cs and ApiDefinitions. cs

After the replacement, the compilation is successful

If you want to learn more about iOS Binding knowledge and related skills, you can learn about it through this link https://github.com/kinfey/AMapMAUIControls/blob/main/tutorial/cn/01.iOSBinding.md

  1. Open the newly created Android binding project with Visual Studio Code, create a Jars folder, and add the Android libVLC library org.videolan.libvlc.aar to the binding project Jars folder

  2. Modify the .csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net7.0-android</TargetFramework>
    <SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <LibraryProjectZip Include="Jars\org.videolan.libvlc.aar" />
  </ItemGroup>

  <ItemGroup>
    <TransformFile Include="Transforms\Metadata.xml" />
    <TransformFile Include="Transforms\EnumFields.xml" />
    <TransformFile Include="Transforms\EnumMethods.xml" />
  </ItemGroup>
</Project>

Compile, the compilation error is the same as iOS, please download and replace EnumMethods.xml from GitHub https://github.com/kinfey/dotNETMauiHOL/tree/main/code/apps/02.Binding/TelloApp.Bindings/VLCSharp.Droid

After the replacement, the compilation was successful

If you want to learn more about Android Binding knowledge and related skills, you can learn about it through this link https://github.com/kinfey/AMapMAUIControls/blob/main/tutorial/cn/02.DroidBinding.md

Congratulations, you have successfully bound the libVLC library. Next, we will enter the next chapter and integrate all the previous exercises into an example to complete a complete DJI Ryze Tello application with .NET MAUI.

Resources

  1. Learn Android native library binding, please visit https://docs.microsoft.com/en-us/xamarin/android/platform/binding-java-library/

  2. Learn iOS native library binding, please visit https://learn.microsoft.com/en-us/xamarin/cross-platform/macios/binding/?context=xamarin%2Fios