Chapter 15. The Native Development Kit (NDK)

The Native Development Kit, or NDK, is an add-on to SDK that helps you integrate native code—code that uses platform-specific features, generally exposed through C or C++ language APIs—within your Android application. The NDK allows your Android application to call some native code and even include some native libraries.

In the Gingerbread release of Android, NDK takes support for native code even further with the introduction of the NativeActivity class. You can now write your entire activity in C or C++. However, NativeActivity is not the subject of this chapter. Here, we’ll look at integrating native C code within your Java Android application.

What Is and Isn’t the NDK For?

The main motivation for developing parts of your app in native code is performance. As you can see, the NDK supports math and graphics libraries well, as well as some supporting system libraries. So graphically and computationally intensive applications are the best candidates for NDK. One could argue that the recent boom in the popularity of mobile games is driving this development as well.

Note that any native code accessible from your app via the Java Native Interface (JNI) still runs inside your application’s Dalvik VM. So it’s subject to the same security sandboxing rules that an Android application lives by. Writing parts of your application in C or C++ just so you can do something that might not be possible in Java usually is not a good reason for NDK. Keep ...

Get Learning Android now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.