Pro Android Apps Performance Optimization, Android

[ Pobierz całość w formacie PDF ]
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
orders-ny@springer-sbm.com
, or visit
www.springeronline.com
.
For information on translations, please e-mail
rights@apress.com
, 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
.
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

Chapter 2: Getting Started With the NDK ....................................................... 33

Chapter 3: Advanced NDK.............................................................................. 73

Chapter 4: Using Memory Efficiently ........................................................... 109

Chapter 5: Multithreading and Synchronization ......................................... 133

Chapter 6: Benchmarking And Profiling...................................................... 163

Chapter 7: Maximizing Battery Life ............................................................. 177

Chapter 8: Graphics..................................................................................... 207

Chapter 9: RenderScript .............................................................................. 231
Index ............................................................................................................... 265
iii
Contents
Contents at a Glance.......................................................................................... iii
About the Author.............................................................................................. viii
About the Technical Reviewers ......................................................................... ix
Acknowledgments .............................................................................................. x
Introduction ....................................................................................................... xi

Chapter 1: Optimizing Java Code .................................................................... 1
How Android Executes Your Code ........................................................................................................................... 2
Optimizing Fibonacci .............................................................................................................................................. 4
From Recursive To Iterative ............................................................................................................................... 5
BigInteger .......................................................................................................................................................... 6
Caching Results .................................................................................................................................................... 11
android.util.LruCache<K, V>............................................................................................................................ 12
API Levels ............................................................................................................................................................. 13
Fragmentation .................................................................................................................................................. 16
Data Structures ..................................................................................................................................................... 17
Responsiveness .................................................................................................................................................... 20
Lazy initializations ........................................................................................................................................... 22
StrictMode ....................................................................................................................................................... 23
SQLite ................................................................................................................................................................... 25
SQLite Statements ........................................................................................................................................... 25
Transactions .................................................................................................................................................... 28
Queries ............................................................................................................................................................. 30
Summary .............................................................................................................................................................. 31

Chapter 2: Getting Started With the NDK ....................................................... 33
What Is In the NDK? .............................................................................................................................................. 34
Mixing Java and C/C++ Code ............................................................................................................................... 37
Declaring the Native Method ........................................................................................................................... 37
Implementing the JNI Glue Layer..................................................................................................................... 38
Creating the Makefiles..................................................................................................................................... 40
Implementing the Native Function ................................................................................................................... 41
Compiling the Native Library............................................................................................................................ 43
Loading the Native Library ............................................................................................................................... 43
iv
[ Pobierz całość w formacie PDF ]