Binding native Android (and iOS) libraries in MAUI

Viewed 43

We are working on an Android and iOS (but we want to just get Android working for now) application that needs to rely on some native drivers (.so, .a and some .ini files). The company providing those drivers also provides a Xamarin project to showcase how the drivers are used. They seem to be storing those driver and other files as assets (.ini files under Assets/Files and .so files under Assets/lib/arm64-v8a and Assets/lib/armeabi-v7a respectively) and extracting the former using the following code:

void ExtractAssets()
{
    var assets = ApplicationContext.Assets;
    var paths = assets.List("Files");

    foreach (var path in paths)
    {
        // Read the compressed file and extract
        string readPath = Path.Combine("Files", path);
        byte[] buffer = new byte[32000];
        int bytesRead = 0;
        using (var inStream = assets.Open(readPath))
        using (var outStream = ApplicationContext.OpenFileOutput(path, Android.Content.FileCreationMode.Private))
        {
            do
            {
                bytesRead = inStream.Read(buffer, 0, buffer.Length);
                if (bytesRead > 0)
                {
                    outStream.Write(buffer, 0, bytesRead);
                }
            }
            while (bytesRead > 0);
        }

        System.Diagnostics.Debug.WriteLine(string.Format("Extracted '{0}' to '{1}'", readPath, path));
    }
}

Since Xamarin is becoming obsolete and MAUI is the next big thing, we decided to build our project on that. My colleague and I have spent the entire afternoon on a sample project and we still do not manage to simply load a libhello-jni.so file (we use this one) at runtime to call a stringFromJNI(). We tried:

  • Using an Android Native Binding project — we never really got what that's about and how mappings are supposed to work
  • Trying to include the libhello-jni.so file as an asset and load it using JavaNative.LoadLibrary("hello-jni"); and this had various unsuccessful results, it either did not find the library at all or it was complaining about the lib being built for x86 time processor instead of x64.

Can anyone please help us with some sample code allowing us to just simply bind a native .so file in a MAUI project, or at least provide us with a good guide? Thank you and sorry for the long post.

0 Answers
Related