AndroidRuntime Caused by: java.lang.unsatisfiedLinkError: Couldn't load tfp_jni: findLibrary returned null

12,944

A number of things must occur for the linkage to work

  1. The native library must be compiled to an .so file for the appropriate ABI(s) - this is typically accomplished via the ndk-build script/batch file, though it can also be done using a generated stand alone toolchain. IDE projects may want to configure running this as a custom build step.

  2. The native library must get packaged in the application .apk. If it was built from a jni/ folder under an application project directory, then ndk-build probably copied it into an ABI-appropriate subdirectory of the libs/ folder of the project. However, if the native library belongs to a distinct Android library, extra steps may be required. In particular, a .so cannot be obtained by the build system from a library .jar and so one associated with library code must either by explicitly copied under the libs/ folder of the client project, or else found by referencing a library project directory tree (not a lonely .jar) which includes it.

  3. The installer on the device must decide that one of the .so files contained in the .apk is appropriate for the device's ABI (architecture) and copy it out of the .apk into the install directory for use.

  4. The runtime linkage names of the jni functions (downstream of any compiler name-mangling) must match those which the VM is looking for. Typically, problems here come up from not correctly encoding the java fully qualified class name in the native function name. The javah tool is intended to help avoid such mistakes, though with care it can be done manually.

Each of these steps presents a potential breakdown, so debugging an unsatisfied link can be approached by trying to find the first stage at which the .so file goes missing.

Share:
12,944
Patricia
Author by

Patricia

News addict & avid reader. Software Engineer & Architect; strong emphasis in OO, ORM and database design using SysML, UML, and ER diagrams. I like knowledge and money, because on earth both are power. I like honesty and kindness, because in my heart both are sincere. I like humility and compassion, because in Heaven both are divine. May we all be filled with the love of God and be blessed by the existence we cannot see! Tenacious P, named by my coworkers, because I NEVER quit.

Updated on June 04, 2022

Comments

  • Patricia
    Patricia almost 2 years

    So, it looks like there are a lot of issues like mine out there but not sure any of them are related to my issue. OK. I have an Android project that uses an SDK as a referenced library. The SDK contains C++, so I am using the android-ndk-r9 library. The SDK that I reference in my Android project is a JNI library (Oooooo - scary stuff). Oh yeah, don't let me forget to mention armeabi-v7a (which appears to be another scary subject). My error occurs when this line is executed:

     System.loadLibrary("tfp_jni");
    

    tfp_jni is really a libtfp_jni.so file under the armeabi-v7a folder in libs folder of my SDK library project. That SDK library project contains an Android.mk file. I do not think the code is getting in there. But here is the contents of that .mk file:

    LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE := tfp-prebuilt
    LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libtfp_jni.so
    LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
    include $(PREBUILT_SHARED_LIBRARY)
    

    Then in my Android project, I have a jni folder containing an Android.mk and an Application.mk. Here are the contents:

    Android.mk

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    OPENCV_INSTALL_MODULES:=on
    OPENCV_INSTALL_CAMERA:=off
    
    include $(INNERID_ANDROID_ROOT)/Android.mk
    include $(OPENCV_ANDROID_ROOT)/sdk/native/jni/OpenCV.mk
    

    Application.mk

    APP_STL := gnustl_static
    APP_CPPFLAGS := -frtti -fexceptions
    APP_ABI := armeabi-v7a
    APP_PLATFORM := android-14
    

    Environment Variables

    Environment Variables

    Paths and Symbols - Includes

    Paths and Symbols - Includes

    All other tabs are empty except for the Source Location and Output Location, which displays the project folder.

    Android project & Preferences

    Android project & Preferences

    I have tried various other answers from the similar questions on Stackoverflow with no success. Please let me know if there is anything else you need to see and I'll provide the additional information as soon as possible.

    New Image

    Imgur

    Correction! The apk is in the verify-demo-nolic project /bin folder. The Referenced Library tfp_java.jar is the SDK library project. You can see from Finder that the .so file is in libs. Does this help?