Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to remove symbols from a shared library built with Android NDK?

I'm building a shared library for use in a Java app using the Android NDK. Using readelf to inspect the lib/armeabi-v7a/libXXXlib.so file generated by a release build, it appears to contain all the symbols (function, variable names) of my native C/C++ code.

Indeed, the shared object file appears to be identical for the debug and release builds. (The only difference in the output in the libs folder being whether or not the gsb.setup and gdbserver files are created.) I'm overriding the optimisation set by the NDK with an APP_CFLAGS += -O3 in my Application.mk, but I wouldn't expect the release build to generate debug symbols by including the -g flag, which it does.

I found a comment on SO stating 'In both cases, debug and release, it leaves the -g flag in because, to quote the comment, "we generate symbol versions of the binaries that are later stripped when they are copied to the final project's libs/ directory"', and another stating: 'JNI cannot find the functions for java if they don't have names.'

ndk-build is certainly stripping something from the obj/armeabi-v7a/libXXXlib.so file, as that is much larger than the one eventually created in libs, but it still seems to be leaving all my functions' and variables' names intact in the file.

Are all my functions' names required by JNI (rather than just the entry-points into the native code)?

If not, how can I remove the symbols which are not required?

like image 351
Rich Avatar asked Jun 10 '13 11:06

Rich


People also ask

What can I do with Android NDK?

The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.

What is LIBC in Android?

libc++ LLVM's libc++ is the C++ standard library that has been used by the Android OS since Lollipop, and as of NDK r18 is the only STL available in the NDK. Note: For full details of the expected level of C++ library support for any given version, see the C++14 Status, C++17 Status, and C++20 Status pages.

What is native library in Android?

On Android, it is possible to use the Mobile SDK native API from an NDK library instead of the Java API. To use the native API, you must call TasInitialize with two extra parameters: the JNI environment and the application context. Both are passed as parameters to each JNI native method.


1 Answers

Feel free to set default visibility=hidden, and set attribute visibility=default for the few functions you need to export (similar to DLLEXPORT) in Windows

like image 162
Alex Cohn Avatar answered Oct 15 '22 19:10

Alex Cohn