Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8688481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:22:38+00:00 2026-06-12T23:22:38+00:00

i have an android app which spawns many native executables dinamically linked with libraries

  • 0

i have an android app which spawns many native executables dinamically linked with libraries i distribute with the package.
To launch those binaries, i use the LD_LIBRARY_PATH environment variable to make them aware of the place to load the libraries from, but on some devices this doesn’t work at all, the LD_LIBRARY_PATH is correctly updated but the binary fails to find the library anyway.
This is not something i can reproduce because on my two devices ( Galaxy Nexus & Nexus 7 with stock roms ) it just works fine.

I tried many ways, for instance i spawn:

LD_LIBRARY_PATH=/my/package/custom/libs:$LD_LIBRARY_PATH && cd /binary/directory && ./binary

And :

    String[] envp = { "LD_LIBRARY_PATH=" + libPath + ":$LD_LIBRARY_PATH" };

    Process process = Runtime.getRuntime().exec( "su", envp );

    writer = new DataOutputStream( process.getOutputStream() );
    reader = new BufferedReader( new InputStreamReader( process.getInputStream() ) );

    writer.writeBytes( "export LD_LIBRARY_PATH=" + libPath + ":$LD_LIBRARY_PATH\n" );
    writer.flush();

But on those devices nothing seemed to work … so i’m starting to think that this is a kernel related issue, some kernels ( like mine ) use the LD_LIBRARY_PATH, other kernels don’t ( simply ignore it, or they’re using just the LD_LIBRARY_PATH that was set on application startup, therefore there’s no way to change it at runtime ).

I also tried to use System.load but it didn’t work, probably because those libs are not JNI … is there something i could try before starting to think about using statically linked binaries ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:22:40+00:00Added an answer on June 12, 2026 at 11:22 pm

    Here is a simple wrapper I wrote about:

    #include <android/log.h>
    #include <dlfcn.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef int (*main_t)(int argc, char** argv);
    
    static int help(const char* argv0)
    {
        printf("%s: simple wrapper to work around LD_LIBRARY_PATH\n\n", argv0);
        printf("Args: executable, list all the libraries you need to load in dependency order, executable again, optional parameters\n");
        printf("example: %s /data/local/ttte /data/data/app/com.testwrapper/lib/ttt.so /data/local/ttte 12345\n", argv0);
        printf("Note: the executable should be built with CFLAGS=\"-fPIC -pie\", LDFLAGS=\"-rdynamic\"\n");
    
        return -1;
    }
    
    int main(int argc, char** argv)
    {
        int rc, nlibs;
        void *dl_handle;
    
        if (argc < 2)
        {
            return help(argv[0]);
        }
    
        __android_log_print(ANDROID_LOG_DEBUG, "wrapper", "running '%s'", argv[1]);
    
        for (nlibs = 2; ; nlibs++)
        {
            if (nlibs >= argc)
            {
                return help(argv[0]);
            }
    
            __android_log_print(ANDROID_LOG_DEBUG, "wrapper", "loading '%s'", argv[nlibs]);
            dl_handle = dlopen(argv[nlibs], 0); // do not keep the handle, except for the last
            __android_log_print(ANDROID_LOG_DEBUG, "wrapper", "loaded '%s' -> %p", argv[nlibs], dl_handle);
            if (strcmp(argv[1], argv[nlibs]) == 0)
            {
                break;
            }
        }
    
        main_t pmain = (main_t)dlsym(dl_handle, "main");
        __android_log_print(ANDROID_LOG_DEBUG, "wrapper", "found '%s' -> %p", "main", pmain);
        rc = pmain(argc - nlibs, argv + nlibs);
    
    //   we are exiting the process anyway, don't need to clean the handles actually
    
    //   __android_log_print(3, "wrapper", "closing '%s'", argv[1]);
    //   dlclose(dl_handle);
    
        return 0;
    }
    

    To keep it readable, I drop most of error handling, unessential cleanup, and handling of special cases.

    Android.mk for this executable:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_MODULE    := wrapper
    LOCAL_SRC_FILES := wrapper/main.c
    LOCAL_LDLIBS    := -llog
    
    include $(BUILD_EXECUTABLE)
    

    Note that you must take care of deployment: packaging this wrapper into the APK, extraction to some local path (never to USB storage or to /sdcard!), marking it as executable (chmod 777).

    These are the additional parameters you must supply when you build the executables you run through the wrapper. If you use ndk-build to build them, it looks as follows:

    LOCAL_C_FLAGS   += -fPIC -pie
    LOCAL_LDFLAGS   += -rdynamic 
    

    Note that you don’t need to chmod for these executables anymore. Another trick: you can build the secondary executables into shared libraries, and the same wrapper will continue to work! This saves the trouble of deployment of these binaries. NDK and Android build will deliver them safely through libs/armeabi of the APK to your app’s lib directory automagically.

    Update

    There seems to be a much easier solution, using the ProcessBuilder with modified environment: https://stackoverflow.com/a/8962189/192373.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an android app which has native code. The native code needs to
I have an Android app which uses a lot of activities. All those activities
I have an android app which is native, I'm displaying a web page using
I have an android app in which there are many scenarios where request and
I have an android app which let the user to submit a form ,
I have an Android app which is crashing on some phones, and not on
I have an Android app which calculates poker odds ( Shufflebot if you're interested).
I have an android app which gives the user the option of loading a
I have an Android app which includes an activity called Help This uses a
I have made an android app which working fine. I implemented the Login functionality

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.