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 ?
Here is a simple wrapper I wrote about:
To keep it readable, I drop most of error handling, unessential cleanup, and handling of special cases.
Android.mkfor this executable:Note that you must take care of deployment: packaging this
wrapperinto 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 usendk-buildto build them, it looks as follows:Note that you don’t need to
chmodfor 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.