What is Android NDK?
Chances are, if you’ve found this article, you know already what the Android NDK is. If you’ve stumbled upon here by chance, however, here’s a quick explanation: The Android NDK is a set of tools provided by Google to allow Android developers to code portions of their applications in native C, which then gets compiled into the application to run just the same as any other Android application. The advantage to this is that there is a performance increase for some processes with native code (though not automatically) and some developers can harness it to see performance gains in their applications.
It is important to note that the NDK does not guarantee performance increases; there are really only a few cases where native C compiling really does enhance performance, and for the most part working in the regular Android framework API is recommended by Google. The NDK can also be used to incorporate existing C libraries into your app, but if a Java library is available that serves the same function it’s almost always a better idea to use that.
If, however, you still need to use the Android NDK, here are some handy tips and instructions on how to install it and integrate it in your development environment!
NOTE: The process for installing the NDK on Windows is much more laborious than for Mac or Windows, as the Android NDK does not run natively under Windows and requires the use of cygwin. Once Cygwin is installed, however, the process is much the same, since Cygwin is a UNIX-like CLI that allows for UNIX functionality in Windows. This guide will go through installing the NDK on Linux / Mac first, and then go through the additional steps required to install the NDK on Windows!
Step 1: Acquiring the NDK
NOTE: before you do this, make sure your Android SDK environment is up-to-date! Though they haven’t for awhile, Google releases new versions of the NDK periodically that usually require functions or libraries that are bundled with newer versions of the SDK.
To acquire the NDK, head over to:
and grab the latest version appropriate to your OS (the version current as of this writing is r5b, for Linux/Windows/Mac). The NDK isn’t an installer, so once you’ve got it you’ll have to unzip it to a directory of your choice.
It can be anywhere you like, so long as it’s somewhere readable (I keep mine in
Step 2: Installing the NDK
There’s not much to this step, actually: once you’ve unzipped the NDK, it’s pretty much good to go. To compile your native C code, pop them in the
source/jni directory of your project, and create a makefile. Once that’s done, head over to your project directory and run the ndk-build script. Once the build finishes, compile your application normally; the Android compiler will package the native C source code into the generated apk file.
This guide assumes that you have a makefile and source; creating the makefile is another matter entirely, and beyond the scope of this article. Here is an an in-depth guide to creating makefiles should you need to do so:
And there you have it – you have a fully functioning Android NDK to compile native C code with! Pretty easy, all things considered!
ADDENDUM: Windows Installs
If you are on Windows, however, you’re going to need a bit more help than that. You’re going to need to use Cygwin, a UNIX-like interface that lets Windows users get a UNIX-style interface. To do that head over to www.cygwin.com and download their setup.exe. Run it, selecting your install directories and mirrors as recommended, until you get to the package installation screen seen here:
Open the Devel category, and select the make package. Also install any dependencies that Cygwin prompts you to install; it’ll have to download all these packages from the Internet, so depending on your connection speed this may take a couple of minutes. Once that’s done, head over to wherever you installed Cygwin (for me, it was
c:\cygwin) and edit the cygwin.bat file. It should look like this:
@echo off C: chdir C:\cygwin\bin bash --login -i
In order to use cygwin with the NDK, we’re going to have to add some stuff in there. Delete the text in the file and put this into it:
@echo off set IS_UNIX= set DEV_ROOT=c:/androidproject/android/testproject set JAVA_HOME=c:/Progra~1/Java/jdk1.6.0_21 set CLASSPATH=c:/androidproject/android/testproject/obj set PATH=c:/asdk/android-sdk-windows/tools;c:/android_ndk/android-ndk-r4b set ANDROID_NDK_ROOT=/cygdrive/c/android_ndk/android-ndk-r4b set NDK_PROJECT_PATH=/cygdrive/c/androidproject/android/testproject C: chdir C:\cygwin\bin bash --login -i
A quick explanation of this batch file.
DEV_ROOT should point to the project that’s using the native C code,
CLASSPATH should point to the
/obj directory in that path, and the
PATH variables should point to the SDK
/tools folder as well as the
android-ndk folder you unzipped above. The
ANDROID_NDK_ROOT variable points to your NDK install, and the
NDK_PROJECT_PATH should point to the project in question.
Also, even though we’re using Windows, Cygwin interprets slashes in true POSIX style; make sure to keep the forward slash and don’t use any backslashes.
Once you’ve edited that, save your changes and run cygwin.bat. It will drop you into a UNIX-like shell. Just navigate to the Android NDK directory, and run this:
The ndk-build script will pull all the necessary variables from the cygwin.bat we edited earlier, and your C code will merrily start compiling!
So there you have it – a brief introduction on what the NDK is as well as how to install it on your own system, should you need to use it. It’s a powerful tool, if used correctly, and here’s to hoping the knowledge we’ve imparted to you in this article can help you streamline or speed up your existing Android applications using the power of C and the Android NDK!
Help us spread the word!
If you liked this article, consider enrolling in one of these related courses:
|Jul 20-22||Java for Android|
|Aug 10-13||Android Bootcamp|
|Aug 10-11||Android Application Development|
|- Classroom - Online|