Unity does not see .so library in Android build

Here is the issue: I have a .so library with c++ functions, mark it for Android and Editor, put it into Assets/Plugins/Android folder, make a build for Android.
Result: c++ functions work in Editor but do NOT work on Android build, actually it throws DllNotFound exception. I tried to change [DllImport to (“mylib”)], [DllImport to (“mylib.so”)], [DllImport to (“/mylib”)], [DllImport to (“/mylib.so”)]. But it does not work
Could anybody explain me how exactly it should work in this case?
The same result with .aar library. Build just does not see it. Why is this happening
Unity 2019.3
Thanks

After the apk is produced, unzip it and see if your library is present.

Hi
Yes, the library is present there

In both ARMv7 and ARM64 folders? assuming you’re targeting both. Can you attach android logcat just in case ?

Hi, Tomas
Only ARMv7 folder.
Here is logical I have from Android developer:

And how can I target both CPU architectures? I can choose only one in Import Settings.

Native library is compiled either for ARMv7 or ARM64. In other words you need to have two native libraries - one for ARMv7, one for ARM64. And then set the CPU settings appropriately in plugin inspector.

So I have to make two copies of library and set appropriate CPU settings… Ok. I tried to build for ARM64 and this does not work either. Do you have some steps or tutorial for my case? I did not find clear explanation how to use c++ code in Android build.

https://docs.unity3d.com/Manual/AndroidNativePlugins.html this might help

No!))
This did not help! That is the case! So you have nothing than that link? That link has example project which could not import into Unity as package! And that link does not describe in detail how to work with c++ code… Uh…

@Tomas1856 @dkrepkikh_1
Same issue faced by me. Any solution to this ?

I’m interested too, I have the same problem when compiling in ARM64 Android
Yes the Library is in the apk, but the DllImport does not find the library.
It works in ARMv7.
The library has been set for ARM64 of course.

I made an example which compiles shared libraries and the uses them in the android application. GitHub - todi1856/AndroidExamples

Hopefully this helps

I am sorry, this does not work.
I have this output :

StdOut:
StdErr:
'C:\Program' n'est pas reconnu en tant que commande interne
ou externe, un programme ex�cutable ou un fichier de commandes.

Translation :
C:\Program is not recognized as an internal or external command, an executable program or a command file.

It seems Google tools don’t like the fact NDK is placed in the path with whitespaces. I tried to workaround this, but it seems it’s really not that easy.

You have two options:

  • Install Unity (together with NDK, SDK) into path with no whitespaces

or

  • Copy paste <unity_install_path>\Editor\Data\PlaybackEngines\AndroidPlayer\NDK to some path with no whitespaces, then in Unity->Preferences->External Tools override the NDK directory.

I’ll try to check if it’s possible to use gradle for compiling native files, maybe it doesn’t have issue with whitespaces

1 Like

Ok, after installing Unity to a custom folder with no spaces, your example works.

What does that mean for my library ? This is a “.so” library but I don’t have the native code.
This library’s role is to give me an Android Native Surface to display video streaming.

Do I need the native code to test compilation with your example ?
How a “DllNotFound” could happen whereas the library is in the apk ?

Thank you

It’s possible that .so you provided is compiled/linked for incorrect architecture, thus when trying to load it, the application fails.

The example I provided, proves that if native library is compiled/linked for correct architecture, DllImport works correctly with it. Thus there’s no issue in Unity in this case.

Where did you get that shared library?

1 Like

This library has been developed by my client, it’s a VR app, and this library is for the Android platform only.

But, I have a question :

  • In the library configuration, I have the option “ARM64”, does that mean that the library is compiled for the ARM64 too ? or this option is always available, no matter the compilation of the library ?

Thank you

Are you referring to the plugin inspector, could you show a screenshot? If CPU value set to ARM64 in Plugin Inspector, means you’re telling Unity that this library is compatible with ARM64 platform, Unity Editor itself doesn’t check if shared library was compiled for ARM64. Thus if you provide library with incorrect architecture, the runtime will refuse to load such library.

1 Like

Yes, I am referring to the plugin inspector.
See the capture joined.
Thanks for the infos, I’ll see if the client can send me the native code to check that.

7664782--956845--Capture.JPG

1 Like