androidc++maui.net-maui

How to include existing C++ libraries per platform in MAUI project?


We have a C++ library that is built per platform i.e. .dll for Windows, .so for Android & .a for iOS. Tried the following to include the .so file in MAUI app for Android. (Other platforms are pending) -

However, the code fails with

UnsatisfiedLinkError exception : dlopen failed: library "lib name.so" not found at Java.Interop.JniEnvironment.StaticMethods.CallStaticVoidMethod(JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) in at Java.Interop.JniEnvironment.StaticMethods.CallStaticVoidMethod(JniObjectReference type, JniMethodInfo method, JniArgumentValue* args) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniEnvironment.g.cs:line 13250 at Java.Interop.JniPeerMembers.JniStaticMethods.InvokeVoidMethod(String encodedMember, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniStaticMethods.cs:line 41 at Java.Lang.JavaSystem.LoadLibrary(String libname) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net6.0/android-31/mcw/Java.Lang.JavaSystem.cs:line 339

In VS Output window, the following message is seen -

"Shared library 'lib name' not loaded, p/invoke 'method name' may fail"

Tried moving the lib directory around but no luck.

What are we missing here?

Also are there any MAUI samples for using existing C++ libraries? Is the support really good enough for production use?

--- UPDATE --- So the correct location to put the .so files for Android is apparently <project root>\Resources\lib\arm64-v8a\ Also, anything copied into <project root>\Resources\Raw is directly copied into Android asset directory.

Now, the problem is the 32-bit windows dll are not get picked as Maui Windows app is 64-bit by default and I am not able to convert it. Will post another question and link to it.


Solution

  • So the correct location to put the .so files for Android is apparently

    <project root>\Resources\lib\<platform>\

    So for arm64-v8, it's <project root>\Resources\lib\arm64-v8a\

    (Also, anything copied into <project root>\Resources\Raw is directly copied into Android asset directory.)