(Xamarin) We want to make a .so and dylib framework

Tao, Scott-BT 1 Reputation point
2020-12-31T01:33:04.69+00:00

We have a .net project (Xamarin) and want to export to a library like .so and dylib, and use it on native iOS and Android application, how to make it?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,716 Reputation points Microsoft External Staff
    2020-12-31T08:54:52.52+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    If you are using Visual Studio, and you are porting to xamarin some JNI-Java code that was already working on Android, follow this Steps:

    1.Place your .so files under a lib folder on your Xamarin project, example:

    52420-image.png

    2.Make sure that your .so files are set the Build Action to Android Native Library
    52502-image.png

    3.In C#-Xamarin you can load your libraries in the following way

    try  
    {  
        JavaSystem.LoadLibrary("SDL2");  
        JavaSystem.LoadLibrary("glib-2.0");  
        JavaSystem.LoadLibrary("gthread-2.0");  
        JavaSystem.LoadLibrary("fluidsynth");  
        JavaSystem.LoadLibrary("sdl_mixer");  
        JavaSystem.LoadLibrary("initmixer");  
    }  
    catch (UnsatisfiedLinkError e)  
    {  
        return e.Message;  
    }  
    

    4.the native methods in C# must be declarated in the following way:

    [DllImport("initmixer", EntryPoint = "Java_sf2Tools_FluidsynthJNI_loadSong")]  
    public static extern int loadSong(IntPtr env, IntPtr thiz, IntPtr songPath, int miliseconds);  
    [DllImport("initmixer", EntryPoint = "Java_sf2Tools_FluidsynthJNI_isPlaying")]  
    public static extern int isPlaying();  
    [DllImport("initmixer", EntryPoint = "Java_sf2Tools_FluidsynthJNI_pauseAudio")]  
    public static extern void pauseAudio();  
    [DllImport("initmixer", EntryPoint = "Java_sf2Tools_FluidsynthJNI_resumeAudio")]  
    public static extern void resumeAudio();  
    [DllImport("initmixer", EntryPoint = "Java_sf2Tools_FluidsynthJNI_stopAudio")]  
    public static extern void stopAudio();  
    [DllImport("initmixer", EntryPoint = "Java_sf2Tools_FluidsynthJNI_setSoundfonts")]  
    public static extern void setSoundfonts(IntPtr env, IntPtr thiz, IntPtr js);  
    

    On the "EntryPoint" field you have to put the name of the function exactly as they was in your C/C++ code normally Java_your_package_name_YourClassName_YourMethodName.

    For example here is my original C++ code:

    void Java_sf2Tools_FluidsynthJNI_setSoundfonts(JNIEnv * env, jobject this, jstring js)  
    {  
        //Some code  
    }  
    void Java_sf2Tools_FluidsynthJNI_pauseAudio(JNIEnv * env, jobject this)  
    {  
        //Some code  
    }  
    void Java_sf2Tools_FluidsynthJNI_resumeAudio(JNIEnv * env, jobject this)  
    {  
        //Some code  
    }  
    int Java_sf2Tools_FluidsynthJNI_isPlaying(JNIEnv * env, jobject this)  
    {  
        //Some code  
    }  
    void Java_sf2Tools_FluidsynthJNI_stopAudio(JNIEnv * env, jobject this)  
    {  
        //Some code  
    }  
    
    int Java_sf2Tools_FluidsynthJNI_loadSong(JNIEnv * env, jobject this, jstring songPath)  
    {  
        //Some code  
    }  
    

    Or maybe check the warnings that usually gives Android Studio to know the full name of your native functions:

    52437-image.png

    C/C++

    void Java_sf2Tools_FluidsynthJNI_setSoundfonts(JNIEnv * env, jobject this, jstring js)  
    

    C# Xamarin

    fluidsynth.setSoundfonts(JNIEnv.Handle, System.IntPtr.Zero, new Java.Lang.String(getCFGPathFiltered()).Handle);  
    

    Refer : https://stackoverflow.com/questions/47433520/load-so-file-in-xamarin-android

    And there is similar thead about this, you can check here: https://stackoverflow.com/questions/15896650/xamarin-use-ndk-built-so/15902529#15902529

    For dylib , you can check document: https://learn.microsoft.com/en-us/xamarin/ios/platform/native-interop

    Best Regards,

    Jessie Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.