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

I have a folder with a lot of .so files which I'm trying to use while my code is running. 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

Best Answer

  • Accepted Answer

    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 have have to be stored 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 called 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 or not. Also, the libraries of course has to be build as AndroidNativeLibrary.

Answers

  • Tobi-Wan-KenobiTobi-Wan-Kenobi DKMember
    Accepted Answer

    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 have have to be stored 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 called 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 or not. Also, the libraries of course has to be build as AndroidNativeLibrary.

Sign In or Register to comment.