Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
680 views
in Technique[技术] by (71.8m points)

build - Xamarin: use NDK-built .so

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?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...