I went ahead and installed the JDK in a normal directory, The same went for the Android SDK. The NDK is a 7zip file, that I went ahead and put at the root of my C drive because I’m that kind of guy. Next I unpacked ant into the NDK directory, and SDL. For SDL be sure to use some command line or other unzip tool that makes sure that the text files are translated appropriately, otherwise they are impossible to read in good editing tools, like notepad.

Now to stage the project directory. The ‘Android project’ that we are going to build out of is *INSIDE* the SDL archive. And SDL needs to be inside the project directory, so we xcopy out the bits we need.

Now we need to set some environment variables. Again this is where my stuff is, yours may be different.

set NDK_ROOT=C:\android-ndk-r10d
set ANDROID_HOME=”C:\Program Files (x86)\Android\android-sdk\”
set PATH=%NDK_ROOT%\apache-ant-1.9.4\bin;”C:\Program Files\Java\jdk1.7.0_79\bin”;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%PATH%

Now we need to edit the jni\src\Android.mk and point it to your sources. In this case I’m using dinomage.com‘s simple main.c & image.bmp as I haven’t written anything exciting yet. Change YourSourceHere.c, to main.c

Now copy in the image.bmp file into the assets directory:

md assets
copy image.bmp assets

Now we can fire up the native compiler tools, and by default it’ll compile for ARM thumb, ARM v7a, and x86

..\ndk-build

Now for the java. Android is a java platform, so now we have to compile the wrapper that calls our C/C++ code. First we have to set what version of Android we are compiling for:

android update project –path . –subprojects -t 12

And now we compile with ‘ant’ by typing in:

ant debug

And if all goes well you’ll get:

BUILD SUCCESSFUL
Total time: 2 seconds

Now to run the program, we can fire up one of the emulators (you will have to configure one no doubt) using AVD. I just clicked around, and picked something that’d launch. The shell seems to crash a lot, but otherwise yeah.

Well it turns out signal was an inline function until platform android-21, now it’s not inline anymore. When you use the ndk r10, android-21 is used by default but it’s not fully retro-compatible with devices running former Android versions. In this case, signal can’t be found on the emulator. (It did run properly on my Lollipop phone).

Fixing this is simple just edit jni\Application.mk and add in the APP_PLATFORM line:

6 thoughts on “Building SDL2 for Android”

> use some command line or other unzip tool that makes sure that the text files are translated appropriately, otherwise they are impossible to read in good editing tools, like notepad

I’d say Notepad is not a good editing tool precisely because it only handles CRLF line endings (and the wrapping support is bad too). Have you tried Notepad++? Still easy to use, but a lot more powerful.

Changing line endings when you extract a .zip file can sometimes break things depending on the other tools you’re using, so I avoid it. Obviously it works in this case.

Yeah, I’ve tried notepad++, its the only good way to deal with chinese text when emailing stuff around.

But getting something to build and run on Android was such a massive PITA, but kind of rewarding too. I just have a bunch of other things to figure out, like screen scaling, input (which I think it sees touches like mouse taps) GPS, music and all kinds of other stuff….