Let’s assume we have a shared library called MyTest.so and we want to use it in the Xamarin.Android project. The MyTest.so consists of a function
int MyTest_GetValue();
Now, we need to use this function on Xamarin.Android project. Here are the steps so as to succeed:
Step 1: Create a new folder inside the Xamarin.Android project called lib and sub-folder armeabi. Copied my .so library to be used inside the armeabi folder as stated here
Step 2: Set the properties of the library.so (imported library) Build action to "AndroidNativeLibrary" and Copy to output to "Always Copy".
Step 3: (Working in Xamarin.Android Class eg: MainActivity.cs)
Include the namespace InteropServices by “using System.Runtime.InteropServices;”
Use the standard DllImport in the project to import the native library as below:
[DllImport("MyTest.so")]
public extern static int MyTest_GetValue();// with exact Functtion Name, Type & Params in the .so Lib.
Step 4: Consume the function above (MyTest_GetValue()
) in the application.
For Example:
int value= MyTest_GetValue();
Console.Writeline(value.ToString());
Mission Accomplished! :D
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…