Book Image

Android Native Development Kit Cookbook

By : Liu Feipeng
Book Image

Android Native Development Kit Cookbook

By: Liu Feipeng

Overview of this book

Building Android applications would usually mean that you spend all of your time working in Java. There are however times when this is not the most efficient or best method for the application being built. This is where Android NDK comes in. Android NDK allows the developer to write in Native C/C++, giving you the power to reuse code and libraries and also, in most cases, increase the speed and efficiency of your application.The "Android Native Development Kit Cookbook" will help you understand the development, building, and debugging of your native Android applications. We will discover and learn JNI programming and essential NDK APIs such as OpenGL ES, and the native application API. We will then explore the process of porting existing libraries and software to NDK. By the end of this book you will be able to build your own apps in NDK apps."Android Native Development Kit Cookbook" begins with basic recipes that will help you in the building and debugging of native apps, and JNI programming. The recipes cover various topics of application development with Android NDK such as OpenGL programming and Multimedia programming. We will begin with a simple recipe, Hello NDK, before moving on to cover advanced topics with recipes on OpenGL ES that focus on 2D and 3D graphics, as well as recipes that discuss working with NDK and external APIs. If you are looking for ways to make your application available in Android and take measures to boost your application's performance, then this Cookbook is for you.
Table of Contents (16 chapters)
Android Native Development Kit Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

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).

What this book covers

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.

What you need for this book

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.

Who this book is for

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.

Conventions

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."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

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 , 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.

Customer support

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.

Downloading the example code

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.

Errata

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

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 with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.