This is an updated post from the OPifex Entertainment blog.
The Android NDK can be a complicated beast to setup when you first jump into it. This short guide will help you with some of the pitfalls that I found.
What do you need to get started?
- Example Solution
- The Android SDK
- The Android NDK
- Apache Ant Compiler
- An Android Device(Recomended) or an Android Emulator
Download all of the above zip files and extract them. I chose to extract each of them to my local C Drive:
- C:\Android-NDK-r8b
- C:\Android-SDK
- C:\Apache-Ant
A side note, most of the resources I found on the web tells you to use 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 this guide that does not use Cygwin.
Setting up Environment Variables
To make the process of updating, compiling, and installing the Android NDK applications easier, I updated my Environment PATH variable by adding these additional paths :
- C:\android-sdk\tools (Android SDK Tools)
- C:\android-sdk\platform-tools (Android SDK Platform Tools)
- C:\android-ndk-r8b (Android NDK)
- C:\apache-ant\bin (Apache ANT)
The following should be what's added to your PATH environment variable :
C:\android-sdk\tools;C:\android-sdk\platform-tools;C:\android-ndk-r8b;C:\apache-ant\bin
This makes it easy to call the ndk-build, ant, and adb commands from a command line window without having to specify the full path to the respective binaries.
Understanding the Project Structure
An Android NDK Project consists of a small initial folder structure.
- C/C++ Files ( /jni )
- Resources ( /res/values )
- Java Files ( /src/com/namespace/project )
In the root of your project folder you'll find 2 important files.
- AndroidManifest.xml (Project Build Information)
- Build.xml (Java Application Information)
The Resources folder will contain only strings.xml which is where the name of your project is located.
Your Java Files directory is where the main Java to C/C++ 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 to build. To do this we need to target an Android platform. To find the id of your Android platform run this in the cmd prompt:
android list target
Find the id in the list and run the following command from your NDK Project Directory Root :
android update project --target 10 --path . --subprojects
For my specific needs, I need OpenGL ES 2.0 for the Ouya, so I'm targeting Android-16 which happends to be id 10 on my system.
Next we need to compile the C/C++ code into a .so (Android Library file) so that the Java application can access it. So run the following command (again from the NDK Project Directory Root)
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 run :
adb install -r bin\{YOURPROJECTNAME}-debug.apk
And to get console output on your device run :
adb logcat
Creating an Install.bat
Typing in the commands one after another is rather tedius. So lets create a simple batch file to update, compile, and install the 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 -c
call adb logcat > log.txt
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. It will also clear your Android log, and start sending all data to log.txt.