Running Android NDK
We're moving to the Ouya console!
In our past projects we've used XNA, which is Windows based, and will not run on the Ouya. (Unless you take a look at Monogame). Since we're moving to this impressive new platform, we decided to do some research and came to the conclusion that we would create our own in house engine. (We're engine programming junkies)
For our new engine, we wanted to make sure we could run our game on nearly anything.
Android (the Ouya specifically)
Windows
Linux
and eventually, Mac and iOS
To support these platforms we needed to go to the lowest common denominator, which is C. Also, most platforms support at least a small portion of C++ (NDK, and iOS).
So with that, we chose C++, which made the Android NDK a must. Though we did have a very heated discussion on whether or not to stick with a purely C engine.
What to download
Example Solution : Download
So what do we need to get started?
The Android SDK
The Android NDK
Apache Ant Compiler
An Android Device(Recomended) or an Android Emulator setup
Download and install both the Android SDK and Android NDK. I chose to extract my NDK directory to my local C Drive with the name Android-NDK-r8b. (Note that the last 3 characters may be different if you're on a different version)
After downloading the Apache Ant zip file, I extracted it again to my local C Drive.
A side note, nearly every resource I found on the web tells you that you need Cygwin to compile an NDK application on a Windows machine. I went through the trouble of downloading and installing it (which took hours...) and came up with a solution that does not use Cygwin.
Setting up Environment Variables
To make the process of updating, compiling, and installing the Android NDK applications, I updated my Environment PATH by adding these additional paths :
Android SDK Tools (C:\Users\ghoofman\AppData\Local\Android\android-sdk\tools)
Android SDK Platform Tools (C:\Users\ghoofman\AppData\Local\Android\android-sdk\platform-tools)
Android NDK (C:\android-ndk-r8b)
Apache ANT (C:\apache-ant-1.8.4\bin)
This makes it easy to call the ndk-build, ant, and adb commands from a command line window.
Understanding the Project Structure
An Android NDK Project consists of a small initial folder structure.
Root project ( / )
C/C++ Files ( /jni )
Resources ( /res/values )
Java Source ( /src/com/namespace/project )
In the root project folder you'll find 2 project files.
AndroidManifest.xml (Project Build Information)
Build.xml (Java Application Information)
There's only strings.xml in Resources which is where the name of your project is located.
Your Java Source directory is where the main Java connecting files are stored. These files simply call back to your C/C++ Code.
The most important part of an NDK application is the /jni folder. Here is where your Android.mk file and C/C++ files will live. The Android.mk file defines how your NDK project is built.
Building the NDK Project
First we have to update the Android project, so that it knows what's in the project, and which Android platform to target. To do this we use the following command :
android update project --target 10 --path . --subprojects
For my specific needs, I need OpenGL ES 2.0 for Ouya, so I'm targeting Android platform 10. Next we need to compile the C/C++ code into a .so (Android Library file) so that the Java application can access it.
ndk-build
Now that we have a .so lib file we can build our Java application with :
ant debug
Thus creating the .apk file we're used to having on Android. So to install we can call :
adb install -r bin{YOURPROJECTNAME}-debug.apk
And to get console output on your device run :
adb logcat
Creating an Install.bat
With the environment PATH variable set up, it becomes easy to create a simple batch file to update, compile, and install an NDK project. Open up notepad and enter the following lines :
call android update project --target 10 --path . --subprojects
call ndk-build
call ant debug
call adb install -r bin\SMRF-debug.apk
call adb logcat
Save the file as Install.bat into the root directory of your NDK project and then simply double click it. It will do an update, compile, and then install to the first default Android device on your machine.