How do I refer to a native library with Unity 5 on Android?

How do I refer to a native library with Unity 5 on Android?

I have a library called mylibandroid.a. This is given to me by another developer. I also have the iOS version of this lib, and have been able to access it from Unity.
It is supposedly built for android. I have included it in my Plugins folder in my Unity 5 project, but I renamed it mylibandroid.so as droid libs use this naming convention. I can see the library included in the .apk file, so Unity seems to know it should go in there.
My code looks like this:
public class AlmoTest : MonoBehaviour
{
#if UNITY_ANDROID
    [DllImport("mylibandroid.so")]
    private static extern void FreeGlobals();

    public void TestFreeGlobalsButtonPress()
    {
        Debug.Log("Pressed");
        FreeGlobals();
    }
#endif
}

When my function is called, I get this error:
E/Unity   ( 4188): Unable to find mylibandroid.so
I/Unity   ( 4188): Pressed
I/Unity   ( 4188):
I/Unity   ( 4188): (Filename: ./artifacts/generated/common/runtime/UnityEngineDebug.gen.cpp Line: 56)
I/Unity   ( 4188):
I/Unity   ( 4188): DllNotFoundException: mylibandroid.so
I/Unity   ( 4188):   at (wrapper managed-to-native) AlmoTest:TestFreeGlobalsButtonPress ()
I/Unity   ( 4188):   at AlmoTest.TestFreeGlobalsButtonPress () [0x00000] in :0

Solutions/Answers:

Answer 1:

As bitter said,

Your file should be named “libmylibandroid.so”

Drag and drop this file into your Unity Project

Then, when you call the lib, just write :

[DllImport("mylibandroid")]
private static extern void FreeGlobals();

and not :

[DllImport("mylibandroid.so")]
private static extern void FreeGlobals();

Hope that will be useful for someone 🙂

Answer 2:

I think the reason you are having problems is because your library has a weird name. Normally libraries are called lib{name}.so and you import them by just specifying {name}. So, assuming you want to call your library foobar you name the file libfoobar.so and you import it using [DllImport(“foobar”)].

If that doesn’t work try to unpack your Apk and make sure the libraries didn’t get stripped for some reason.

References