Search code examples
androideclipseubuntuandroid-ndkjava-native-interface

UnsatisfiedLinkError: ... :findLibrary returned null


03-01 14:00:53.556: E/AndroidRuntime(27208): java.lang.UnsatisfiedLinkError: Couldn't load
example from loader dalvik.system.PathClassLoader[DexPathList[[zip file 
"/data/app/com.example.test-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.test-
2, /vendor/lib, /system/lib]]]: findLibrary returned null

I am trying to get a basic environment set up for Android NDK development on Ubuntu 12 and I cannot get this error to go away. My system is already set up for regular Android development with the SDK. I've installed the eclipse C/C++ development tools.

My .bashrc has these lines at the bottom:

NDK_HOME=~/android-ndk-r9c
export NDK_HOME
export PATH=/home/steve/android-ndk-r9c:${PATH}
export NDK_PATH=/home/steve/android-ndk-r9c

In my Eclipse properties, my NDK location in Android->NDK is set to /home/steve/android-ndk-r9c. My Android.mk looks as follows:

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := test
LOCAL_SRC_FILES := example.c

include $(BUILD_SHARED_LIBRARY)

I tried using build-ndk on the command line. I now have in my project directory a file obj/local/armeabi/libtest.so, but it's not doing me any good.

For what it's worth, no project works, not even the NDK sample projects (such as HelloJni). What can I do to compile a basic JNI application?

EDIT: This is on an actual device. The ndk-build output of hello-jni is:

[armeabi-v7a] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi-v7a/gdbserver
[armeabi-v7a] Gdbsetup       : libs/armeabi-v7a/gdb.setup
[armeabi] Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
[armeabi] Gdbsetup       : libs/armeabi/gdb.setup
[x86] Gdbserver      : [x86-4.6] libs/x86/gdbserver
[x86] Gdbsetup       : libs/x86/gdb.setup
[mips] Gdbserver      : [mipsel-linux-android-4.6] libs/mips/gdbserver
[mips] Gdbsetup       : libs/mips/gdb.setup
[armeabi-v7a] Compile thumb  : hello-jni <= hello-jni.c
[armeabi-v7a] SharedLibrary  : libhello-jni.so
[armeabi-v7a] Install        : libhello-jni.so => libs/armeabi-v7a/libhello-jni.so
[armeabi] Compile thumb  : hello-jni <= hello-jni.c
[armeabi] SharedLibrary  : libhello-jni.so
[armeabi] Install        : libhello-jni.so => libs/armeabi/libhello-jni.so
[x86] Compile        : hello-jni <= hello-jni.c
[x86] SharedLibrary  : libhello-jni.so
[x86] Install        : libhello-jni.so => libs/x86/libhello-jni.so
[mips] Compile        : hello-jni <= hello-jni.c
[mips] SharedLibrary  : libhello-jni.so
[mips] Install        : libhello-jni.so => libs/mips/libhello-jni.so

Solution

  • The right way of loading the shared library is

    static {
        System.loadLibrary("test");
    }
    

    The library name is taken from the LOCAL_MODULE definition of your Android.mk file (or from the Application.mk file, if you decide to use it). In your case, you are naming your module test. The ndk-build generates the shared library libtest.so.

    Pay attention that you do not need to include the lib- prefix in the System.loadLibrary() call.