Search Unity

Making C plugins work in Android. Just not happening.

Discussion in 'Android' started by 5c4r3cr0w, Sep 13, 2018.

  1. 5c4r3cr0w

    5c4r3cr0w

    Joined:
    Mar 8, 2016
    Posts:
    35
    Hello all,

    So I was checking out unity's native plugin documentations. I am trying to call a C function from c# code by my c plugin. I downloaded this example

    https://docs.unity3d.com/uploads/Examples/AndroidNativePlugin.zip

    This one works as expected when you build with IL2CPP scripting backend. Now I tried to add more function in Nativecode.c file. Now it looks somewhat like this:


    Code (CSharp):
    1. float add(float x, float y)
    2. {
    3.     return (x+y);
    4. }
    5.  
    6. float minus(float x, float y)
    7. {
    8.     return (x-y);
    9. }
    I complied a new libnative.so file replacing existing one. But I am getting below error:

    DllNotFoundException: Dll file "native" could not be loaded.

    What is it that I am missing here?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    Have you compiled the original native library from source code? It might be that your compilation settings are wrong and the resulting library could not be loaded.
    Can you show the full source code for C plugin?
     
  3. 5c4r3cr0w

    5c4r3cr0w

    Joined:
    Mar 8, 2016
    Posts:
    35
    Above is full source code of NativeCode.c file.

    There are other three files along with .c file and it's content.

    Android.mk:


    Code (CSharp):
    1. include $(CLEAR_VARS)
    2.  
    3. # override strip command to strip all symbols from output library; no need to ship with those..
    4. # cmd-strip = $(TOOLCHAIN_PREFIX)strip $1
    5.  
    6. LOCAL_ARM_MODE  := arm
    7. LOCAL_PATH      := $(NDK_PROJECT_PATH)
    8. LOCAL_MODULE    := libnative
    9. LOCAL_CFLAGS    := -Werror
    10. LOCAL_SRC_FILES := NativeCode.c
    11. LOCAL_LDLIBS    := -llog
    12.  
    13. include $(BUILD_SHARED_LIBRARY)
    Application.mk:

    Code (CSharp):
    1. APP_OPTIM        := release
    2. APP_ABI          := armeabi
    3. APP_PLATFORM     := android-8
    4. APP_BUILD_SCRIPT := Android.mk
    build_plugin.sh:

    Code (CSharp):
    1. #!/bin/sh
    2. echo ""
    3. echo "Compiling NativeCode.c..."
    4. $ANDROID_NDK_ROOT/ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk $*
    5. mv libs/armeabi/libnative.so ..
    6.  
    7. echo ""
    8. echo "Cleaning up / removing build folders..."  #optional..
    9. rm -rf libs
    10. rm -rf obj
    11.  
    12. echo ""
    13. echo "Done!"
    And the command I used to generate .so file:

    Code (CSharp):
    1. gcc *.c -O3 -fPIC -shared -o libnative.so
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    This command uses gcc from Android NDK, not just some gcc installed on your system, right?

    The build_plugin.sh is the file you should be using to build plugin.
     
  5. 5c4r3cr0w

    5c4r3cr0w

    Joined:
    Mar 8, 2016
    Posts:
    35
    No I used this command from my system gcc. How do I set up android NDKs gcc? I also tried executing sh file but it didn't created compiled libnative.so file.
     
  6. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,735
    You need to set ANDROID_NDK_ROOT environment variable to your NDK install directory and then the build_plugins.sh should work.

    System gcc does not suite, because it produces a library for your computer, not for phone.
     
    5c4r3cr0w likes this.
  7. 5c4r3cr0w

    5c4r3cr0w

    Joined:
    Mar 8, 2016
    Posts:
    35
    Thanks a lot. Now it's working as expected :)