Create bindings to native libraries .net Maui
Asked Answered
C

3

16

Is there any way to bind native libraries like in Xamarin? I have tried to do some native implementations in the new .NET Maui and haven't had success on it. I would really appreciate if someone has some information about it.

Curriculum answered 7/4, 2022 at 18:57 Comment(1)
The dotnet team released a video on this a few days ago: youtube.com/watch?v=oibfI-ZsmzQPsychologize
V
5

Yes, native library bindings are done the same way as in Xamarin, there are two new project templates "Android Java Library Binding" and "iOS Binding Library" just add them and follow regular Xamarin documentation in order to configure it:

For android you can also check this git repo - https://github.com/Bohdandn/MAUI.WebRTC.Demo, unfortunately iOS doesn't compile there.

Vareck answered 5/7, 2022 at 21:43 Comment(2)
Thanks Bohdan, for this and your Github webrtc repo. I have a XF sln to upgrade. I'm hearing on the grapevine from senior iOS Maui devs, and online (e.g. here learn.microsoft.com/en-us/answers/questions/1061273/…) that binding libraries for iOS in particular do not work out of the box for Maui using these Xamarin methods. Can anyone point to Maui specific documentation for binding either iOS or Android? I found this video at least learn.microsoft.com/en-us/events/dotnetconf-focus-on-maui/…Raviv
It seems that they still haven't added MAUI specific docs for native bindings. Regard compilation issues - you can track this issue: github.com/xamarin/xamarin-macios/issues/16001, problem is in transferring libraries between windows and mac build host, directly on mac everything works.Vareck
L
1

For android jar files, I'm using .NET MAUI class library enter image description here

<ItemGroup Condition="$(TargetFramework.Contains('-android'))">
    <EmbeddedJar Include="classes.jar" />
</ItemGroup>

Add this "ItemGroup" to your project, if you can create a wrap class like

enter image description here

Legionnaire answered 29/11, 2023 at 6:9 Comment(0)
G
0

As Bohdan said, it is same as Xamarin.Forms. We need to create Multiple projects to manage this projects. The architecture, we follow is below.

  • .NET Maui Class Library
  • Maui Droid Projects (Holding Android Jar files)
  • Maui iOS Projects (Holding iOS Jar files)

Like below image and go through the below repo, this might help you.

https://github.com/CommunityToolkit/Maui.NativeLibraryInterop

Video Tutorial - https://learn.microsoft.com/en-us/shows/dotnetconf-focus-on-maui/binding-native-libraries-for-dotnet-maui

Guide for iOS - https://learn.microsoft.com/en-us/dotnet/maui/migration/ios-binding-projects?view=net-maui-8.0

Guide for Android - https://learn.microsoft.com/en-us/dotnet/maui/migration/android-binding-projects?view=net-maui-8.0

I wrote a blog for android binding few days ago, this might help you - https://xmonkeys360.com/2024/05/08/binding-android-kotlin-libraries-for-net-maui/

enter image description here

Garzon answered 19/7, 2024 at 6:58 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.