Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate native runtime library with dlopen on NDK?

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 that my library is added to the apk package?

like image 370
Hellhound Avatar asked Mar 30 '13 13:03

Hellhound


1 Answers

The best strategy on Android is to load all native libraries from Java, even if you choose which libraries to load, by some runtime rules. You can use dlsym(0, ...) then to access exported functions there.

APK builder will pick up all .so files it finds in libs/armeabi-v7a and the installer will unpack them into /data/data/<your.package>/lib directory.

like image 85
Alex Cohn Avatar answered Nov 14 '22 02:11

Alex Cohn