ebook img

Pro Android C++ with the NDK PDF

403 Pages·2012·5.27 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Pro Android C++ with the NDK

Pro Android C++ with the NDK Onur Cinar Apress Pro Android C++ with the NDK Copyright © 2012 by Onur Cinar Th is work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifi cally the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfi lms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifi cally for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher's location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN 978-1-4302-4827-9 ISBN 978-1-4302-4828-6(eBook) Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefi t of the trademark owner, with no intention of infringement of the trademark. Th e use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identifi ed as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. Th e images of the Android Robot (01 / Android Robot) are reproduced from work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. Android and all Android and Google-based marks are trademarks or registered trademarks of Google, Inc., in the U.S. and other countries. Apress Media, L.L.C. is not affi liated with Google, Inc., and this book was written without endorsement from Google, Inc. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. Th e publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Steve Anglin Technical Reviewer: Grant Allen Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeff rey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editor: Brigid Duff y Copy Editor: Mary Behr Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/. Dedicated to my son Deren, my wife Sema, and my parents, Zekiye and Dogan, for their love, continuous support, and always encouraging me to pursue my dreams. I could not have done this without all of you. —Onur Cinar Contents at a Glance About the Author ...............................................................................................................xix About the Technical Reviewer ...........................................................................................xxi Preface ............................................................................................................................xxiii ■ Chapter 1: Getting Started with C++ on Android ...............................................................1 ■ Chapter 2: Exploring the Android NDK .............................................................................41 ■ Chapter 3: Communicating with Native Code using JNI ..................................................67 ■ Chapter 4: Auto-Generate JNI Code Using SWIG ..............................................................95 ■ Chapter 5: Logging, Debugging, and Troubleshooting ...................................................127 ■ Chapter 6: Bionic API Primer .........................................................................................155 ■ Chapter 7: Native Threads .............................................................................................179 ■ Chapter 8: POSIX Socket API: Connection-Oriented Communication ............................209 ■ Chapter 9: POSIX Socket API: Connectionless Communication .....................................247 ■ Chapter 10: POSIX Socket API: Local Communication ...................................................259 ■ Chapter 11: C++ Support ...............................................................................................275 v vi Contents at a Glance ■ Chapter 12: Native Graphics API ...................................................................................285 ■ Chapter 13: Native Sound API .......................................................................................335 ■ Chapter 14: Profi ling and NEON Optimization ...............................................................363 Index .................................................................................................................................381 Contents About the Author ...............................................................................................................xix About the Technical Reviewer ...........................................................................................xxi Preface ............................................................................................................................xxiii ■ Chapter 1: Getting Started with C++ on Android ...............................................................1 Microsoft Windows ........................................................................................................................2 Downloading and Installing the Java Development Kit on Windows .....................................................................2 Downloading and Installing the Apache ANT on Windows .....................................................................................6 Downloading and Installing the Android SDK on Windows ....................................................................................8 Downloading and Installing the Cygwin on Windows ..........................................................................................11 Downloading and Installing the Android NDK on Windows ..................................................................................14 Downloading and Installing the Eclipse on Windows ..........................................................................................16 Apple Mac OS X ...........................................................................................................................17 Installing Xcode on Mac ......................................................................................................................................18 Validating the Java Development Kit on Mac ......................................................................................................19 Validating the Apache ANT on Mac ......................................................................................................................19 Validating the GNU Make .....................................................................................................................................19 Downloading and Installing the Android SDK on Mac ..........................................................................................20 Downloading and Installing the Android NDK on Mac .........................................................................................22 Downloading and Installing the Eclipse on Mac ..................................................................................................23 vii viii Contents Ubuntu Linux ...............................................................................................................................25 Checking the GNU C Library Version ....................................................................................................................25 Enabling the 32-Bit Support on 64-Bit Systems ..................................................................................................25 Downloading and Installing the Java Development Kit on Linux .........................................................................26 Downloading and Installing the Apache ANT on Linux .........................................................................................26 Downloading and Installing the GNU Make on Linux ...........................................................................................27 Downloading and Installing the Android SDK on Linux ........................................................................................28 Downloading and Installing the Android NDK on Linux ........................................................................................29 Downloading and Installing the Eclipse on Linux ................................................................................................31 Downloading and Installing the ADT ............................................................................................32 Installing the Android Platform Packages ............................................................................................................35 Confi guring the Emulator .....................................................................................................................................36 Summary .....................................................................................................................................39 ■ Chapter 2: Exploring the Android NDK .............................................................................41 Components Provided with the Android NDK ..............................................................................41 Structure of the Android NDK ......................................................................................................42 Starting with an Example ............................................................................................................43 Specifying the Android NDK Location ..................................................................................................................43 Importing the Sample Project ..............................................................................................................................44 Adding Native Support to Project ........................................................................................................................47 Running the Project .............................................................................................................................................48 Building from the Command Line ........................................................................................................................49 Examining the Structure of an Android NDK Project ............................................................................................50 Build System ...............................................................................................................................51 Android.mk ..........................................................................................................................................................52 Application.mk .....................................................................................................................................................63 Using the NDK-Build Script .........................................................................................................64 Troubleshooting Build System Problems .....................................................................................65 Summary .....................................................................................................................................66 Contents ix ■ Chapter 3: Communicating with Native Code using JNI ..................................................67 What is JNI? ................................................................................................................................67 Starting with an Example ............................................................................................................67 Declaration of Native Methods ............................................................................................................................68 Loading the Shared Libraries ...............................................................................................................................69 Implementing the Native Methods .......................................................................................................................69 Data Types ...................................................................................................................................75 Primitive Types ....................................................................................................................................................75 Reference Types ..................................................................................................................................................76 Operations on Reference Types ...................................................................................................77 String Operations .................................................................................................................................................77 Array Operations ..................................................................................................................................................78 NIO Operations .....................................................................................................................................................80 Accessing Fields ..................................................................................................................................................81 Calling Methods ...................................................................................................................................................82 Field and Method Descriptors ..............................................................................................................................84 Exception Handling ......................................................................................................................88 Catching Exceptions ............................................................................................................................................88 Throwing Exceptions ...........................................................................................................................................89 Local and Global References .......................................................................................................89 Local References .................................................................................................................................................90 Global References ................................................................................................................................................90 Weak Global References ......................................................................................................................................91 Threading ....................................................................................................................................92 Synchronization ...................................................................................................................................................92 Native Threads .....................................................................................................................................................93 Summary .....................................................................................................................................93 ■ Chapter 4: Auto-Generate JNI Code Using SWIG ..............................................................95 What is SWIG? .............................................................................................................................95 Installation ...................................................................................................................................96 x Contents Installing on Windows ..........................................................................................................................................96 Installing on Mac OS X .........................................................................................................................................98 Installing on Ubuntu Linux ...................................................................................................................................99 Experimenting with SWIG Through an Example ........................................................................100 Interface File ......................................................................................................................................................101 Invoking SWIG from Command Line ..................................................................................................................104 Integrating SWIG into Android Build Process .....................................................................................................105 Updating the Activity ..........................................................................................................................................107 Executing the Application ..................................................................................................................................108 Exploring Generated Code .................................................................................................................................108 Wrapping C Code .......................................................................................................................109 Global Variables .................................................................................................................................................110 Constants ...........................................................................................................................................................110 Read-Only Variables ..........................................................................................................................................111 Enumerations.....................................................................................................................................................112 Structures ..........................................................................................................................................................115 Pointers .............................................................................................................................................................117 Wrapping C++ Code ..................................................................................................................117 Pointers, References, and Values ......................................................................................................................117 Default Arguments .............................................................................................................................................118 Overloaded Functions ........................................................................................................................................119 Classes ..............................................................................................................................................................120 Exception Handling ....................................................................................................................122 Memory Management ...............................................................................................................123 Calling Java from Native Code ..................................................................................................123 Asynchronous Communication ..........................................................................................................................123 Enabling Directors .............................................................................................................................................124 Enabling RTTI .....................................................................................................................................................125 Overriding the Callback Method ........................................................................................................................125 Updating the HelloJni Activity ............................................................................................................................125 Summary ...................................................................................................................................126

Description:
Android is one of the major players in the mobile phone market. Android is a mobile platform that is built on the top of Linux operating system. The native-code support on Android offers endless opportunities to application developers, not limited the functionality that is provided by Android framew
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.