Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,26 @@ above:
5. Android Studio can warn you and auto-fix common mistakes in class names and
function signatures.

### Version scripts

All of the app libraries shown here are built using a version script. This is a
file that explicitly lists which symbols should be exported from the library,
and hides all the others. Version scripts function similarly to
`-fvisibility=hidden`, but can go a step further and are capable of hiding
symbols in static libraries that are used by your app. Hiding as many symbols as
possible results in smaller binaries that load faster, as there are fewer
relocations required and LTO can do a better job. They also run faster as
same-library function calls do not need to be made through the PLT. There are no
good reasons to not use a version script for your NDK code. See the NDK
documentation on [controlling symbol visibility] for more information.

You can find these in each sample as the `lib<name>.map.txt` file (where
`<name>` is the name of the library passed to `add_app_library()` in the
`CMakeLists.txt` file). The build plumbing that uses the version scripts is in
the definition of `add_app_library()` in `cmake/AppLibrary.cmake`.

[controlling symbol visibility]: https://developer.android.com/ndk/guides/symbol-visibility

## Additional documentation

- [Add Native Code to Your Project](https://developer.android.com/studio/projects/add-native-code.html)
Expand Down
6 changes: 6 additions & 0 deletions audio-echo/app/src/main/cpp/libecho.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBECHO {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions bitmap-plasma/app/src/main/cpp/libplasma.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBPLASMA {
global:
JNI_OnLoad;
local:
*;
};
7 changes: 7 additions & 0 deletions camera/basic/src/main/cpp/libndk_camera.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
JNI_OnLoad;
local:
*;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBCAMERA_TEXTUREVIEW {
global:
JNI_OnLoad;
local:
*;
};
15 changes: 14 additions & 1 deletion cmake/AppLibrary.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ include_guard()
# Wrapper for add_library which enables common options we want for all
# libraries.
function(add_app_library name)
cmake_parse_arguments(PARSE_ARGV 1 arg "" "" "")
cmake_parse_arguments(PARSE_ARGV 1 arg "NO_VERSION_SCRIPT" "" "")
add_library(${name} ${arg_UNPARSED_ARGUMENTS})

target_compile_options(${name}
Expand All @@ -12,4 +12,17 @@ function(add_app_library name)
-Wextra
-Werror
)

if(NOT arg_NO_VERSION_SCRIPT)
target_link_options(${name}
PRIVATE
-Wl,--no-undefined-version
-Wl,--version-script,${CMAKE_SOURCE_DIR}/lib${name}.map.txt
)

set_target_properties(${name}
PROPERTIES
LINK_DEPENDS ${CMAKE_SOURCE_DIR}/lib${name}.map.txt
)
endif()
endfunction()
6 changes: 6 additions & 0 deletions endless-tunnel/app/src/main/cpp/libgame.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBGAME {
global:
ANativeActivity_onCreate;
local:
*;
};
6 changes: 6 additions & 0 deletions exceptions/app/src/main/cpp/libexceptions.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBEXCEPTIONS {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions gles3jni/app/src/main/cpp/libgles3jni.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBGLES3JNI {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions hello-gl2/app/src/main/cpp/libgl2jni.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBGL2JNI {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions hello-jni/app/src/main/cpp/libhello-jni.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBHELLOJNI {
global:
JNI_OnLoad;
local:
*;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBJNICALLBACK {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions hello-oboe/app/src/main/cpp/libhello-oboe.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBHELLOOBOE {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions hello-vulkan/app/src/main/cpp/libhellovkjni.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBHELLOVKJNI {
global:
Java_com_google_androidgamesdk_GameActivity_initializeNativeCode;
local:
*;
};
6 changes: 6 additions & 0 deletions native-activity/app/src/main/cpp/libnative-activity.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBNATIVEACTIVITY {
global:
ANativeActivity_onCreate;
local:
*;
};
6 changes: 6 additions & 0 deletions native-audio/app/src/main/cpp/libnative-audio-jni.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBNATIVEAUDIOJNI {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions native-codec/app/src/main/cpp/libnative-codec-jni.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBNATIVECODECJNI {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions native-midi/app/src/main/cpp/libnative_midi.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBNATIVE_MIDI {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions orderfile/app/src/main/cpp/liborderfiledemo.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBORDERFILEDEMO {
global:
JNI_OnLoad;
local:
*;
};
6 changes: 6 additions & 0 deletions sanitizers/app/src/main/cpp/libsanitizers.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBSANITIZERS {
global:
JNI_OnLoad;
local:
*;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBACCELEROMETERGRAPH {
global:
JNI_OnLoad;
local:
*;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
LIBCHOREOGRAPHERNATIVEACTIVITY {
global:
ANativeActivity_onCreate;
JNI_OnLoad;
local:
*;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
local:
*;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
local:
*;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
local:
*;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBNDK_CAMERA {
global:
ANativeActivity_onCreate;
local:
*;
};
2 changes: 1 addition & 1 deletion unit-test/app/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ target_link_libraries( # Specifies the target library.
log
)

add_app_library(app_tests SHARED adder_test.cpp)
add_app_library(app_tests NO_VERSION_SCRIPT SHARED adder_test.cpp)
target_link_libraries(app_tests
PRIVATE
$<TARGET_OBJECTS:adder>
Expand Down
6 changes: 6 additions & 0 deletions unit-test/app/src/main/cpp/libunittest.map.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBUNITTEST {
global:
JNI_OnLoad;
local:
*;
};
11 changes: 0 additions & 11 deletions vectorization/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,3 @@ target_link_libraries(app
base::base
log
)

target_link_options(app
PRIVATE
-flto
-Wl,--version-script,${CMAKE_SOURCE_DIR}/libapp.map.txt
)

set_target_properties(app
PROPERTIES
LINK_DEPENDS ${CMAKE_SOURCE_DIR}/libapp.map.txt
)