Tobiuo
Tobiuo

Reputation: 323

How to access .dll or .so files in an android project Xamarin

I have a folder with a lot of .so files which I'm trying to use while my code is running. enter image description here

I place them in a folder called "External" and then I try to access them like this:

[DllImport("External\\libvocon_ext_heap.so", EntryPoint = "ph_CreateWin32PrivateHeap", CallingConvention = CallingConvention.Cdecl)]
public static extern PH_ERROR ph_CreateWin32PrivateHeap(ref LH_HEAP_INTERFACE pHeapInterface, out IntPtr ppHeapInst);

I've also tried to place the files in the Assets folder, but i still get the same System.DllNotFoundException

Upvotes: 2

Views: 2461

Answers (2)

Tobiuo
Tobiuo

Reputation: 323

This is a late answer but I did find the solution to my problem. The main issue was how I stored the library files in the project. In order to locate the library files they have to be stored under specific directories. Instead of storing them in the "External" directory as I did, they must be stored in a directory with the name "libs" and a specific subdirectory like the following:

  • libs>armeabi>libfoo.so
  • libs>armeabi-v7a>libfoo.so
  • libs>x86>libfoo.so

Because Android CPUs can be based on 3 different ARM architecture, there has to be a directory for each of those architectures (armeabi, armeabi-v7a, x86). I imported the library files like this:

[DllImport("libvocon_ext_heap.so", EntryPoint = "ph_CreateWin32PrivateHeap", CallingConvention = CallingConvention.Cdecl)]

You only need to write the name of the library file since it will locate the right directory path by itself. You can also write the name like "vocon_ext_heap" since it can automatically add the "lib" and ".so" to the file name if it is missing.

I also re-installed the Xamarin SDK manager because I found a possible solution asking to re-install it, but I don't know if it fixed anything related to this problem. Also, the libraries of course has to be build as AndroidNativeLibrary as Softlion mentioned.

Upvotes: 4

Softlion
Softlion

Reputation: 12615

Have you set the build action of your so file to AndroidNativeLibrary ?

See https://developer.xamarin.com/guides/android/advanced_topics/using_native_libraries/

Upvotes: 1

Related Questions