Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity does not see .so library in Android build

Discussion in 'Android' started by dkrepkikh, Jul 23, 2020.

  1. dkrepkikh

    dkrepkikh

    Joined:
    Dec 19, 2016
    Posts:
    11
    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 ("<full path>/mylib")], [DllImport to ("<fullpath>/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
     
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    After the apk is produced, unzip it and see if your library is present.
     
  3. dkrepkikh

    dkrepkikh

    Joined:
    Dec 19, 2016
    Posts:
    11
    Hi
    Yes, the library is present there
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    In both ARMv7 and ARM64 folders? assuming you're targeting both. Can you attach android logcat just in case ?
     
  5. dkrepkikh

    dkrepkikh

    Joined:
    Dec 19, 2016
    Posts:
    11
    Hi, Tomas
    Only ARMv7 folder.
    Here is logical I have from Android developer:
     

    Attached Files:

  6. dkrepkikh

    dkrepkikh

    Joined:
    Dec 19, 2016
    Posts:
    11
    And how can I target both CPU architectures? I can choose only one in Import Settings.
     
  7. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    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.
     
  8. dkrepkikh

    dkrepkikh

    Joined:
    Dec 19, 2016
    Posts:
    11
    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.
     
  9. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    https://docs.unity3d.com/Manual/AndroidNativePlugins.html this might help
     
  10. dkrepkikh

    dkrepkikh

    Joined:
    Dec 19, 2016
    Posts:
    11
    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...
     
    Last edited: Jul 24, 2020
  11. bhupiister

    bhupiister

    Joined:
    Dec 13, 2019
    Posts:
    42
  12. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    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.
     
  13. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
  14. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    I am sorry, this does not work.
    I have this output :

    Code (CSharp):
    1.  
    2. StdOut:
    3. StdErr:
    4. 'C:\Program' n'est pas reconnu en tant que commande interne
    5. ou externe, un programme ex�cutable ou un fichier de commandes.
    6.  
    Translation :
    C:\Program is not recognized as an internal or external command, an executable program or a command file.
     
  15. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    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
     
    GorasGames likes this.
  16. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    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
     
    Last edited: Nov 17, 2021
  17. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    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?
     
    GorasGames likes this.
  18. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    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
     
  19. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    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.
     
    GorasGames likes this.
  20. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    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.
     

    Attached Files:

    Tomas1856 likes this.
  21. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    I have the native code, it's in C++, seems generated with CMake.
    Can I take the native code and generate the .so file with your example project ?
    If yes, how ? Just place my c++ files in the "Source~" folder ?

    Thanks
     
  22. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    CMake is a build system, it doesn't generate C++ files, it compiles/links them. I don't have much experience with CMake though, can't you help that much with it.

    But if you're saying you have C++ files available, you can drop them in Unity project, and if you'll select il2cpp, they will be compiled/linked into libil2cpp.so. You can then use [DllImport ("__Internal")] to access the functions.
     
  23. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    Yes sorry, my bad, I know CMake does not generate C++. I have c++ files and CMake files in the native code.
    The native code is in an Android Studio project.

    I tried to drop them in Unity, but compilation failed
    This is my structure :
    in the folder : "Assets\Runtime\Plugins\Android\CineVRMediaSurface"
    I have these cpp and header files :
    Unity/IUnityGraphics.h
    Unity/IUnityInterface.h
    CMediaSurface (.h & .cpp)
    Common.h
    CSurfaceTexture (.h & .cpp)
    LogUtils.h
    PluginSurface.cpp


    I go this error when Building native binary with IL2CPP :
    Code (CSharp):
    1. stdout:
    2. Building libil2cpp.so with AndroidToolChain
    3.     Output directory: P:\Repaga Studio\Clients\ODN\CINEVR Multi\cinemur-vr-multi\Temp\StagingArea\assets\bin\Data\Native\arm64-v8a
    4.     Cache directory: P:\Repaga Studio\Clients\ODN\CINEVR Multi\cinemur-vr-multi\Library\il2cpp_android_arm64-v8a\il2cpp_cache
    5. il2cpp.exe didn't catch exception: Unity.IL2CPP.Building.BuilderFailedException: P:\Repaga Studio\Clients\ODN\CINEVR Multi\cinemur-vr-multi\Temp\StagingArea\Il2Cpp\il2cppOutput\PluginSurface.cpp:6:10: fatal error: 'Unity/IUnityGraphics.h' file not found
    6. #include "Unity/IUnityGraphics.h"
    Seems not to find the folder of the include, should I delete the "Unity" folder and just leave "IUnityGraphics.h" ?
     
    Last edited: Nov 17, 2021
  24. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,663
    Hmph, looks Unity is ignoring the subfolders of headers... that's unfortunate, if it helps, yes try to get rid of Unity folder.
     
  25. GorasGames

    GorasGames

    Joined:
    May 1, 2014
    Posts:
    18
    Hello,

    Removing the folder (only in #include instruction) worked but I have now some errors due to some opengl methods missing. I think this is caused by some libraries included by the CMake file (EGL & GLESv3) in the Android Studio original project.

    We'll see with the original developer of the plugin.

    Thanks for your help !
     
    Tomas1856 likes this.
  26. bhupiister

    bhupiister

    Joined:
    Dec 13, 2019
    Posts:
    42
    @Repaga-Dev I have successfully compiled a big library like opencascade for Android. I m not at desk right now, but I have made a setup in visual studio that creates library for Android. I'll post as soon as I can.
     
    GorasGames likes this.
  27. arnaldoGaroto

    arnaldoGaroto

    Joined:
    Feb 3, 2013
    Posts:
    22
    I'm facing a similar issue. I've been using com.google.signin:google-signin-support:1.0.4 for a while, it's been working fine for a year in builds made with unity2019 LTS. Now I'm trying to build another project with Unity2020.3.12f1 and google sign in works for armeabi-v7a but on arm64-v8a devices I get an error:
    Code (CSharp):
    1.  DllNotFoundException: Unable to load DLL 'native-googlesignin': The specified module could not be found.
    Unzipping the APK I can see that libnative-googlesignin.so is included in the lib/armeabi-v7a but is missing in the lib/arm64-v8a folder.

    Unzipping the library: google-signin-support-1.0.4.aar I can see that it contains an x86, armeabi-v7a and arm64-v8a folders (with a libnative-googlesignin.so version on each folder), so I don't understand why the libnative-googlesignin.so from the arm64-v8a fails to get linked/included when I build with Unity2020

    What can I try?
     
  28. JuliusM

    JuliusM

    Unity Technologies

    Joined:
    Apr 17, 2013
    Posts:
    824
    Check what abiFilters are specified in your gradle templates. I guess there will only be armeabi-v7a but you would also want to add arm64-v8a.
     
  29. arnaldoGaroto

    arnaldoGaroto

    Joined:
    Feb 3, 2013
    Posts:
    22
    to reply to my own issue, it looks like it was some sort of issue with the GoogleDependencyManager that somehow autoresolved to an .aar that didn't have the platform I needed. Forcing to resolve again might have fixed it (the .aar got automatically updated to a good one but I don't remember exactly the steps I followed)
     
  30. Finijumper

    Finijumper

    Joined:
    Jul 12, 2016
    Posts:
    75
    Hi, I'm facing the same issue, I built GameNetworkingSockets using vcpkg and I got these files.

    upload_2023-7-31_20-56-28.png



    But when I export to Android and I try to call a function like this:
    Code (CSharp):
    1. [DllImport("GameNetworkingSockets")]
    2.         internal static extern bool GameNetworkingSockets_Init(IntPtr identity, StringBuilder errorMessage);
    I get the following error:
    The library seems to work fine on Windows, but it's not recognised on Android for some reason.