Fixed bug 2146 - Enable static linking of libSDL on Android
authorSam Lantinga <slouken@libsdl.org>
Thu, 10 Oct 2013 21:51:39 -0700
changeset 78041f85fb33694b
parent 7803 97a1127629fb
child 7805 4f0f7b64afa5
Fixed bug 2146 - Enable static linking of libSDL on Android

Denis Bernard

This patch to Android.mk adds support for static linking of libSDL for Android applications. A patched readme with static build instructions is also provided.

It does not break existing build environments setup according to the README-android.txt since the static library version will not be built in not required.

The static build uses the Android NDK module system (see docs/IMPORT-MODULE.html in the NDK folder and step 5 in the instructions below).

Instructions:
1. Copy the android-project directory wherever you want to keep your projects
and rename it to the name of your project.
2. Create a symlink to SDL/src/main/android/SDL_android_main.c as
<project>/jni/src/SDL_android_main.c
3. Rename <project>/jni/src/Android_static.mk to <project>/jni/src/Android.mk
(overwrite the existing one)
4. Edit <project>/jni/src/Android.mk to include your source files
5. create and export an environment variable named NDK_MODULE_PATH that points
to the parent directory of this SDL directory. e.g.:

export NDK_MODULE_PATH="$PWD"/..

6. Edit <project>/src/org/libsdl/app/SDLActivity.java and remove the call to
System.loadLibrary("SDL2") line 42.
7. Run 'ndk-build' (a script provided by the NDK). This compiles the C source

Although this requires an environment variable to be setup, it can be added once and for all to the main Android.mk of the project.
Android.mk
README-android.txt
android-project/jni/src/Android_static.mk
     1.1 --- a/Android.mk	Thu Oct 10 21:50:25 2013 -0700
     1.2 +++ b/Android.mk	Thu Oct 10 21:51:39 2013 -0700
     1.3 @@ -49,3 +49,20 @@
     1.4  LOCAL_LDLIBS := -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
     1.5  
     1.6  include $(BUILD_SHARED_LIBRARY)
     1.7 +
     1.8 +###########################
     1.9 +#
    1.10 +# SDL static library
    1.11 +#
    1.12 +###########################
    1.13 +
    1.14 +LOCAL_MODULE := SDL2_static
    1.15 +
    1.16 +LOCAL_MODULE_FILENAME := libSDL2
    1.17 +
    1.18 +LOCAL_SRC_FILES += $(LOCAL_PATH)/src/main/android/SDL_android_main.c
    1.19 +
    1.20 +LOCAL_LDLIBS := 
    1.21 +LOCAL_EXPORT_LDLIBS := -Wl,--undefined=Java_org_libsdl_app_SDLActivity_nativeInit -ldl -lGLESv1_CM -lGLESv2 -llog -landroid
    1.22 +
    1.23 +include $(BUILD_STATIC_LIBRARY)
     2.1 --- a/README-android.txt	Thu Oct 10 21:50:25 2013 -0700
     2.2 +++ b/README-android.txt	Thu Oct 10 21:51:39 2013 -0700
     2.3 @@ -111,6 +111,28 @@
     2.4  
     2.5  
     2.6  ================================================================================
     2.7 + Build an app with static linking of libSDL
     2.8 +================================================================================
     2.9 +
    2.10 +This build uses the Android NDK module system.
    2.11 +
    2.12 +Instructions:
    2.13 +1. Copy the android-project directory wherever you want to keep your projects
    2.14 +   and rename it to the name of your project.
    2.15 +2. Rename <project>/jni/src/Android_static.mk to <project>/jni/src/Android.mk
    2.16 +   (overwrite the existing one)
    2.17 +3. Edit <project>/jni/src/Android.mk to include your source files
    2.18 +4. create and export an environment variable named NDK_MODULE_PATH that points
    2.19 +   to the parent directory of this SDL directory. e.g.:
    2.20 +
    2.21 +   export NDK_MODULE_PATH="$PWD"/..
    2.22 +
    2.23 +5. Edit <project>/src/org/libsdl/app/SDLActivity.java and remove the call to
    2.24 +   System.loadLibrary("SDL2") line 42.
    2.25 +6. Run 'ndk-build' (a script provided by the NDK). This compiles the C source
    2.26 +
    2.27 +
    2.28 +================================================================================
    2.29   Customizing your application name
    2.30  ================================================================================
    2.31  
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/android-project/jni/src/Android_static.mk	Thu Oct 10 21:51:39 2013 -0700
     3.3 @@ -0,0 +1,12 @@
     3.4 +LOCAL_PATH := $(call my-dir)
     3.5 +
     3.6 +include $(CLEAR_VARS)
     3.7 +
     3.8 +LOCAL_MODULE := main
     3.9 +
    3.10 +LOCAL_SRC_FILES := YourSourceHere.c
    3.11 +
    3.12 +LOCAL_STATIC_LIBRARIES := SDL2_static
    3.13 +
    3.14 +include $(BUILD_SHARED_LIBRARY)
    3.15 +$(call import-module,SDL)LOCAL_PATH := $(call my-dir)