Android NDK Beginner's Guide Second Edition
Total Page:16
File Type:pdf, Size:1020Kb
[ 1 ] Android NDK Beginner's Guide Second Edition 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 Second Edition Copyright © 2015 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 Second Edition: April 2015 Production reference: 1240415 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78398-964-5 www.packtpub.com Credits Author Project Coordinator Sylvain Ratabouil Mary Alex Reviewers Proofreaders Guy Cole Simran Bhogal Krzysztof Fonał Safis Editing Sergey Kosarevsky Raimon Ràfols Indexer Monica Ajmera Mehta Commissioning Editor Ashwin Nair Graphics Disha Haria Acquisition Editor Vinay Argekar Production Coordinator Conidon Miranda Content Development Editor Rohit Singh Cover Work Conidon Miranda Technical Editor Ryan Kochery Copy Editors Hiral Bhat Adithi Shetty Sameen Siddiqui About the Author Sylvain Ratabouil is an IT consultant, experienced in Android, Java, and C/C++. He has contributed to the development of digital and mobile applications for large companies as well as industrial projects for the space and aeronautics industries. As a technology lover, he is passionate about mobile technologies and cannot live without his Android smartphone. About the Reviewers Guy Cole is a veteran Silicon Valley contractor with engagements in many well-known companies such as Facebook, Cisco, Motorola, Cray Research, Hewlett-Packard, Wells Fargo Bank, Barclays Global Investments, DHL Express, and many smaller, less-famous companies. You can contact him via LinkedIn for your next project. Krzysztof Fonał is passionate about computer science. He fell in love with this field when he was eleven. He strongly believes that technology doesn't matter; problem solving skills matters, as well as the passion to absorb knowledge. He currently works with Trapeze Group, which is a world leader in providing IT solutions. He plans to work with machine learning books and also on the Corona SDK. Sergey Kosarevsky is a software engineer with experience in C++ and 3D graphics. He worked for mobile industry companies and was involved in mobile projects at SPB Software, Yandex, and Layar. He has more than 12 years of software development experience and more than 6 years of Android NDK experience. Sergey earned his PhD in the field of mechanical engineering from St.Petersburg Institute of Machine-Building in Saint-Petersburg, Russia. He is a coauthor of Android NDK Game Development Cookbook. In his spare time, Sergey maintains and develops an open source multiplatform gaming engine, Linderdaum Engine (http://www.linderdaum.com), and a multi-platform open source file manager, WCM Commander (http://wcm.linderdaum.com). Raimon Ràfols has been developing for mobile devices since 2004. He has experience in developing on several technologies, specializing in UI, build systems, and client-server communications. He is currently working as a mobile software engineering manager at Imagination Technologies near London. In his spare time, he enjoys programming, photography, and giving talks at mobile conferences about Android performance optimization and Android custom views. I would like to express my gratitude to my beloved girlfriend, Laia, for her support and understanding. www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.PacktPub.com. 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 atwww.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. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read 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 a 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 9 entirely free books. Simply use your login credentials for immediate access. Table of Contents Preface vii Chapter 1: Setting Up Your Environment 1 Getting started with Android development 2 Setting up Windows 2 Time for action – preparing Windows for Android development 3 Installing Android development kits on Windows 8 Time for action – installing Android SDK and NDK on Windows 8 Setting up OS X 13 Time for action – preparing OS X for Android development 13 Installing Android development kits on OS X 16 Time for action – installing Android SDK and NDK on OS X 17 Setting up Linux 22 Time for action – preparing Ubuntu for Android development 23 Installing Android development kits on Linux 25 Time for action – installing Android SDK and NDK on Ubuntu 25 Installing the Eclipse IDE 30 Time for action – installing Eclipse with ADT on your OS 30 Setting up the Android emulator 35 Time for action – creating an Android virtual device 35 Developing with an Android device 39 Time for action – setting up an Android device 39 More about ADB 42 Summary 43 Chapter 2: Starting a Native Android Project 45 Building NDK sample applications 46 Time for action – compiling and deploying San Angeles sample 46 Generating project files with Android manager 49 Compiling native code with NDK-Build 51 [ i ] Table of Contents Building and packaging an application with Ant 52 Deploying an application package with Ant 52 Launching an application with ADB Shell 53 More about Android tooling 54 Creating your first native Android project 55 Time for action – creating a native Android project 55 Introducing Dalvik and ART 59 Interfacing Java with C/C++ 60 Time for action – calling C code from Java 61 Debugging native Android applications 64 Time for action – debugging a native Android application 64 Defining NDK application-wide settings 67 NDK-GDB day-to-day 68 Analyzing native crash dumps 69 Time for action – analyzing a native crash dump 69 Deciphering crash dumps 71 Setting up a Gradle project to compile native code 73 Time for action – creating a native Android project 74 Time for action – using your own Makefiles with Gradle 78 Summary 80 Chapter 3: Interfacing Java and C/C++ with JNI 81 Initializing a native JNI library 82 Time for action – defining a simple GUI 82 Time for action – initializing the native store 88 Converting Java strings in native code 91 Time for action – handling strings in the native store 91 Native character encoding 97 JNI String API 98 Passing Java primitives to native code 99 Time for action – handling primitives in the native store 99 Referencing Java objects from native code 103 Time for action – saving references to Objects in native Store 103 Local references 107 Global references 109 Weak references 110 Managing Java arrays 112 Time for action – handling Java arrays in native Store 112 Primitive arrays 121 Object arrays 124 Raising and checking Java exceptions 124 [ ii ] Table of Contents Time for action – raising & catching exceptions in native Store 125 Executing code in Exception state 128 Exception handling API 130 Summary 130 Chapter 4: Calling Java Back from Native Code 133 Calling Java back from native code 134 Time for action – determining JNI method signatures 134 Time for action – calling back Java from native code 138 More on the JNI Reflection API 142 Debugging JNI 143 Synchronizing Java and native threads 144 Time for action – allocating an object with JNI 144 Time for action – running and synchronizing a thread 151 Synchronizing Java and C/C++ with JNI Monitors 155 Attaching and detaching native threads 156 Processing bitmaps natively 157 Time for action – decoding a camera's feed 158 Time for action – processing pictures with the Bitmap API 165 Registering native methods manually 171 JNI in C versus JNI in C++ 171 Summary 172 Chapter 5: Writing a Fully Native Application 173 Creating a native Activity 174 Time for action – creating a basic native Activity 174 More about the Native App Glue 180 Handling Activity events 182 Time for action – stepping the event loop 182 Time for action – handling Activity events 187 Accessing window surface natively 193 Time for action – displaying