问题描述
在我做C.基于Eclipse(Android项目)两个简单的功能(设置和返回一个int),我用的产生的.so。我如何可以使用Xamarin本的.so和消费这两个功能在我Xamarin.Android项目?
I made two simple functions (set and return an int) on eclipse (Android project) in C. I used ndk-build to produce a .so. How can i use this .so on Xamarin and consume those two functions on my Xamarin.Android project?
谢谢!
推荐答案
让我们假设有一个叫MyTest.so一个共享库,我们希望在Xamarin.Android项目中使用它。该MyTest.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();
int MyTest_GetValue();
现在,我们需要使用的Xamarin.Android项目这个功能。下面是步骤,以取得成功:
Now, we need to use this function on Xamarin.Android project. Here are the steps so as to succeed:
第1步:创建一个名为lib和子文件夹armeabi的Xamarin.Android项目中一个新的文件夹。复制的文件夹armeabi内部使用我的.so库作为陈述
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
第二步:设置library.so(进口库)的性质构建行动AndroidNativeLibrary,并复制到输出始终复制
Step 2: Set the properties of the library.so (imported library) Build action to "AndroidNativeLibrary" and Copy to output to "Always Copy".
第三步:(在Xamarin.Android类如工作:MainActivity.cs)
Step 3: (Working in Xamarin.Android Class eg: MainActivity.cs)
-
由包括命名空间InteropServices使用System.Runtime.InteropServices;
Include the namespace InteropServices by "using System.Runtime.InteropServices;"
使用标准的DllImport项目导入下面的机库:
函数[DllImport(MyTest.so)]
公众的extern静态INT MyTest_GetValue(); //用精确Functtion名称,类型和放大器; PARAMS中的.so库。
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.
步骤4:消耗上述功能(MyTest_GetValue())在应用程序
Step 4: Consume the function above (MyTest_GetValue()) in the application.
例如:
int值= MyTest_GetValue();
int value= MyTest_GetValue();
Console.Writeline(value.ToString());
Console.Writeline(value.ToString());
任务完成:D
这篇关于Xamarin:使用NDK内置的.so的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!