SDL2 on Android - preet/scratch GitHub Wiki

This page describes how to build SDL2 as a standalone shared library for Android. These instructions work for SDL 2.0.4.x (unstable)


$ hg clone http://hg.libsdl.org/SDL
  • Copy the android-project directory somewhere outside of the cloned SDL directory
$ cd SDL
$ cp -r android-project ../SDLAndroidBuild
  • Remove the 'src' directory in /jni -- we're building a standalone version of SDL2
  • Copy the SDL directory within the jni folder in this new directory (its a bit weird)
$ cd ..
$ cp -r SDL SDLAndroidBuild/jni
  • Run ndk-build in SDLAndroidBuild
  • Shared libraries for all architectures should be output in SDLAndroidBuild/libs
  • Headers can be copied directly from the original SDL source directory
  • Example setup
$ mkdir -p build_env/sdl2/include/SDL2
$ cp -r SDLAndroidBuild/libs build_env/sdl2
$ cp -r SDL/include build_env/sdl2/include/SDL2

In the application that links to SDL2 (ie. whichever lib has the 'main' function), you'll need to include SDL_android_main.c in your project:

SDL/src/main/android/SDL_android_main.c

You need to modify the include paths based on how your project file includes header paths

#include "SDL_main.h" might become #include <SDL2/SDL_main.h> etc

SDL_android_main.c has the following include which refers back to the SDL source tree:

#include "../../SDL_internal.h"

Its not clear what this is for but things seem to work if you just remove this. Here's another example of it being removed for a similar purpose: https://forums.libsdl.org/viewtopic.php?t=10264&highlight=SDL_internal

If the file is compiled as C++ (or you copy the contents into a c++ file) remember to use extern "C" {} to have C linkage.