Table Of ContentAndroid Native
Development Kit
Cookbook
A step-by-step tutorial with more than 60 concise recipes
on Android NDK development skills
Feipeng Liu
BIRMINGHAM - MUMBAI
Android Native Development Kit Cookbook
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or
transmitted in any form or by any means, without the prior written permission of the publisher,
except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the
information presented. However, the information contained in this book is sold without
warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers
and distributors will be held liable for any damages caused or alleged to be caused directly or
indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies
and products mentioned in this book by the appropriate use of capitals. However, Packt
Publishing cannot guarantee the accuracy of this information.
First published: March 2013
Production Reference: 1140313
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84969-150-5
www.packtpub.com
Cover Image by Artie Ng (artherng@yahoo.com.au)
Credits
Author Project Coordinator
Feipeng Liu Leena Purkait
Reviewers Proofreader
Roger Belk Aaron Nash
Frank Grützmacher
Sylvain Ratabouil Indexer
Monica Ajmera Mehta
Acquisition Editor
Martin Bell Graphics
Aditi Gajjar
Valentina D'silva
Commissioning Editor
Shreerang Deshpande
Production Coordinator
Shantanu Zagade
Lead Technical Editor
Mayur Hule
Cover Work
Shantanu Zagade
Technical Editors
Lubna Shaikh
Worrell Lewis
About the Author
Feipeng Liu is a technology enthusiast who is focused on multimedia systems and
applications. He started mobile applications development in 2008 on Windows Mobile.
Since Feb 2010, he has been developing apps for Android with NDK. His Android apps have
been used by many users. One of his apps, video converter Android, has reached one
million downloads within 10 months. Feipeng received his B.ENG in Electrical and Electronic
Engineering degree from Nanyang Technological University, and Master of Computing degree
in the Department of Computer Science from National University of Singapore.
I would like to thank Shreerang Deshpande for offering me the opportunity
to author this book and for helping me throughout the writing, and Leena
Purkait for keeping track of its progress and coordination, Mayur Hule,
Lubna Shaikh, and Worrell Lewis for editing the book drafts! I would like to
express my gratitude to other Packt Publishing staff who helped with the
book! My grateful thanks are also extended to Roger, Frank and Sylvain,
who offered great suggestions during the review.
I also would like to thank Assoc. Prof. Wei Tsang Ooi in National University of
Singapore, the supervisor of my master project. A lot of stuff in this book is
based on the things I learnt during the project.
Last but not least, I would like to thank my parents Zhulan Shen and Yi Liu,
and Ms. Yang Xiaoqing for the support and understanding during the writing
of the book.
About the Reviewers
Roger Belk is a 45-year-old self-taught Android developer with 20 + apps in Google’s
Play Store under the developer name BigTexApps. He started out using Google’s & MIT’s
App Inventor and then after two years of learning to use AI, he taught himself to use Java to
build Android apps. He has reviewed two other books, Google App Inventor, Ralph Roberts,
Packt Publishing (ISBN 978-1-84969-212-0) and Android 3.0 Animation, Alex Shaw, Packt
Publishing (ISBN 978-1-84951-528-3).
Frank Grützmacher spent some years in the research of distributed electronic design
tools and worked for several German blue chip companies such as Deutsche Post and AEG.
He was involved in Android platform extensions for a mobile manufacturer. Therefore, on
one hand he knows how to build large enterprise apps and on the other hand how to make
Android system apps.
He is currently working for the IT daughter of the largest German Telco company.
In the past, he has reviewed Corba- and Java-related books for American and
German publishers.
Sylvain Ratabouil is a confirmed IT consultant experienced with C++ and Java
technologies. He worked in the space industry and got involved in aeronautic projects
at Valtech, where he is now taking part in the digital revolution.
As a technology lover, he is passionate about mobile technologies and cannot live or sleep
anymore without his Android smart phone.
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to
your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub
files available? You can upgrade to the eBook version at www.PacktPub.com and as a print
book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up
for a range of free newsletters and receive exclusive discounts and offers on Packt books
and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt’s online digital book
library. Here, you can access, read and search across Packt’s entire library of books.
Why Subscribe?
f Fully searchable across every book published by Packt
f Copy and paste, print and bookmark content
f On demand and accessible via web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view nine entirely free books. Simply use your login credentials for
immediate access.
Table of Contents
Preface 1
Chapter 1: Hello NDK 7
Introduction 7
Setting up an Android NDK development environment in Windows 8
Setting up an Android NDK development environment in Ubuntu Linux 16
Setting up an Android NDK development environment in Mac OS 19
Updating Android NDK 20
Writing a Hello NDK program 22
Chapter 2: Java Native Interface 27
Introduction 28
Loading native libraries and registering native methods 29
Passing parameters and receiving returns in primitive types 34
Manipulating strings in JNI 39
Managing references in JNI 43
Manipulating classes in JNI 50
Manipulating objects in JNI 53
Manipulating arrays in JNI 57
Accessing Java static and instance fields in the native code 62
Calling static and instance methods from the native code 67
Caching jfieldID, jmethodID, and referencing data to improve performance 73
Checking errors and handling exceptions in JNI 76
Integrating assembly code in JNI 80
Chapter 3: Build and Debug NDK Applications 83
Introduction 83
Building an Android NDK application at the command line 84
Building an Android NDK application in Eclipse 94
Building an Android NDK application for different ABIs 97
Building Android NDK applications for different CPU features 102
Table of Contents
Debugging an Android NDK application with logging messages 106
Debugging an Android NDK application with CheckJNI 110
Debugging an Android NDK application with NDK GDB 113
Debugging an Android NDK application with CGDB 116
Debugging an Android NDK application in Eclipse 119
Chapter 4: Android NDK OpenGL ES API 123
Introduction 123
Drawing 2D Graphics and applying transforms with the OpenGL ES 1.x API 127
Drawing 3D graphics and lighting up the scene with the OpenGL ES 1.x API 133
Mapping texture to 3D objects with the OpenGL ES 1.x API 140
Drawing 3D graphics with the OpenGL ES 2.0 API 145
Displaying graphics with EGL 152
Chapter 5: Android Native Application API 159
Introduction 159
Creating a native activity with the native_activity.h interface 161
Creating a native activity with the Android native app glue 166
Managing native windows at Android NDK 174
Detecting and handling input events at Android NDK 178
Accessing sensors at Android NDK 181
Managing assets at Android NDK 186
Chapter 6: Android NDK Multithreading 191
Introduction 191
Creating and terminating native threads at Android NDK 192
Synchronizing native threads with mutex at Android NDK 195
Synchronizing native threads with conditional variables at Android NDK 200
Synchronizing native threads with reader/writer locks at Android NDK 206
Synchronizing native threads with semaphore at Android NDK 212
Scheduling native threads at Android NDK 217
Managing data for native threads at Android NDK 228
Chapter 7: Other Android NDK API 233
Introduction 233
Programming with the jnigraphics library in Android NDK 234
Programming with the dynamic linker library in Android NDK 238
Programming with the zlib compression library in Android NDK 241
Programming audio with the OpenSL ES audio library in Android NDK 247
Programming with the OpenMAX AL multimedia library in Android NDK 259
ii
Table of Contents
Chapter 8: Porting and Using the Existing Libraries with Android NDK 265
Introduction 265
Porting a library as a shared library module with the Android NDK
build system 266
Porting a library as a static library module with the Android NDK
build system 271
Porting a library with its existing build system using the
Android NDK toolchain 275
Using a library as a prebuilt library 280
Using a library in multiple projects with import-module 282
Porting a library that requires RTTI, exception, and STL support 285
Chapter 9: Porting an Existing Application to Android with NDK 293
Introduction 293
Porting a command-line executable to Android with an NDK build system 294
Porting a command-line executable to Android with an NDK
standalone compiler 298
Adding GUI to a ported Android app 305
Using background threads at porting 310
Index 319
iii
Description:My grateful thanks are also extended to Roger, Frank and Sylvain, Roger Belk is a 45-year-old self-taught Android developer with 20 + apps in Google's . Bonus Chapter 2, Developing Games with NDK, discusses writing games