Android NDK Beginner's Guide Discover the native side of Android and inject the power of C/C++ in your applications Sylvain Ratabouil BIRMINGHAM - MUMBAI Android NDK Beginner's Guide Copyright © 2012 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: January 2012 Production Reference: 1200112 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84969-152-9 www.packtpub.com Cover Image by Marcus Grandon ([email protected]) Credits Author Project Coordinator Sylvain Ratabouil Jovita Pinto Reviewers Proofreader Marko Gargenta Lynda Sliwoski Dr. Frank Grützmacher Indexer Robert Mitchell Hemangini Bari Acquisition Editor Graphics Sarah Cullington Valentina D'silva Lead Technical Editor Production Coordinators Dayan Hyames Prachali Bhiwandkar Technical Editor Melwyn D'sa Pramila Balan Nilesh Mohite Copy Editor Cover Work Laxmi Subramanian Alwin Roy About the Author Sylvain Ratabouil is a confirmed IT consultant with experience in C++ and Java technologies. He worked for the space industry and got involved in aeronautic projects at Valtech Technologies where he now takes part in the Digital Revolution. Sylvain earned the master's degree in IT from Paul Sabatier University in Toulouse and did M.Sc. in Computer Science from Liverpool University. As a technology lover, he is passionate about mobile technologies and cannot live or sleep without his Android smartphone. I would like to thank Steven Wilding for offering me to write this book; Sneha Harkut and Jovita Pinto for awaiting me with so much patience; Reshma Sundaresan, and Dayan Hyames for putting this book on the right track; Sarah Cullington for helping me finalizing this book; Dr. Frank Grützmacher, Marko Gargenta, and Robert Mitchell for all their helpful comments. About the Reviewers Dr. Frank Grützmacher has worked for several major German firms in the area of large distributed systems. He was an early user of different Corba implementations in the past. He got his Ph.D. in the field of electrical engineering, but with the focus on distributed heterogeneous systems. In 2010, he was involved in a project, which changed parts of the Android platform for a manufacturer. From there, he got his knowledge about the android NDK and native processes on this platform. He has already worked as a reviewer for another Android 3.0 book. Robert Mitchell is an MIT graduate with over 40 years experience in Information Technology and is semiretired. He has developed software for all the big iron companies: IBM, Amdahl, Fujitsu, National Semiconductor, and Storage Technology. Software companies include Veritas and Symantec. Recent languages that he knows are Ruby and Java, with a long background in C++. 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 [email protected] 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? Fully searchable across every book published by Packt Copy and paste, print and bookmark content 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: Setting Up your Environment 7 Getting started with Android development 7 Setting up Windows 8 Time for action – preparing Windows for Android development 8 Installing Android development kits on Windows 12 Time for action – installing Android SDK and NDK on Windows 13 Setting up Mac OS X 18 Time for action – preparing Mac OS X for Android development 18 Installing Android development kits on Mac OS X 20 Time for action – installing Android SDK and NDK on Mac OS X 20 Setting up Linux 22 Time for action – preparing Ubuntu Linux for Android development 22 Installing Android development kits on Linux 27 Time for action – installing Android SDK and NDK on Ubuntu 27 Setting up the Eclipse development environment 29 Time for action – installing Eclipse 29 Emulating Android 33 Time for action – creating an Android virtual device 33 Developing with an Android device on Windows and Mac OS X 37 Time for action – setting up your Android device on Windows and Mac OS X 37 Developing with an Android device on Linux 39 Time for action – setting up your Android device on Ubuntu 39 Troubleshooting a development device 42 Summary 43 Chapter 2: Creating, Compiling, and Deploying Native Projects 45 Compiling and deploying NDK sample applications 46 Time for action – compiling and deploying the hellojni sample 46 Table of Contents Exploring Android SDK tools 51 Android debug bridge 51 Project configuration tool 54 Creating your first Android project using eclipse 56 Time for action – initiating a Java project 56 Introducing Dalvik 59 Interfacing Java with C/C++ 60 Time for action – calling C code from Java 60 More on Makefiles 65 Compiling native code from Eclipse 67 Time for action – creating a hybrid Java/C/C++ project 67 Summary 72 Chapter 3: Interfacing Java and C/C++ with JNI 73 Working with Java primitives 74 Time for action – building a native key/value store 75 Referencing Java objects from native code 85 Time for action – saving a reference to an object in the Store 85 Local and global JNI references 90 Throwing exceptions from native code 91 Time for action – raising exceptions from the Store 92 JNI in C++ 96 Handling Java arrays 96 Time for action – saving a reference to an object in the Store 97 Checking JNI exceptions 106 Summary 107 Chapter 4: Calling Java Back from Native Code 109 Synchronizing Java and native threads 110 Time for action – running a background thread 111 Attaching and detaching threads 120 More on Java and native code lifecycles 121 Calling Java back from native code 122 Time for action – invoking Java code from a native thread 122 More on callbacks 133 JNI method definitions 134 Processing bitmaps natively 135 Time for action – decoding camera feed from native code 136 Summary 146 Chapter 5: Writing a Fully-native Application 147 Creating a native activity 148 Time for action – creating a basic native activity 148 [ ii ] Table of Contents Handling activity events 155 Time for action – handling activity events 155 More on Native App Glue 166 UI thread 167 Native thread 168 Android_app structure 170 Accessing window and time natively 171 Time for action – displaying raw graphics and implementing a timer 172 More on time primitives 181 Summary 181 Chapter 6: Rendering Graphics with OpenGL ES 183 Initializing OpenGL ES 184 Time for action – initializing OpenGL ES 184 Reading PNG textures with the asset manager 193 Time for action – loading a texture in OpenGL ES 194 Drawing a sprite 208 Time for action – drawing a Ship sprite 209 Rendering a tile map with vertex buffer objects 220 Time for action – drawing a tile-based background 221 Summary 238 Chapter 7: Playing Sound with OpenSL ES 239 Initializing OpenSL ES 241 Time for action – creating OpenSL ES engine and output 241 More on OpenSL ES philosophy 248 Playing music files 249 Time for action – playing background music 249 Playing sounds 256 Time for action – creating and playing a sound buffer queue 257 Event callback 266 Recording sounds 268 Summary 272 Chapter 8: Handling Input Devices and Sensors 273 Interacting with Android 274 Time for action – handling touch events 276 Detecting keyboard, D-Pad, and Trackball events 288 Time for action – handling keyboard, D-Pad, and trackball, natively 289 Probing device sensors 298 Time for action – turning your device into a joypad 300 Summary 313 [ iii ]
Description: