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:
2.Make sure that your .so
files are set the Build Action
to Android Native Library
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:
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.