Commit e977225937645764cfa5f26f4bab53af446d1cab

Sam Lantinga 2017-08-13T17:59:59

Fixed bug 2839 - No way to create pre-built libraries for Android Mark Callow README-android says to copy or link the SDL source tree to the jni folder in your Android project. It is not desirable to have to compile SDL with every application; furthermore the Android NDK has support for prebuilt libraries. Attached is script (to be put in build-scripts) that builds the Android version of the libraries. The script builds both the existing SDL2 module and a new SDL2_main module. This is a static library containing the code from src/main/android/SDL_android_main.c. Also attached is a patch for Android.mk adding this module. Note that when building an application's native .so using this prebuilt libSDL2main, you must use a link option, such as --whole-archive, that forces inclusion of the code in the .so because the functions in SDL_android_main are called only from Java.

diff --git a/Android.mk b/Android.mk
index 0c203c5..c5af199 100755
--- a/Android.mk
+++ b/Android.mk
@@ -67,3 +67,23 @@ LOCAL_LDLIBS :=
 LOCAL_EXPORT_LDLIBS := -Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
 
 include $(BUILD_STATIC_LIBRARY)
+
+###########################
+#
+# SDL main static library
+#
+###########################
+
+include $(CLEAR_VARS)
+
+LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
+
+LOCAL_MODULE := SDL2_main
+
+LOCAL_MODULE_FILENAME := libSDL2main
+
+LOCAL_SRC_FILES := $(LOCAL_PATH)/src/main/android/SDL_android_main.c
+
+include $(BUILD_STATIC_LIBRARY)
+
+
diff --git a/build-scripts/androidbuildlibs.sh b/build-scripts/androidbuildlibs.sh
new file mode 100644
index 0000000..4a0bb2f
--- /dev/null
+++ b/build-scripts/androidbuildlibs.sh
@@ -0,0 +1,74 @@
+#!/bin/sh
+#
+# Build the Android libraries without needing a project
+# (AndroidManifest.xml, jni/{Application,Android}.mk, etc.)
+#
+# Usage: androidbuildlibs.sh [arg for ndk-build ...]"
+#
+# Useful NDK arguments:
+#
+#  NDK_DEBUG=1          - build debug version
+#  NDK_LIBS_OUT=<dest>  - specify alternate destination for installable
+#                         modules.
+#
+# Note that SDLmain is not an installable module (.so) so libSDLmain.a
+# can be found in $obj/local/<abi> along with the unstripped libSDL.so.
+#
+
+
+# Android.mk is in srcdir
+srcdir=`dirname $0`/..
+srcdir=`cd $srcdir && pwd`
+cd $srcdir
+
+
+#
+# Create the build directories
+#
+
+build=build
+buildandroid=$build/android
+obj=
+lib=
+ndk_args=
+
+# Allow an external caller to specify locations.
+for arg in $*
+do
+  if [ "${arg:0:8}" == "NDK_OUT=" ]; then
+	obj=${arg#NDK_OUT=}
+  elif [ "${arg:0:13}" == "NDK_LIBS_OUT=" ]; then
+	lib=${arg#NDK_LIBS_OUT=}
+  else
+    ndk_args="$ndk_args $arg"
+  fi
+done
+
+if [ -z $obj ]; then
+  obj=$buildandroid/obj
+fi
+if [ -z $lib ]; then
+  lib=$buildandroid/lib
+fi
+
+for dir in $build $buildandroid $obj $lib; do
+    if test -d $dir; then
+        :
+    else
+        mkdir $dir || exit 1
+    fi
+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.
+ndk-build \
+  NDK_PROJECT_PATH=null \
+  NDK_OUT=$obj \
+  NDK_LIBS_OUT=$lib \
+  APP_BUILD_SCRIPT=Android.mk \
+  APP_ABI="all" \
+  APP_PLATFORM=android-12 \
+  APP_MODULES="SDL2 SDL2_main" \
+  $ndk_args