Phil Hassey - game dev blog
Phil Hassey as Wolverine
"What kind of
arrogant jerk
has a website like this?"

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
SRCDIR := ../../src
LOCAL_CFLAGS := -DANDROID_NDK \
-DDISABLE_IMPORTGL \
-DHAS_SOCKETLEN_T \
-DBUILD_IGALCON2 \
-DBUILD_ANDROID \
-DGC_BUILD_ANDROID
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/../../src \
$(LOCAL_PATH)/../../src/igalcon1 \
$(LOCAL_PATH)/../../src/igalcon1/enet
LOCAL_SRC_FILES := \
importgl.c \
$(SRCDIR)/main.cpp $(SRCDIR)/data.cpp $(SRCDIR)/driver-sdl.cpp $(SRCDIR)/timer.c \
$(SRCDIR)/game2.cpp $(SRCDIR)/level.cpp $(SRCDIR)/mygl.cpp \
$(SRCDIR)/menu.cpp $(SRCDIR)/theme.cpp $(SRCDIR)/myview.cpp $(SRCDIR)/pause.cpp \
$(SRCDIR)/settings.cpp $(SRCDIR)/multi.cpp $(SRCDIR)/help.cpp \
$(SRCDIR)/igalcon1/server.c \
$(SRCDIR)/igalcon1/game.c \
$(SRCDIR)/igalcon1/level.c \
$(SRCDIR)/igalcon1/states.c \
$(SRCDIR)/igalcon1/engine.c \
$(SRCDIR)/igalcon1/menu.c \
$(SRCDIR)/igalcon1/missions.c \
$(SRCDIR)/igalcon1/net.c \
$(SRCDIR)/igalcon1/multi.c \
$(SRCDIR)/igalcon1/client.c \
$(SRCDIR)/igalcon1/web.c \
$(SRCDIR)/igalcon1/timer.c \
$(SRCDIR)/igalcon1/md5.c \
$(SRCDIR)/igalcon1/viewgl.c \
$(SRCDIR)/igalcon1/enet/*.c
LOCAL_LDLIBS := -lGLESv1_CM -ldl -llog
include $(BUILD_SHARED_LIBRARY)

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.

2 Responses to “Android Day 2: The NDK”

  1. Greg Says:

    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!

  2. philhassey Says:

    Hmm, good point!

    NDK is native development kit.

    JNI is the Java Native Interface.

    -Phil