I need to deploy a native binary executable in an Android app. My approach is mostly modeled after this SO answer. The build process differs, but I deploy the native binaries as assets, which the app copies to its internal private storage as needed.
I have taken further inspiration from Termux and am building a mini directory structure in private shared storage. The executable resides in usr/bin
. However, it also depends on a library, which lives in usr/lib
.
I use the standard Java mechanism of ProcessBuilder
to start my binary. However, I figure I somehow need to tell Android where to look for the library required by the executable.
How can I tell Android where to look for libraries? Moreover, how can I ensure that my custom path takes precedence over system-wide library paths just in case there is a system-wide library which has the same name but is incompatible?
The library path is set through the LD_LIBRARY_PATH
environment variable. Sample code here (context
can be any context you can get hold of, such as an activity, service or the application context):
ProcessBuilder builder = new ProcessBuilder(context.getFilesDir()
+ "/usr/bin/mybinary --version");
builder.environment().put("LD_LIBRARY_PATH", context.getFilesDir()
+ "/usr/lib");
builder.environment().put("PATH", context.getFilesDir()
+ "/usr/bin:" + builder.environment().get(Const.ENV_PATH));
Process process = builder.start();