™ Android Programming Pushing the Limits Erik Hellman This edition first published 2014 © 2014 Erik Hellman Registered office John Wiley & Sons Ltd, The Atrium, Southern Gate, Chichester, West Sussex, PO19 8SQ, United Kingdom For details of our global editorial offices, for customer services and for information about how to apply for permission to reuse the copyright material in this book please see our website at www.wiley.com. The right of the author to be identified as the author of this work has been asserted in accordance with the Copyright, Designs and Patents Act 1988. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording or otherwise, except as permitted by the UK Copyright, Designs and Patents Act 1988, without the prior permission of the publisher. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. This publication is designed to provide accurate and authoritative information in regard to the subject matter covered. It is sold on the understanding that the publisher is not engaged in rendering professional services. If professional advice or other expert assistance is required, the services of a competent professional should be sought. Trademarks: Wiley and the Wiley logo are trademarks or registered trademarks of John Wiley and Sons, Inc. and/ or its affiliates in the United States and/or other countries, and may not be used without written permission. Android is a trademark of Google, Inc. All other trademarks are the property of their respective owners. John Wiley & Sons, Ltd. is not associated with any product or vendor mentioned in the book. A catalogue record for this book is available from the British Library. ISBN 978-1-118-71737-0 (paperback); ISBN 978-1-118-71730-1 (ePDF); 978-1-118-71735-6 (ePub) Set in 9.5 pt Myriad Pro by Indianapolis Composition Services Printed in the United States by Bind-Rite Dedication This book is dedicated to my amazing dad, Ingemar Hellman, who taught me programming when I was nine years old. None of this would have been possible if it weren’t for all the amazing things he taught me. Erik Hellman, Second generation software developer Publisher’s Acknowledgements Some of the people who helped bring this book to market include the following: Editorial and Production VP Consumer and Technology Publishing Director: Michelle Leete Associate Director–Book Content Management: Martin Tribe Associate Publisher: Chris Webb Project Editor: M. Faunette Johnston Copy Editor: Melba Hopper Technical Editor: Erik Westenius Editorial Manager: Jodi Jensen Senior Project Editor: Sara Shlaer Editorial Assistant: Annie Sullivan Marketing Associate Marketing Director: Louise Breinholt Marketing Manager: Lorna Mein Senior Marketing Executive: Kate Parrett Marketing Assistant: Polly Thomas Composition Services Compositor: Jennifer Goldsmith, Andrea Hornberger Proofreader: Wordsmith Editorial Indexer: Potomac Indexing, LLC About the Author Erik Hellman grew up in the small town of Borlänge in the middle of Sweden and currently lives in the city of Malmö with his wife. From an early age he showed a great interest in all kinds of technology, much thanks to his father. This interest has carried on over the years and he likes to spend time tinkering with all kinds of technology, be it hardware or software. Erik currently works as a developer consultant at Factor10. He previously worked for Sony Mobile as one of the lead architects for their Android development and later as a research engineer where he participated in the development of the next generation of mobile solutions from Sony. Erik has a lifetime of experience with software development and programming. His father taught him how to write code in Turbo Pascal at the age of nine, as well as how to design relational databases in Oracle 3 on his old IBM AT 286. Much later, Erik studied Software Engineering at Blekinge Institute of Technology where he also worked as an assistant teacher in various software engineering courses. Erik can often be found at various developer conferences where he likes to speak about cutting-edge software technologies. You can also find out more about Erik and his other hobbies at his blog (http://blog.hellsoft.se) or follow him on Google+ (http://gplus.to/ErikHellman) or Facebook (https://www.facebook.com/ErikHellman). Acknowledgements First of all, I’d like to thank my wife Kaisa-Leena. Without her love, support, and infinite patience with me during the writing of this book, this book would never have been finished. A big thank you goes out to all my friends and former colleagues at Sony Mobile. Almost everything I’ve learned about Android and mobile technologies came from my years working with all of you. I’m extremely proud to have been part of the team that built the Xperia series of devices. A special thank you to my former boss, Henrik Bengtsson, and his team at the Sony Mobile Research in Lund. Finally, I’d like to thank my editors; Faunette, Melba, Erik, and all the people working at Wiley for helping me write this book. A big thanks goes to my technical editor Erik Westenius for his eye for details and making sure that my code and examples are understandable. Also, a thank you to Kristoffer Åberg at Sony Mobile for giving me valuable input on the UI design sections. Contents Introduction................................................................................................................................1 Who This Book Is For .............................................................1 What This Book Covers ...........................................................2 How This Book Is Structured ......................................................2 What You Need to Use This Book .................................................5 Source Code .....................................................................5 Errata ............................................................................5 Part I Building a Better Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Chapter 1 Fine-Tuning Your Development Environment .....................................9 Operating Systems for Android Development .....................................9 Advanced Android SDK Tools .....................................................9 The adb Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Stress-Testing an Application’s UI with Monkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 The Gradle Build System for Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Optimize and Obfuscate with ProGuard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Android Library Projects and Third-Party Libraries ................................17 Precompiled JAR Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Setting Up a Library Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Version Control and Source Code Management ..................................19 Mastering Your IDE ..............................................................22 Debugging Android Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Static Code Analysis with lint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 Refactoring Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Developer Options on Android Devices ..........................................29 Understanding Developer Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Summary .......................................................................32 Further Resources ...............................................................32 Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 viii Contents Chapter 2 Efficient Java Code for Android ...............................................................33 Comparing Android’s Dalvik Java to Java SE ......................................33 Optimizing Java Code for Android ...............................................36 Type-Safe Enum on Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Enhanced For-Loop on Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 Queues, Synchronization, and Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Memory Management and Allocations ...........................................40 Reducing Object Allocations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Multithreading on Android ......................................................44 Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 AsyncTask . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Picking the Right Solution for Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Summary .......................................................................52 Further Resources ...............................................................53 Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Online Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Part II Getting the Most Out of Components . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Chapter 3 Components, Manifests, and Resources..............................................57 Android Components ...........................................................57 The Activity Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 The Service Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 The BroadcastReceiver Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 The ContentProvider Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 The Application Component . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Application Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 The Android Application Manifest ...............................................62 The Manifest Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Google Play Filters and Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 The Application Element . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 Component Elements and Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Intent Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 Resources and Assets. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .69 Advanced String Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 Localization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 Using Resource Qualifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Using Assets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Description: