Issue
What i want is to build a native binary with NDK. There are couples shared lib also been build. The APK structure look like this:
/data/data/mypackage/files/my_binary
/data/data/mypackage/lib/liba.so
/data/data/mypackage/lib/libb.so
When the binary is been executed,error like CANNOT LINK EXECUTABLE: could not load library "liba.so" needed by "./mybinay"; caused by library liba.so not found
been throw out. It's working fine after setting LD_LIBRARY_PATH
to /data/data/mypackage/lib
.
My question is how to make it work without setting LD_LIBRARY_PATH
in CMAKE file or Gradel?
Cmake file.
add_library(a, src/a.cc)
add_library(b, src/b.cc)
add_executable(mybinary src/mybinary.cc)
target_link_libraries(a log)
target_link_libraries(mybinary a android log b)
Gradle file.
externalNativeBuild {
cmake {
cppFlags "-std=c++14 -frtti -fexceptions"
targets 'mybinary','a','b'
}
}
Solution
The runtime scenario is not related to CMake or gradle. You don't need LD_LIBRARY_PATH to build the executable.
I prefer to pack the executable into libs/
of the APK, next to liba.so and libb.so. The trick is to rename my_binary to something Ike libmy_binary.so (see https://stackoverflow.com/a/15099666/192373).
So, this would mean that now CMakeLists.txt can look as follows:
add_library(a, src/a.cc)
add_library(b, src/b.cc)
add_executable(mybinary src/mybinary.cc)
target_link_libraries(a log)
target_link_libraries(lib.mybinary.so a android log b)
And setting LD_LIBRARY_PATH is trivial:
try {
Runtime.getRuntime().exec(
getApplicationInfo().nativeLibraryDir + "/lib.mybinary.so",
new String[]{"LD_LIBRARY_PATH="+ getApplicationInfo().nativeLibraryDir});
} catch (IOException e) {
e.printStackTrace();
}
Answered By - Alex Cohn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.