mirror of
				https://github.com/android/ndk-samples
				synced 2025-11-04 06:15:39 +08:00 
			
		
		
		
	Add (and require) version scripts for everything.
This commit is contained in:
		
							
								
								
									
										20
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								README.md
									
									
									
									
									
								
							@@ -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)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								audio-echo/app/src/main/cpp/libecho.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								audio-echo/app/src/main/cpp/libecho.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBECHO {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								bitmap-plasma/app/src/main/cpp/libplasma.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								bitmap-plasma/app/src/main/cpp/libplasma.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBPLASMA {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										7
									
								
								camera/basic/src/main/cpp/libndk_camera.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								camera/basic/src/main/cpp/libndk_camera.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
LIBNDK_CAMERA {
 | 
			
		||||
  global:
 | 
			
		||||
    ANativeActivity_onCreate;
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBCAMERA_TEXTUREVIEW {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -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}
 | 
			
		||||
@@ -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
									
								
								endless-tunnel/app/src/main/cpp/libgame.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								endless-tunnel/app/src/main/cpp/libgame.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBGAME {
 | 
			
		||||
  global:
 | 
			
		||||
    ANativeActivity_onCreate;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								exceptions/app/src/main/cpp/libexceptions.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								exceptions/app/src/main/cpp/libexceptions.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBEXCEPTIONS {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								gles3jni/app/src/main/cpp/libgles3jni.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								gles3jni/app/src/main/cpp/libgles3jni.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBGLES3JNI {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								hello-gl2/app/src/main/cpp/libgl2jni.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								hello-gl2/app/src/main/cpp/libgl2jni.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBGL2JNI {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								hello-jni/app/src/main/cpp/libhello-jni.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								hello-jni/app/src/main/cpp/libhello-jni.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBHELLOJNI {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBJNICALLBACK {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								hello-oboe/app/src/main/cpp/libhello-oboe.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								hello-oboe/app/src/main/cpp/libhello-oboe.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBHELLOOBOE {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								hello-vulkan/app/src/main/cpp/libhellovkjni.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								hello-vulkan/app/src/main/cpp/libhellovkjni.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBHELLOVKJNI {
 | 
			
		||||
  global:
 | 
			
		||||
    Java_com_google_androidgamesdk_GameActivity_initializeNativeCode;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBNATIVEACTIVITY {
 | 
			
		||||
  global:
 | 
			
		||||
    ANativeActivity_onCreate;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBNATIVEAUDIOJNI {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBNATIVECODECJNI {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								native-midi/app/src/main/cpp/libnative_midi.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								native-midi/app/src/main/cpp/libnative_midi.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBNATIVE_MIDI {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								orderfile/app/src/main/cpp/liborderfiledemo.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								orderfile/app/src/main/cpp/liborderfiledemo.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBORDERFILEDEMO {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
							
								
								
									
										6
									
								
								sanitizers/app/src/main/cpp/libsanitizers.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								sanitizers/app/src/main/cpp/libsanitizers.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBSANITIZERS {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBACCELEROMETERGRAPH {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,7 @@
 | 
			
		||||
LIBCHOREOGRAPHERNATIVEACTIVITY {
 | 
			
		||||
  global:
 | 
			
		||||
    ANativeActivity_onCreate;
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBNDK_CAMERA {
 | 
			
		||||
  global:
 | 
			
		||||
    ANativeActivity_onCreate;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBNDK_CAMERA {
 | 
			
		||||
  global:
 | 
			
		||||
    ANativeActivity_onCreate;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBNDK_CAMERA {
 | 
			
		||||
  global:
 | 
			
		||||
    ANativeActivity_onCreate;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -0,0 +1,6 @@
 | 
			
		||||
LIBNDK_CAMERA {
 | 
			
		||||
  global:
 | 
			
		||||
    ANativeActivity_onCreate;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -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>
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								unit-test/app/src/main/cpp/libunittest.map.txt
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								unit-test/app/src/main/cpp/libunittest.map.txt
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
			
		||||
LIBUNITTEST {
 | 
			
		||||
  global:
 | 
			
		||||
    JNI_OnLoad;
 | 
			
		||||
  local:
 | 
			
		||||
    *;
 | 
			
		||||
};
 | 
			
		||||
@@ -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
 | 
			
		||||
)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user