xamarin.android引用so文件

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());

猜你喜欢

转载自blog.csdn.net/zl325118/article/details/55048760