Android Day 2: The NDK
The goal today is to get the Galcon codebase to compile using the NDK. This will create a single .so file which will be accessed via JNI.
Step 1 – Downloading the NDK:
Download the NDK for native C/C++ code. I plan on doing as little Java as possible, so I’m going to find out how well this stuff works.
http://developer.android.com/sdk/ndk/index.html
Step 2 – Building a NDK sample:
Trying to build an NDK sample. I went into ndk/samples/<whatever> and ran ../../ndk-build (as per instructions in previous link). This worked fine.
But when running in Eclipse, I had some trouble with some examples, but not with some of the other ones. I’m not sure what that is about, but for now I’m going to forge on and not worry about it.
Step 3 – Preparing my project for the NDK:
I’ve created an android folder in my Galcon project for storing all this ports info. I’ve created
android/jni/ # a folder
android/jni/Android.mk # a makefile containing
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := igalcon2
LOCAL_CFLAGS := #VARIOUS C FLAGS for define hacks
LOCAL_C_INCLUDES := #VARIOUS INCLUDE FOLDERS, RELATIVE TO android/jni/, so ../../src
LOCAL_SRC_FILES := #The c files, ../../src/whatever.c
LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog
include $(BUILD_SHARED_LIBRARY)
android/default.properties # a file needed to build properly
this file must contain:
“target=android-4” for GLES
It instructs ndk-build which platform we’re using. Without it, the NDK can’t find the GLES headers.
Then I was able to run ndk-build while in the android folder and it started building my project.
Step 4 – OpenGL Includes:
I had to manage some various quirks in the C code and whatnot as I went.
The includes for GLES:
#include <GLES/gl.h>
#include <GLES/glext.h>
Step 5 – Installing a port of STL for the NDK:
Then I found that STL is not supported in the NDK. So I grabbed stlport from www.stlport.org .. Not needing iostream, etc myself, I just moved the stlport folder over so I could include templates from it. This gave endianess and compiler unrecognized type errors. Found this android specific STL port (should be patched into newer versions of stlport eventually.)
http://www.anddev.org/viewtopic.php?p=29939
I had to add this to my CPPFLAGS: -D_STLP_USE_SIMPLE_NODE_ALLOC to avoid having it complain “stlport/stl/_alloc.h:210: undefined reference to `std::__node_alloc::_M_allocate(unsigned int&)”
—
From here it looks like a long way till I have a port working though. I need to get graphics rendering, touch input, keyboard input, music handling, and other little things that just make the game work. If anyone can give me tips on this tonight, feel free 🙂 I’m not excited about using JNI for all this. I’ve heard there are SDL ports, which I’m going to be considering. I also hear I’ve got to download all the game assets separately from the game itself, which seems painful. Time will tell on if this port will work out.
August 3rd, 2010 at 3:44 pm
Hi Phil,
You might consider using the tag in your writing – it enables you to include an explanation for your acronyms using the title attribute. That way, when the user mouses over an acronym, they can see what it stands for. I found this post was a little bit of an alphabet soup in a lot of places.
I’m like you, not so familiar with Java and Android, so all these acronyms are fairly meaningless to me. Your post might be more helpful to other newcomers if you explained them in a non-obtrusive way like the abbr tag.
Otherwise, thanks for sharing your experiences!
August 4th, 2010 at 9:50 am
Hmm, good point!
NDK is native development kit.
JNI is the Java Native Interface.
-Phil