Book Image

Mastering Android NDK

Book Image

Mastering Android NDK

Overview of this book

Android NDK is used for multimedia applications that require direct access to system resources. NDK is also the key for portability, which in turn allows a reasonably comfortable development and debugging process using familiar tools such as GCC and Clang toolchains. This is a hands-on guide to extending your game development skills with Android NDK. The book takes you through many clear, step-by-step example applications to help you further explore the features of Android NDK and some popular C++ libraries and boost your productivity by debugging the development process. Through the course of this book, you will learn how to write portable multi-threaded native code, use HTTP networking in C++, play audio files, use OpenGL ES 3, and render high-quality text. Each chapter aims to take you one step closer to building your application. By the end of this book, you will be able to create an engaging, complete gaming application.
Table of Contents (17 chapters)
Mastering Android NDK
About the Authors
About the Reviewers

Creating an Ant-based application template manually

Let's start with the lowest level and create a template for our applications buildable with Apache Ant. Every Android application which is to be built using Apache Ant should contain a predefined directories structure and configuration .xml files. This is usually done using Android SDK tools and IDEs. We will explain how to do it by hand to let you know the machinery behind the curtains.


Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

For this book, the source code files can be downloaded or forked from the following GitHub repository as well:

The directory structure of our minimalistic project looks like the following screenshot (see the source code bundle for the complete source code):

We need to create the following files within this directory structure:

  • res/drawable/icon.png

  • res/values/strings.xml

  • src/com/packtpub/ndkmastering/

  • AndroidManifest.xml

  • build.xml


The icon icon.png should be there, and currently contains a dummy image of an Android application:

The file strings.xml is required to make use of the Android localization system. In the manifest AndroidManifest.xml, we use the string parameter app_name instead of the actual application name. The file strings.xml resolves this parameter into a human readable string:

<?xml version="1.0" encoding="utf-8"?>
  <string name="app_name">AntApp1</string>

The Java source code of the minimal buildable application is in the file:

package com.packtpub.ndkmastering;
public class App1Activity extends Activity

The rest three files, AndroidManifest.xml, build.xml, and, contain the description of the project necessary for Ant to build it.

The manifest AndroidManifest.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

Our application will require Android 4.4 (API Level 19) and is tested with Android 6.0 (API Level 23):

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23" />

Most of the examples in this book will require OpenGL ES 3. Let's mention it here:

<uses-feature android:glEsVersion="0x00030000"/>
<application android:label="@string/app_name"

Here is the name of the main activity:

<activity android:name="com.packtpub.ndkmastering.App1Activity"

We want a fullscreen application in the landscape orientation:


Our application can be started from the system launcher. The displayable name of the application is stored in the app_name parameter:

  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER" />


You can read the official Google documentation on the application manifest at

The file build.xml is much simpler and will resemble mostly what Android tools would generate:

<?xml version="1.0" encoding="UTF-8"?>
<project name="App1" default="help">
  <loadproperties srcFile="" />
  <fail message="sdk.dir is missing. Make sure to generate using 'android update project' or to inject it through an env var"
  <import file="${sdk.dir}/tools/ant/build.xml" />

There is a difference to Android SDK Tools, since we don't use here. This was done just for the sake of simplicity and just has an educational purpose.

The same situation exists with the file, which contains platform-specific declarations:


Now, our first application (which does not even contain any native code yet) is ready to be built. Use the following one-liner to build it:

$ ant debug

If everything was done correctly, you should see the tail of the output similar to the following:

To install an .apk file from the command line, run adb install -r bin/App1-debug.apk to install the freshlybuilt .apk on your device. Start the application from your launcher (AntApp1) and enjoy the black screen. You can use the BACK key to exit the application.