mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-11-30 16:48:41 +01:00
Android: add SDL3 static build to Android.mk (#16306)
This commit is contained in:
+13
@@ -119,6 +119,19 @@ endif
|
||||
include $(BUILD_SHARED_LIBRARY)
|
||||
|
||||
|
||||
###########################
|
||||
#
|
||||
# SDL static library
|
||||
#
|
||||
###########################
|
||||
|
||||
LOCAL_MODULE := SDL3_static
|
||||
|
||||
LOCAL_MODULE_FILENAME := libSDL3
|
||||
|
||||
include $(BUILD_STATIC_LIBRARY)
|
||||
|
||||
|
||||
###########################
|
||||
#
|
||||
# SDL_test static library
|
||||
|
||||
@@ -168,6 +168,34 @@ The `build-scripts/create-android-project.py` script can create a project using
|
||||
build-scripts/create-android-project.py --variant aar com.yourcompany.yourapp < sources.list
|
||||
```
|
||||
|
||||
Building SDL3 as a static library
|
||||
================================================================================
|
||||
|
||||
The static library can be built with the Android.mk compilation file. It builds the SDL3 library with a weak reference
|
||||
to main(), so it requires your application (built as a shared object) to provide the main() function.
|
||||
|
||||
Create a version script (e.g., custom_version_script.vs) to control symbol visibility and expose JNI_OnLoad:
|
||||
```
|
||||
{
|
||||
global:
|
||||
JNI_OnLoad;
|
||||
local:
|
||||
*;
|
||||
};
|
||||
```
|
||||
|
||||
Add the following compiler and linker flags to your Android.mk:
|
||||
```
|
||||
LOCAL_CFLAGS += -ffunction-sections -fdata-sections
|
||||
LOCAL_LDFLAGS += -Wl,--gc-sections
|
||||
LOCAL_LDFLAGS += -Wl,-version-script -Wl,custom_version_script.vs
|
||||
```
|
||||
|
||||
Advantage: significantly reduces the total binary size by enabling garbage collection of unused sections (--gc-sections).
|
||||
|
||||
Limitation: your application's primary library cannot implement its own JNI_OnLoad, as SDL3 already defines it. If additional custom JNI initialization is required, split it into separate shared libraries, each can define its own JNI_OnLoad.
|
||||
|
||||
|
||||
Customizing your application name
|
||||
================================================================================
|
||||
|
||||
|
||||
@@ -984,6 +984,11 @@ JNIEXPORT int JNICALL SDL_JAVA_INTERFACE(nativeRunMain)(JNIEnv *env, jclass cls,
|
||||
SDL_main_func sdlmain = NULL;
|
||||
void *library_handle = NULL;
|
||||
|
||||
extern int main(int argc, char **argv) __attribute__((weak));
|
||||
|
||||
// try to get sdlmain from static linking
|
||||
sdlmain = (SDL_main_func) main;
|
||||
|
||||
// load the main function from user shared library
|
||||
if (!sdlmain) {
|
||||
const char *library_file = (*env)->GetStringUTFChars(env, library, NULL);
|
||||
|
||||
+6
-1
@@ -175,6 +175,11 @@ function(add_sdl_test_executable TARGET)
|
||||
else()
|
||||
add_executable(${TARGET} ${AST_SOURCES} ${EXTRA_SOURCES})
|
||||
endif()
|
||||
if(ANDROID)
|
||||
if(NOT SDL_TESTS_LINK_SHARED)
|
||||
target_link_options(${TARGET} PRIVATE "LINKER:SHELL:--version-script \"${CMAKE_CURRENT_SOURCE_DIR}/android/SDL_static_versyms.sym")
|
||||
endif()
|
||||
endif()
|
||||
target_compile_definitions(${TARGET} PRIVATE HAVE_BUILD_CONFIG)
|
||||
SDL_AddCommonCompilerFlags(${TARGET})
|
||||
target_include_directories(${TARGET} PRIVATE "${SDL3_SOURCE_DIR}/src/video/khronos")
|
||||
@@ -865,7 +870,7 @@ if(ANDROID AND TARGET SDL3::Jar)
|
||||
-cp "$<TARGET_PROPERTY:SDL3::Jar,JAR_FILE>:${SDL_ANDROID_PLATFORM_ANDROID_JAR}"
|
||||
-d "${classes_path}"
|
||||
COMMAND ${Java_JAR_EXECUTABLE} cf "${OUT_JAR}" -C "${classes_path}" .
|
||||
DEPENDS $<TARGET_PROPERTY:${TEST}-apk-linked,OUTPUTS> "$<TARGET_PROPERTY:SDL3::Jar,JAR_FILE>" "${JAVA_PACKAGE_DIR}/SDLTestActivity.java" "${JAVA_PACKAGE_DIR}/SDLEntryTestActivity.java"
|
||||
DEPENDS SDL3-jar $<TARGET_PROPERTY:${TEST}-apk-linked,OUTPUTS> "$<TARGET_PROPERTY:SDL3::Jar,JAR_FILE>" "${JAVA_PACKAGE_DIR}/SDLTestActivity.java" "${JAVA_PACKAGE_DIR}/SDLEntryTestActivity.java"
|
||||
)
|
||||
add_custom_target(${TEST}-jar DEPENDS "${OUT_JAR}")
|
||||
set_property(TARGET ${TEST}-jar PROPERTY OUTPUT "${OUT_JAR}")
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
# When linking to a static SDL3 library, JNI_OnLoad must have public visibility.
|
||||
global:
|
||||
JNI_OnLoad;
|
||||
local:
|
||||
*;
|
||||
};
|
||||
Reference in New Issue
Block a user