Jump to content
  • Advertisement
Sign in to follow this  

How to integrate native runtime library with dlopen on NDK?

This topic is 2031 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)


include $(CLEAR_VARS)
LOCAL_SRC_FILES := bar/bar.cpp

include $(CLEAR_VARS)
LOCAL_MODULE := native_activity
LOCAL_SRC_FILES := main.cpp

LOCAL_LDLIBS := -llog 
LOCAL_LDLIBS += -landroid 

LOCAL_STATIC_LIBRARIES += android_native_app_glue

$(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",
        LOGI("Library successfully loaded!");
        if (dlsym(lib, "bar") == NULL) {
            fprintf(stderr, "Symbol 'bar' is missing from shared library!!\n");        
            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? 
Edited by chris_01

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!