Search code examples
androidandroid-ndkjava-native-interface

cannot load library via jni


I have a Java project with jni and the native part works fine. I need to load a pre built (built using ndk) .so file from within my jni c file, using dlopen().

But, I get the following dlerror : Cannot load library: load_library[1093]: "system/lib/mytestlib.so" Library not found

JNI Code:

void* handle = dlopen("system/lib/mytestlib.so", RTLD_LAZY);
if(handle == NULL)
{
        char* er = dlerror();
        __android_log_print(ANDROID_LOG_DEBUG, TAG, "dlerror: %s", er);
}

The funny thing is, if I try to load any other prebuilt lib from the phone's /system/lib folder it loads it without error. Also I can even open my .so with fopen without issues, so the path is correct. The fact that its only happening to my lib is really bugging me. Thanks in advance for any help.


Solution

  • You miss / before system. You should provide an absolute path, you cannot rely on current directory being filesystem root. Luckily though , /system/lib is on LD_LIBRARY_PATH, therefore you can write

    void* handle = dlopen("mytestlib.so", RTLD_LAZY);