# 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 <filename unknown>:0

• I'm pretty sure you will have to write a simple Android plugin in Java, and have that Java code import the library. I'm not familiar with this particular problem though. Apr 28 '15 at 18:49
• According to these docs, you can use native plugins (provided they extern c their declarations) without a java interface: docs.unity3d.com/Manual/NativePlugins.html
– Almo
Apr 28 '15 at 18:51
• whatever my point wasn't about the language, so much as: I don't think Unity can simply access the library directly; you need to write a plugin that includes the library, and then use that plugin in Unity. Apr 28 '15 at 18:54
• My point is that the docs say that you can access the library directly, and that is made easier by using C-style bindings. C++ and Obj-C mangle the names. Elsewhere in the docs, they talk about how to access java stuff, and that is something I have done in another test project.
– Almo
Apr 28 '15 at 18:56
• That's not what it says. It says "The native plugin should provide a simple C interface which the C# script then exposes to other user scripts." In other words, if the necessary C interface was implemented by this other developer (you didn't specify) then you could use this library as a Unity plugin, but otherwise you need to implement a Unity plugin. Apr 28 '15 at 20:15

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 :)

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.

• Do you have a documentation reference for that naming convention? I quickly poured over the pages around docs.unity3d.com/Manual/NativePlugins.html, but I saw nothing regarding a "lib" prefix. Apr 29 '15 at 12:50
• I have seen what he says in other blog posts, and I believe he's a Unity Tech employee. I have just tried this, and I still get the DLLNotFoundException. I've checked in the .apk, and I see the .so file in lib/armeabi-v7/. I'm using a Galaxy Nexus One, so I think it's an ARM machine.
– Almo
Apr 29 '15 at 14:49
• I don't know why this answer was downvoted as it looks correct to me. Sep 28 '15 at 7:53