Since its first release in 2008, Android has become the largest mobile platform in the world. The total number of apps in Google Play is expected to reach 1,000,000 in mid 2013. Most of the Android apps are written in Java with Android Software Development Kit (SDK). Many developers only write Android code in Java even though they are experienced with C/C++, without realizing what a powerful tool they are giving up.
Android Native Development Kit (NDK) was released in 2009 to help developers write and port native code. It offers a set of cross-compiling tools and a few libraries. Programming in NDK offers two main advantages. Firstly, you can optimize your apps in native code and boost performance. Secondly, you can reuse a large number of existing C/C++ code. Android Native Development Kit is a practical guide to help you write Android native code with NDK. We will start with the basics such as Java Native Interface (JNI), and build and debug a native app (chapter 1 to 3). We will then explore various libraries provided by NDK, including OpenGL ES, Native Application API, OpenSL ES, OpenMAX AL, and so on (Chapters 4 to 7). After that, we will discuss porting existing applications and libraries to Android with NDK (Chapters 8 and 9). Finally, we will demonstrate how to write multimedia apps and games with NDK (Bonus chapters 1 and 2).
Chapter 1, Hello NDK, covers how to set up an Android NDK development environment in Windows, Linux, and MacOS. We will write a "Hello NDK" application at the end of the chapter.
Chapter 2, Java Native Interface, describes the usage of JNI in detail. We will call native methods from the Java code and vice versa.
Chapter 3, Build and Debug NDK Applications, demonstrates building native code from a command line and Eclipse IDE. We will also look at debugging native code with gdb
, cgdb
, eclipse, and so on.
Chapter 4, Android NDK OpenGL ES API, illustrates OpenGL ES 1.x and 2.0 APIs. We will cover 2D drawing, 3D graphics, texture mapping, EGL, and so on.
Chapter 5, Android Native Application API, discusses Android native application APIs, including managing native windows, accessing sensors, handling input events, managing assets, and so on. We will see how to write a pure native app in this chapter.
Chapter 6, Android NDK Multithreading, depicts Android multithreading API. We will cover creating and terminating native threads, various thread synchronization techniques (mutex, conditional variables, semaphore, and reader/writer lock), thread scheduling, and thread data management.
Chapter 7, Other Android NDK API, discusses a few more Android libraries, including jnigraphics
, the dynamic linker library, the zlib
compression library, the OpenSL ES library, and the OpenMAX AL library.
Chapter 8, Porting and Using Existing Libraries with Android NDK, describes various techniques of porting and using existing C/C++ libraries with NDK. We will port the boost
library at the end of the chapter.
Chapter 9, Porting Existing Applications to Android with NDK, provides a step-by-step guide for porting an existing application to Android with NDK. We use an open source image resizing program as an example.
Bonus Chapter 1, Developing Multimedia Applications with NDK, demonstrates how to write multimedia applications with the ffmpeg
library. We will port the ffmpeg
library and use the library APIs to write a frame grabber application.
Bonus Chapter 2, Developing Games with NDK, discusses writing games with NDK. We will port the Wolfenstein 3D game to show how to set up game display, add game control, and enable audio effects for a game.
You can download the bonus chapters from http://www.packtpub.com/sites/default/files/downloads/Developing_Multimedia_Applications_with_NDK.pdf and http://www.packtpub.com/sites/default/files/downloads/Developing_Games_with_NDK.pdf.
A computer with Windows, Ubuntu Linux, or MacOS installed is necessary (Linux or MacOS is preferable). Although we can run Android apps with an emulator, it is slow and inefficient for Android development. Therefore, it is recommended to have an Android device.
The book assumes a basic understanding of C and C++ programming languages. You should also be familiar with Java and Android SDK.
Note that the sample code of this book is based on Android ndk r8 unless otherwise stated, since it is the latest version of NDK at the time of writing. By the time the book is published, there should be newer versions. The code should also run on any newer versions. Therefore we can install NDK r8 or later.
The book is written for anyone who is interested in writing native code for Android. The chapters are arranged from basic to intermediate to advanced, and they are relatively independent. Readers who are new to NDK are recommended to read from the beginning to the end, while readers who are familiar with NDK can pick up any specific chapters or even specific recipes.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "Windows NDK comes with a new ndk-build.cmd
build script."
A block of code is set as follows:
#include <string.h> #include <jni.h> jstring Java_cookbook_chapter1_HelloNDKActivity_naGetHelloNDKStr(JNIEnv* pEnv, jobject pObj) { return (*pEnv)->NewStringUTF(pEnv, "Hello NDK!"); }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := framegrabber
LOCAL_SRC_FILES := framegrabber.c
#LOCAL_CFLAGS := -DANDROID_BUILD
LOCAL_LDLIBS := -llog -ljnigraphics -lz
LOCAL_STATIC_LIBRARIES := libavformat_static libavcodec_static libswscale_static libavutil_static
include $(BUILD_SHARED_LIBRARY)
$(call import-module,ffmpeg-1.0.1/android/armv5te)
Any command-line input or output is written as follows:
$sudo update-java-alternatives -s <java name>
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Go to Control Panel | System and Security | System | Advanced system settings."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title via the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.