SDL: Support Google Play 16 KB Page Size Requirement (#13470)

From dc2c83c3834f4eaa64698cc19cc80a3f21105e57 Mon Sep 17 00:00:00 2001
From: Matthew Zavislak <[EMAIL REDACTED]>
Date: Thu, 31 Jul 2025 16:12:54 -0700
Subject: [PATCH] Support Google Play 16 KB Page Size Requirement (#13470)

- See: https://developer.android.com/guide/practices/page-sizes#update-packaging
- Also, make min API uniform at 21 (from 16 and 23 in a few places)
---
 .github/workflows/release.yml              | 2 +-
 Android.mk                                 | 4 ++++
 CMakeLists.txt                             | 5 +++++
 android-project/app/build.gradle           | 2 +-
 android-project/app/jni/Application.mk     | 5 ++++-
 android-project/app/jni/src/CMakeLists.txt | 5 +++++
 build-scripts/androidbuildlibs.sh          | 7 +++++++
 src/camera/android/SDL_camera_android.c    | 2 +-
 8 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index d507b69d9fa64..6493b72b2a217 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -562,7 +562,7 @@ jobs:
         run: |
           python build-scripts/build-release.py \
             --actions android \
-            --android-api 23 \
+            --android-api 21 \
             --android-ndk-home "${{ steps.setup-ndk.outputs.ndk-path }}" \
             --commit ${{ inputs.commit }} \
             --root "${{ steps.tar.outputs.path }}" \
diff --git a/Android.mk b/Android.mk
index 3ecc631d100d7..0f3138e5f497e 100644
--- a/Android.mk
+++ b/Android.mk
@@ -108,6 +108,10 @@ LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -lOpenSLES -llog -landroid
 
 LOCAL_LDFLAGS := -Wl,--no-undefined -Wl,--no-undefined-version -Wl,--version-script=$(LOCAL_PATH)/src/dynapi/SDL_dynapi.sym
 
+# https://developer.android.com/guide/practices/page-sizes
+LOCAL_LDFLAGS += "-Wl,-z,max-page-size=16384"
+LOCAL_LDFLAGS += "-Wl,-z,common-page-size=16384"
+
 ifeq ($(NDK_DEBUG),1)
     cmd-strip :=
 endif
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b891873c655aa..0de10a1d90c9d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -1509,6 +1509,11 @@ if(ANDROID)
     endif()
   endif()
 
+if(TARGET SDL3-shared)
+  target_link_options(SDL3-shared PRIVATE "-Wl,-z,max-page-size=16384")
+  target_link_options(SDL3-shared PRIVATE "-Wl,-z,common-page-size=16384")
+endif()
+
 elseif(EMSCRIPTEN)
   # Hide noisy warnings that intend to aid mostly during initial stages of porting a new
   # project. Uncomment at will for verbose cross-compiling -I/../ path info.
diff --git a/android-project/app/build.gradle b/android-project/app/build.gradle
index 989ef34c712a8..f44cf2673293e 100644
--- a/android-project/app/build.gradle
+++ b/android-project/app/build.gradle
@@ -19,7 +19,7 @@ android {
                 abiFilters 'arm64-v8a'
             }
             cmake {
-                arguments "-DANDROID_PLATFORM=android-21", "-DANDROID_STL=c++_static"
+                arguments "-DANDROID_PLATFORM=android-21", "-DANDROID_STL=c++_static", "-DAPP_SUPPORT_FLEXIBLE_PAGE_SIZES=true"
                 // abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
                 abiFilters 'arm64-v8a'
             }
diff --git a/android-project/app/jni/Application.mk b/android-project/app/jni/Application.mk
index 023bc20dfcaa1..80b73fd6bc463 100644
--- a/android-project/app/jni/Application.mk
+++ b/android-project/app/jni/Application.mk
@@ -7,4 +7,7 @@
 APP_ABI := armeabi-v7a arm64-v8a x86 x86_64
 
 # Min runtime API level
-APP_PLATFORM=android-16
+APP_PLATFORM=android-21
+
+# https://developer.android.com/guide/practices/page-sizes#update-packaging
+APP_SUPPORT_FLEXIBLE_PAGE_SIZES := true
\ No newline at end of file
diff --git a/android-project/app/jni/src/CMakeLists.txt b/android-project/app/jni/src/CMakeLists.txt
index 41a82f2af0660..df0a4d0f34c19 100644
--- a/android-project/app/jni/src/CMakeLists.txt
+++ b/android-project/app/jni/src/CMakeLists.txt
@@ -26,4 +26,9 @@ endif()
 add_library(main SHARED
     YourSourceHere.c
 )
+
+#https://developer.android.com/guide/practices/page-sizes#update-packaging
+target_link_options(main PRIVATE "-Wl,-z,max-page-size=16384")
+target_link_options(main PRIVATE "-Wl,-z,common-page-size=16384")
+
 target_link_libraries(main PRIVATE SDL3::SDL3)
diff --git a/build-scripts/androidbuildlibs.sh b/build-scripts/androidbuildlibs.sh
index a903f36eeac89..1004a98703ba2 100755
--- a/build-scripts/androidbuildlibs.sh
+++ b/build-scripts/androidbuildlibs.sh
@@ -30,6 +30,7 @@ abi="arm64-v8a" # "armeabi-v7a arm64-v8a x86 x86_64"
 obj=
 lib=
 ndk_args=
+flexpage=true
 
 # Allow an external caller to specify locations and platform.
 while [ $# -gt 0 ]; do
@@ -42,6 +43,8 @@ while [ $# -gt 0 ]; do
         platform=${arg#APP_PLATFORM=}
     elif [ "${arg:0:8}" == "APP_ABI=" ]; then
         abi=${arg#APP_ABI=}
+    elif [ "${arg:0:32}" == "APP_SUPPORT_FLEXIBLE_PAGE_SIZES=" ]; then
+        flexpage=${arg#APP_SUPPORT_FLEXIBLE_PAGE_SIZES=}
     else
         ndk_args="$ndk_args $arg"
     fi
@@ -67,6 +70,9 @@ done
 # APP_* variables set in the environment here will not be seen by the
 # ndk-build makefile segments that use them, e.g., default-application.mk.
 # For consistency, pass all values on the command line.
+#
+# Add support for Google Play 16 KB Page size requirement:
+# https://developer.android.com/guide/practices/page-sizes#ndk-build
 ndk-build \
     NDK_PROJECT_PATH=null \
     NDK_OUT=$obj \
@@ -75,4 +81,5 @@ ndk-build \
     APP_ABI="$abi" \
     APP_PLATFORM="$platform" \
     APP_MODULES="SDL3" \
+    APP_SUPPORT_FLEXIBLE_PAGE_SIZES="$flexpage" \
     $ndk_args
diff --git a/src/camera/android/SDL_camera_android.c b/src/camera/android/SDL_camera_android.c
index ad39e63a38542..90eb0007c5421 100644
--- a/src/camera/android/SDL_camera_android.c
+++ b/src/camera/android/SDL_camera_android.c
@@ -814,7 +814,7 @@ static void ANDROIDCAMERA_Deinitialize(void)
 static bool ANDROIDCAMERA_Init(SDL_CameraDriverImpl *impl)
 {
     // !!! FIXME: slide this off into a subroutine
-    // system libraries are in android-24 and later; we currently target android-16 and later, so check if they exist at runtime.
+    // system libraries are in android-24 and later; we currently target android-21 and later, so check if they exist at runtime.
     void *libcamera2 = dlopen("libcamera2ndk.so", RTLD_NOW | RTLD_LOCAL);
     if (!libcamera2) {
         SDL_Log("CAMERA: libcamera2ndk.so can't be loaded: %s", dlerror());