Fixed bug 2839 - No way to create pre-built libraries for Android
authorSam Lantinga <slouken@libsdl.org>
Sun, 13 Aug 2017 17:59:59 -0700
changeset 112628e5441ab6c63
parent 11261 2d458a198021
child 11263 f85618f8ba81
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.
Android.mk
build-scripts/androidbuildlibs.sh
     1.1 --- a/Android.mk	Sun Aug 13 14:15:52 2017 -0700
     1.2 +++ b/Android.mk	Sun Aug 13 17:59:59 2017 -0700
     1.3 @@ -67,3 +67,23 @@
     1.4  LOCAL_EXPORT_LDLIBS := -Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
     1.5  
     1.6  include $(BUILD_STATIC_LIBRARY)
     1.7 +
     1.8 +###########################
     1.9 +#
    1.10 +# SDL main static library
    1.11 +#
    1.12 +###########################
    1.13 +
    1.14 +include $(CLEAR_VARS)
    1.15 +
    1.16 +LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
    1.17 +
    1.18 +LOCAL_MODULE := SDL2_main
    1.19 +
    1.20 +LOCAL_MODULE_FILENAME := libSDL2main
    1.21 +
    1.22 +LOCAL_SRC_FILES := $(LOCAL_PATH)/src/main/android/SDL_android_main.c
    1.23 +
    1.24 +include $(BUILD_STATIC_LIBRARY)
    1.25 +
    1.26 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/build-scripts/androidbuildlibs.sh	Sun Aug 13 17:59:59 2017 -0700
     2.3 @@ -0,0 +1,74 @@
     2.4 +#!/bin/sh
     2.5 +#
     2.6 +# Build the Android libraries without needing a project
     2.7 +# (AndroidManifest.xml, jni/{Application,Android}.mk, etc.)
     2.8 +#
     2.9 +# Usage: androidbuildlibs.sh [arg for ndk-build ...]"
    2.10 +#
    2.11 +# Useful NDK arguments:
    2.12 +#
    2.13 +#  NDK_DEBUG=1          - build debug version
    2.14 +#  NDK_LIBS_OUT=<dest>  - specify alternate destination for installable
    2.15 +#                         modules.
    2.16 +#
    2.17 +# Note that SDLmain is not an installable module (.so) so libSDLmain.a
    2.18 +# can be found in $obj/local/<abi> along with the unstripped libSDL.so.
    2.19 +#
    2.20 +
    2.21 +
    2.22 +# Android.mk is in srcdir
    2.23 +srcdir=`dirname $0`/..
    2.24 +srcdir=`cd $srcdir && pwd`
    2.25 +cd $srcdir
    2.26 +
    2.27 +
    2.28 +#
    2.29 +# Create the build directories
    2.30 +#
    2.31 +
    2.32 +build=build
    2.33 +buildandroid=$build/android
    2.34 +obj=
    2.35 +lib=
    2.36 +ndk_args=
    2.37 +
    2.38 +# Allow an external caller to specify locations.
    2.39 +for arg in $*
    2.40 +do
    2.41 +  if [ "${arg:0:8}" == "NDK_OUT=" ]; then
    2.42 +	obj=${arg#NDK_OUT=}
    2.43 +  elif [ "${arg:0:13}" == "NDK_LIBS_OUT=" ]; then
    2.44 +	lib=${arg#NDK_LIBS_OUT=}
    2.45 +  else
    2.46 +    ndk_args="$ndk_args $arg"
    2.47 +  fi
    2.48 +done
    2.49 +
    2.50 +if [ -z $obj ]; then
    2.51 +  obj=$buildandroid/obj
    2.52 +fi
    2.53 +if [ -z $lib ]; then
    2.54 +  lib=$buildandroid/lib
    2.55 +fi
    2.56 +
    2.57 +for dir in $build $buildandroid $obj $lib; do
    2.58 +    if test -d $dir; then
    2.59 +        :
    2.60 +    else
    2.61 +        mkdir $dir || exit 1
    2.62 +    fi
    2.63 +done
    2.64 +
    2.65 +
    2.66 +# APP_* variables set in the environment here will not be seen by the
    2.67 +# ndk-build makefile segments that use them, e.g., default-application.mk.
    2.68 +# For consistency, pass all values on the command line.
    2.69 +ndk-build \
    2.70 +  NDK_PROJECT_PATH=null \
    2.71 +  NDK_OUT=$obj \
    2.72 +  NDK_LIBS_OUT=$lib \
    2.73 +  APP_BUILD_SCRIPT=Android.mk \
    2.74 +  APP_ABI="all" \
    2.75 +  APP_PLATFORM=android-12 \
    2.76 +  APP_MODULES="SDL2 SDL2_main" \
    2.77 +  $ndk_args