For my native C ++ project, I have to configure the loading of a pre-compiled shared library that can be changed on the client side at startup using configurations. What is the correct way to call dlopen on Android? No matter what I do, dlopen will never be able to open any shared library unless I define this library as a precompiled library in my Android.mk file as follows:
LOCAL_PATH := $(call my-dir) LOCAL_CFLAGS += -DDEBUG LOCAL_CFLAGS += -DANDROID include $(CLEAR_VARS) LOCAL_MODULE := bar LOCAL_SRC_FILES := bar/bar.cpp LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar include $(BUILD_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := native_activity LOCAL_SRC_FILES := main.cpp LOCAL_LDLIBS := -llog LOCAL_LDLIBS += -landroid LOCAL_SHARED_LIBRARIES := bar LOCAL_STATIC_LIBRARIES += android_native_app_glue include $(BUILD_SHARED_LIBRARY) $(call import-module, android/native_app_glue)
In my main activity, the main class I am trying to load the library with:
void* lib = dlopen("/system/lib/armeabi-v7a/libbar.so", RTLD_NOW); if (lib == NULL) { fprintf(stderr, "Could not dlopen(\"libbar.so\"): %s\n", dlerror()); exit(1); }else{ LOGI("Library successfully loaded!"); if (dlsym(lib, "bar") == NULL) { fprintf(stderr, "Symbol 'bar' is missing from shared library!!\n"); }else{ LOGI("Library symbol found!"); } int x = bar(25); LOGI("Bar return value: %i", x); }
The rejection of this implementation is that the download is not actually done, because I have to load this library also when starting JNI using JAVA mechanisms.
If I delete the prefix table library from Android.mk, disable JNI loading at startup and add a copy of the precompiled library to the system / lib folder where it should be (in the same place where it is stored using precompilation definitions), the library does not load . I checked the apk package, it contains my manually copied library in the lib folder, as expected.
Why doesn't it work? Is it possible to do strict native library loading while an external precompiled library is running? And what is the best way to ensure that my library is added to the apk package?
source share