Pro Android Apps Performance Optimization ■ ■ ■ Hervé Guihot i Pro Android Apps Performance Optimization Copyright © 2012 by Hervé Guihot All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3999-4 ISBN-13 (electronic): 978-1-4302-4000-6 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 benefit of the trademark owner, with no intention of infringement of the trademark. The 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 affiliated with Google, Inc., and this book was written without endorsement from Google, Inc. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: James Markham Technical Reviewer: Charles Cruz, Shane Kirk, Eric Neff Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Morgan Ertel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editor: Corbin Collins Copy Editor: Jill Steinberg Compositor: MacPS, LLC Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 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. 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. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. 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 http://www.apress.com/source-code/. Contents at a Glance Contents ............................................................................................................. iv About the Author .............................................................................................. viii About the Technical Reviewers ......................................................................... ix Acknowledgments .............................................................................................. x Introduction ....................................................................................................... xi ■Chapter 1: Optimizing Java Code .................................................................... 1(cid:2) ■Chapter 2: Getting Started With the NDK ....................................................... 33(cid:2) ■Chapter 3: Advanced NDK .............................................................................. 73(cid:2) ■Chapter 4: Using Memory Efficiently ........................................................... 109(cid:2) ■Chapter 5: Multithreading and Synchronization ......................................... 133(cid:2) ■Chapter 6: Benchmarking And Profiling ...................................................... 163(cid:2) ■Chapter 7: Maximizing Battery Life ............................................................. 177(cid:2) ■Chapter 8: Graphics ..................................................................................... 207(cid:2) ■Chapter 9: RenderScript .............................................................................. 231(cid:2) Index ............................................................................................................... 265 iii Contents Contents at a Glance .......................................................................................... iii(cid:2) About the Author. ............................................................................................. viii(cid:2) About the Technical Reviewers ......................................................................... ix(cid:2) Acknowledgments .............................................................................................. x(cid:2) Introduction ....................................................................................................... xi(cid:2) ■Chapter 1: Optimizing Java Code .................................................................... 1(cid:2) (cid:2) How Android Executes Your Code ........................................................................................................................... 2 (cid:2) Optimizing Fibonacci .............................................................................................................................................. 4 (cid:2) From Recursive To Iterative ............................................................................................................................... 5 (cid:2) BigInteger .......................................................................................................................................................... 6 (cid:2) Caching Results .................................................................................................................................................... 11 (cid:2) android.util.LruCache<K, V> ............................................................................................................................ 12 (cid:2) API Levels ............................................................................................................................................................. 13 (cid:2) Fragmentation .................................................................................................................................................. 16 (cid:2) Data Structures ..................................................................................................................................................... 17 (cid:2) Responsiveness .................................................................................................................................................... 20 (cid:2) Lazy initializations ........................................................................................................................................... 22 (cid:2) StrictMode ....................................................................................................................................................... 23 (cid:2) SQLite ................................................................................................................................................................... 25 (cid:2) SQLite Statements ........................................................................................................................................... 25 (cid:2) Transactions .................................................................................................................................................... 28 (cid:2) Queries ............................................................................................................................................................. 30 (cid:2) Summary .............................................................................................................................................................. 31 ■Chapter 2: Getting Started With the NDK ....................................................... 33(cid:2) (cid:2) What Is In the NDK? .............................................................................................................................................. 34 (cid:2) Mixing Java and C/C++ Code ............................................................................................................................... 37 (cid:2) Declaring the Native Method ........................................................................................................................... 37 (cid:2) Implementing the JNI Glue Layer ..................................................................................................................... 38 (cid:2) Creating the Makefiles ..................................................................................................................................... 40 (cid:2) Implementing the Native Function ................................................................................................................... 41 (cid:2) Compiling the Native Library ............................................................................................................................ 43 (cid:2) Loading the Native Library ............................................................................................................................... 43 iv ■ CONTENTS (cid:2) Application.mk ...................................................................................................................................................... 44 (cid:2) Optimizing For (Almost) All Devices ................................................................................................................. 46 (cid:2) Supporting All Devices ..................................................................................................................................... 47 (cid:2) Android.mk ........................................................................................................................................................... 50 (cid:2) Performance Improvements With C/C++ .............................................................................................................. 53 (cid:2) More About JNI ................................................................................................................................................ 57 (cid:2) Native Activity ....................................................................................................................................................... 62 (cid:2) Building the Missing Library ............................................................................................................................ 64 (cid:2) Alternative ........................................................................................................................................................ 70 (cid:2) Summary .............................................................................................................................................................. 71 ■Chapter 3: Advanced NDK .............................................................................. 73(cid:2) (cid:2) Assembly .............................................................................................................................................................. 73 (cid:2) Greatest Common Divisor ................................................................................................................................ 74 (cid:2) Color Conversion .............................................................................................................................................. 79 (cid:2) Parallel Computation of Average ..................................................................................................................... 82 (cid:2) ARM Instructions .............................................................................................................................................. 87 (cid:2) ARM NEON ....................................................................................................................................................... 96 (cid:2) CPU Features ................................................................................................................................................... 97 (cid:2) C Extensions ......................................................................................................................................................... 98 (cid:2) Built-in Functions ............................................................................................................................................. 99 (cid:2) Vector Instructions ........................................................................................................................................... 99 (cid:2) Tips ..................................................................................................................................................................... 103 (cid:2) Inlining Functions ........................................................................................................................................... 104 (cid:2) Unrolling Loops .............................................................................................................................................. 104 (cid:2) Preloading Memory ........................................................................................................................................ 105 (cid:2) LDM/STM Instead Of LDR/STD ....................................................................................................................... 106 (cid:2) Summary ............................................................................................................................................................ 107 ■Chapter 4: Using Memory Efficiently ........................................................... 109(cid:2) (cid:2) A Word On Memory ............................................................................................................................................. 109 (cid:2) Data Types .......................................................................................................................................................... 111 (cid:2) Comparing Values .......................................................................................................................................... 113 (cid:2) Other Algorithms ............................................................................................................................................ 115 (cid:2) Sorting Arrays ................................................................................................................................................ 116 (cid:2) Defining Your Own Classes ............................................................................................................................ 117 (cid:2) Accessing Memory .............................................................................................................................................. 118 (cid:2) The Cache’s Line Size .................................................................................................................................... 119 (cid:2) Laying Out Your Data .......................................................................................................................................... 120 (cid:2) Garbage Collection .............................................................................................................................................. 125 (cid:2) Memory Leaks ............................................................................................................................................... 125 (cid:2) References ..................................................................................................................................................... 127 (cid:2) APIs ..................................................................................................................................................................... 131 (cid:2) Low Memory ....................................................................................................................................................... 131 (cid:2) Summary ............................................................................................................................................................ 132 ■Chapter 5: Multithreading and Synchronization ......................................... 133(cid:2) (cid:2) Threads ............................................................................................................................................................... 134 (cid:2) AsyncTask ........................................................................................................................................................... 137 (cid:2) Handlers and Loopers ......................................................................................................................................... 140 v ■ CONTENTS (cid:2) Handlers ......................................................................................................................................................... 140 (cid:2) Loopers .......................................................................................................................................................... 142 (cid:2) Data Types .......................................................................................................................................................... 143 (cid:2) Synchronized, Volatile, Memory Model .......................................................................................................... 143 (cid:2) Concurrency ........................................................................................................................................................ 147 (cid:2) Multicore ............................................................................................................................................................. 148 (cid:2) Modifying Algorithm For Multicore ................................................................................................................ 149 (cid:2) Using Concurrent Cache ................................................................................................................................ 152 (cid:2) Activity Lifecycle ................................................................................................................................................. 154 (cid:2) Passing Information ....................................................................................................................................... 156 (cid:2) Remembering State ....................................................................................................................................... 158 (cid:2) Summary ............................................................................................................................................................ 161 ■Chapter 6: Benchmarking And Profiling ...................................................... 163(cid:2) (cid:2) Measuring Time .................................................................................................................................................. 163 (cid:2) System.nanoTime() ........................................................................................................................................ 164 (cid:2) Debug.threadCpuTimeNanos() ....................................................................................................................... 165 (cid:2) Tracing ................................................................................................................................................................ 167 (cid:2) Debug.startMethodTracing() .......................................................................................................................... 167 (cid:2) Using the Traceview Tool ............................................................................................................................... 168 (cid:2) Traceview in DDMS ........................................................................................................................................ 170 (cid:2) Native Tracing ................................................................................................................................................ 172 (cid:2) Logging ............................................................................................................................................................... 174 (cid:2) Summary ............................................................................................................................................................ 176 ■Chapter 7: Maximizing Battery Life ............................................................. 177(cid:2) (cid:2) Batteries ............................................................................................................................................................. 177 (cid:2) Measuring Battery Usage ............................................................................................................................... 180 (cid:2) Disabling Broadcast Receivers ........................................................................................................................... 183 (cid:2) Disabling and Enabling the Broadcast Receiver ............................................................................................ 186 (cid:2) Networking ......................................................................................................................................................... 187 (cid:2) Background Data ........................................................................................................................................... 188 (cid:2) Data Transfer ................................................................................................................................................. 189 (cid:2) Location .............................................................................................................................................................. 191 (cid:2) Unregistering a Listener ................................................................................................................................ 193 (cid:2) Frequency of Updates .................................................................................................................................... 193 (cid:2) Multiple Providers .......................................................................................................................................... 194 (cid:2) Filtering Providers .......................................................................................................................................... 196 (cid:2) Last Known Location ...................................................................................................................................... 198 (cid:2) Sensors ............................................................................................................................................................... 199 (cid:2) Graphics .............................................................................................................................................................. 200 (cid:2) Alarms ................................................................................................................................................................. 201 (cid:2) Scheduling Alarms ......................................................................................................................................... 203 (cid:2) WakeLocks .......................................................................................................................................................... 204 (cid:2) Preventing Issues ........................................................................................................................................... 205 (cid:2) Summary ............................................................................................................................................................ 206 ■Chapter 8: Graphics ..................................................................................... 207(cid:2) (cid:2) Optimizing Layouts ............................................................................................................................................. 207 (cid:2) RelativeLayout ............................................................................................................................................... 209 vi ■ CONTENTS (cid:2) Merging Layouts ............................................................................................................................................ 213 (cid:2) Reusing Layouts ............................................................................................................................................. 214 (cid:2) View Stubs ..................................................................................................................................................... 215 (cid:2) Layout Tools ........................................................................................................................................................ 217 (cid:2) Hierarchy Viewer ............................................................................................................................................ 218 (cid:2) layoutopt ........................................................................................................................................................ 218 (cid:2) OpenGL ES .......................................................................................................................................................... 218 (cid:2) Extensions ...................................................................................................................................................... 219 (cid:2) Texture Compression ..................................................................................................................................... 221 (cid:2) Mipmaps ........................................................................................................................................................ 226 (cid:2) Multiple APKs ................................................................................................................................................. 228 (cid:2) Shaders .......................................................................................................................................................... 228 (cid:2) Scene Complexity .......................................................................................................................................... 229 (cid:2) Culling ............................................................................................................................................................ 229 (cid:2) Render Mode .................................................................................................................................................. 229 (cid:2) Power Consumption ....................................................................................................................................... 229 (cid:2) Summary ............................................................................................................................................................ 230 ■Chapter 9: RenderScript .............................................................................. 231(cid:2) (cid:2) Overview ............................................................................................................................................................. 231 (cid:2) Hello World .......................................................................................................................................................... 233 (cid:2) Hello Rendering .................................................................................................................................................. 236 (cid:2) Creating a Rendering Script ........................................................................................................................... 237 (cid:2) Creating a RenderScriptGL Context ............................................................................................................... 238 (cid:2) Extending RSSurfaceView .............................................................................................................................. 239 (cid:2) Setting the Content View ............................................................................................................................... 239 (cid:2) Adding Variables to Script .................................................................................................................................. 240 (cid:2) HelloCompute ...................................................................................................................................................... 243 (cid:2) Allocations ..................................................................................................................................................... 244 (cid:2) rsForEach ....................................................................................................................................................... 245 (cid:2) Performance .................................................................................................................................................. 248 (cid:2) Native RenderScript APIs .................................................................................................................................... 249 (cid:2) rs_types.rsh ................................................................................................................................................... 250 (cid:2) rs_core.rsh ..................................................................................................................................................... 253 (cid:2) rs_cl.rsh ......................................................................................................................................................... 255 (cid:2) rs_math.rsh ................................................................................................................................................... 259 (cid:2) rs_graphics.rsh .............................................................................................................................................. 260 (cid:2) rs_time.rsh .................................................................................................................................................... 261 (cid:2) rs_atomic.rsh ................................................................................................................................................. 262 RenderScript vs. NDK .......................................................................................................................................... 263 (cid:2) Summary ............................................................................................................................................................ 263 Index ............................................................................................................... 265 vii About the Author Hervé Guihot started learning about computers more than 20 years ago with an Amstrad CPC464. Although the CPC464 is most likely the reason why he still appreciates green-screened devices (ask him about his phone), Hervé started working with Android as it became a popular platform for application development. It was also was the only platform that combined two of his main passions: software and pastries. After many years working in the world of interactive and digital television, he is focused on bringing Android to more devices to encourage more developers to leverage the power of Android and more people to have access to the technology. Hervé is currently a software engineering manager in MediaTek (www.mediatek.com), a leading fabless semiconductor company for wireless communications and digital multimedia solutions. He holds an engineering degree from the Institut de Formation Supérieure en Informatique et Télécommunication in Rennes, Brittany, and you can sometimes find him waiting in line for an éclair on 18th and Guerrero. viii About the Technical Reviewers Charles Cruz is a mobile application developer for the Android, iOS, and Windows Phone platforms. He graduated from Stanford University with B.S. and M.S. degrees in Engineering. He lives in Southern California and, when not doing technical things, plays lead guitar in an original metal band (www.taintedsociety.com) and a classic rock tribute band. Charles can be reached at [email protected] and @CodingNPicking on Twitter. Shane Kirk earned his B.S. in Computer Science from the University of Kentucky in 2000. He’s currently a software engineer for DeLorme, a mapping and GPS technology company based in Yarmouth, Maine, where he spends his days writing C++ and Java code for mobile and desktop applications. When Shane isn’t coding, you’ll usually find him making lots of noise with his guitar or lost in the pages of a good book. Eric Neff is an experienced technical architect with more than 14 years of overall experience in as a technical architect and senior software developer. He is an expert in full life-cycle application development, middle-ware, and n-tier application development, with specific expertise in Microsoft .NET application development. He specializes in object-oriented analysis and design in systems development with a focus on the scheduling of service personal or manufactured items and was instrumental in the design and implementation of data relation schemas for the lexicography industry. Eric was recently promoted to Director of Mobile Innovations at Kiefer Consulting, Inc, putting into practice several years of hobbyist development in the mobile space on iPhone, Android, and Windows Mobile. Eric is active in the local development community through his participation in the Sacramento Google Technology Group and as a board member of the Sacramento Dot Net User Group. He has given presentations on mobile web technologies, mobile development, and ASP.NET techniques. ix
Description: