Let me paint a picture;
My development environment on linux consists of a text editor with code highlighting (GEdit in my case), GCC/G++, and make. Development goes like this for me: Tickatickatype, Ctrl-S, “make ; ./test” . Using a basic handwritten Makefile isn’t that hard for testing a small project. I make it automate things like adding flags and defines from the command line. In the case of Android, I have it copy my source into the android project, compile it, pack it in an apk, and then copy the apk to dropbox so I can test it on my device.
If you use cygwin on windows with GCC/G++ you can more or less follow along as long as you promise to use your best judgment.
I’ve got three links that I used to figure out how to hack a function android project together with the latest SDL2 source.
I used this to learn how to use the NDK and arrange the project: http://www.dinomage.com/2013/01/howto-sdl-on-android/
I used this to learn how to make an APK file: http://blog.beuc.net/posts/Building_Android_NDK_samples/
This is a prebuilt android project using an older version of SDL2: http://www.midnightfragfest.com/2012/05/04/sdl-2-0-android-project-template/
First you need your android dev environment.
SDK tools (not ADT bundle): http://developer.android.com/sdk/index.html
ANT: I downloaded it with the package manager.
ADB (if you want to test your app on the emulator): Also downloaded with the package manager.
I’m using Linux Mint, an Ubuntu derivative. To install into my home directory, I extracted the sdk and ndk into “~/bin”. Then in “~/.profile”, I added
to the bottom. Be sure to name the directories correctly. This is important so ant can automatically use zipalign from the sdk to finish building your apk. Log out and log back in or you can do it manually: export PATH=”$PATH:$HOME/bin/android-sdk/tools:$HOME/bin/android-ndk”
If you’re doing it the first time you gotta run the android utility to download the packages you need. Since it’s in your path, you can just type “android” in the command line and it just works. Let it download and parse it’s xml for a bit. Then you need the SDK tools and at least API 10. I checked the tools folder, API 10 (Android 2.3.3) and then all the Android 4 APIs.
After those install, you now have your command line android development environment. Now you need a project.
Second: your project.
Download your libraries! Go to http://www.libsdl.org for SDL2 and SDL_image. http://sourceforge.net/projects/libpng/files/ is where you’ll find the latest libpng.
Last night, I wrote down everything I could remember, then recreated the project using those instructions. It worked on the first try. If you’re the kind to skim through things, read every single word, you _______.
Go ahead and extract the three libraries using your favorite archive manager.
Put SDL2/android-project/ wherever you want it. This is your Android Project!
Copy your program into android-project like this: Make a directory android-project/assets/ and copy anything your program loads into it. Copy all your C and/or C++ files to android-project/jni/src/. This is just your source, not SDL. You need to define your main function correctly. It doesn’t hurt anything to do it this way on other platforms.
int main(int argc, char *argv)
Now you need to copy and configure your libraries. Copy SDL2, SDL2_image, and libpng to “android-project/jni/”.
Now remove android-project/jni/SDL2/android-project/ because that’s just silly. There’s plenty of other things to remove if you use your best judgment. I’d hold off on that for now, though.
Fix libpng: copy “android-project/jni/libpng/scripts/pnglibconf.h.prebuilt” to “android-project/jni/libpng/pnglibconf.h”.
Now edit “android-project/jni/SDL2_image/Android.mk”. I’m only using and installing png. Set the other formats to false. You see it when your read it. Then correct “PNG_LIBRARY_PATH”.
Edit “android-project/jni/Application.mk”. I have “APP_STL := gnustl_static” so I can use STL like vectors. Also I have the following architectures “APP_ABI := armeabi-v7a armeabi x86”.
Edit “android-project/jni/src/Android.mk”. First think you need to do is fix LOCAL_C_INCLUDES. Otherwise it will complain when your program asks for such things as “SDL.h”.
SDL_PATH := ../SDL2-2.0.0
SDL_IMAGE_PATH := ../SDL2_image-2.0.0
LOCAL_C_INCLUDES := $(LOCAL_PATH)/$(SDL_PATH)/include \
Add “SDL2_image” to “LOCAL_SHARED_LIBRARIES”. SDL_image compiles libpng into itself, so you don’t need to add it. Then you have to fix LOCAL_SRC_FILES. Point YourSourceHere.c to whatever file your main function is in. Make sure SDL_android_main.c has the correct extension. For some reason it was cpp (same in the older prebuild project) when in the current code it’s just c. The NDK complained there was no rule to build target SDL_android_main.cpp for so long.
Now to edit the almighty “android-project/AndroidManifest.xml”. Change your app designation to whatever from org.libsdl.app. The version code if you want to, too. Change your java activity from SDLActivity to whatever. In that same bracket, add android:configChanges=”orientation” so it will rotate. There’s other things to configure here like if your app is fullscreen or not.
Make a new file: “android-project/com/myname/app/main.java” where “main” is what you named your activity and you can figure out the rest. Now it’s time to write some java code! Well, not really. SDL2/README-android.txt contains a sample that just extends SDLActivity. Change “package com.gamemaker.game” to what’s appropriate and the class to whatever the java file is named.
One last thing bars it from being compilable. Edit “android-project/org/libsdl/app/SDLActivity.java”. You just need to uncomment “System.loadLibrary(“SDL2_image”);” from under the comment “load the .so”.
Now you can put a couple trimmings on your app. Change your icon in “android-project/res/drawable*/” if you want. Also change your app name in “android-project/res/values/strings.xml”.
Now you can build it!
Third: Build It
Open a terminal in android-project. Here’s what you need to do.
” ndk-build ” This compiles the C and C++ source.
” android update project –name test –path . –target “android-10″ ” This configures some junk for ant. I think you don’t need to run this a second time.
” ant debug ” This should build your app and put it in an apk. “android-project/bin/test-debug.apk”
At this stage you can sideload it on your device or the emulator using “adb install test-debug.apk” or downloading it and opening the apk. Depending on your device/carrier, you might need to root your phone to do this. I know my own Samsung Captivate (Galaxy S on AT&T) wouldn’t do this stock, but I’ve been using Cyanogenmod for a long time. Your apk is nowhere near ready to be released. For that, you need to read up on http://developer.android.com about signing your app and whatnot. But now you can test touch controls and whatever else you’d like.
So now you have your app to try out. Quite frankly, after going through all this, it’s a lot better just to use java and the android canvas api. This is especially true for small projects. Android is designed to be the only overhead you need so that every app can use civilized features like managed code and garbage collection. Trying to use a native library with your own native code just serves to add even more overhead. True, you’re going to have to redo some stuff in a different language you might have to learn, but you’ll be making your app a whole lot more portable and modern. I’ll be doing that too.