How to integrate native runtime library with dlopen on NDK?

Started by
-1 comments, last by chris_01 11 years ago
For my native c++ project I've to set up a runtime loading of a precompiled shared library, which could be changed by client side on start up using configurations. What is the proper way of calling dlopen on android? No matter what I do, dlopen can never open any shared library, if I not define this library as precompiled library in my Android.mk file like this:

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 native activity main class I try 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 disadventage of this implementation is that is not really a runtime loading, because I've to load this library also on JNI startup using JAVA mechanisms.
If I remove the bar library precompile defintions from Android.mk, disable JNI loading on startup and add a copy of the precompiled library to the systems/lib folder where it should be (same place where it is stored using precompile definitions), the loading of the library fails. I've checked the apk package it contains my manually copied library at lib folder as expected.
Why this will not work? Is it possible to perform a strict native runtime library loading of an external precompiled library? And what is the best way to ensure thatmy library is added to the apk package?

This topic is closed to new replies.

Advertisement