Android High Performance Programming
()
About this ebook
- Wide coverage of various topics that help in developing optimal applications
- Explore the concepts of Advanced Native Coding in depth
- A must-have for professional-standard Android developers for whom performance failures and the sloppy use of resources are simply unacceptable
This book is aimed at developers with an advanced knowledge of Android who want to test their skills and learn new techniques to increase the performance of their applications. We assume they are comfortable working with the entire Android SDK and have been doing it for a few years. They need to be familiar with frameworks such as NDK to use native code, which is crucial for app performance.
Related to Android High Performance Programming
Related ebooks
Android Application Development with Maven Rating: 0 out of 5 stars0 ratingsAndroid Studio Cookbook Rating: 4 out of 5 stars4/5Learning iBeacon Rating: 0 out of 5 stars0 ratingsLearning Swift - Second Edition Rating: 0 out of 5 stars0 ratingsUnity 5 for Android Essentials Rating: 4 out of 5 stars4/5Asynchronous Android Programming - Second Edition Rating: 0 out of 5 stars0 ratingsLearning Android Application Testing Rating: 0 out of 5 stars0 ratingsAndroid Application Security Essentials Rating: 0 out of 5 stars0 ratingsLearning Embedded Android N Programming Rating: 0 out of 5 stars0 ratingsAndroid Application Programming with OpenCV Rating: 3 out of 5 stars3/5Mastering Android Game Development Rating: 0 out of 5 stars0 ratingsLearning Android Forensics Rating: 4 out of 5 stars4/5Learning Penetration Testing with Python Rating: 0 out of 5 stars0 ratingsPractical Django 2 and Channels 2: Building Projects and Applications with Real-Time Capabilities Rating: 0 out of 5 stars0 ratingsAsynchronous Android Rating: 4 out of 5 stars4/5Learning Android Application Development Rating: 0 out of 5 stars0 ratingsApplication Development with Qt Creator - Second Edition Rating: 4 out of 5 stars4/5Mastering OpenCV Android Application Programming Rating: 0 out of 5 stars0 ratingsMastering Android Application Development Rating: 5 out of 5 stars5/5JavaScript Mobile Application Development Rating: 0 out of 5 stars0 ratingsAndroid Things Projects Rating: 0 out of 5 stars0 ratingsMastering Android Wear Application Development Rating: 0 out of 5 stars0 ratingsIntelliJ IDEA Essentials Rating: 0 out of 5 stars0 ratingsOpenCV Android Programming By Example Rating: 0 out of 5 stars0 ratingsExploring SE for Android Rating: 0 out of 5 stars0 ratingsAndroid NDK: Beginner's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsSwift 2 Design Patterns Rating: 0 out of 5 stars0 ratingsLearning AirWatch Rating: 5 out of 5 stars5/5PHP Programming Solutions Rating: 0 out of 5 stars0 ratingsFlash Development for Android Cookbook Rating: 3 out of 5 stars3/5
Hardware For You
50 Android Hacks Rating: 5 out of 5 stars5/5Mastering ChatGPT Rating: 0 out of 5 stars0 ratingsFitbit For Dummies Rating: 0 out of 5 stars0 ratingsSamsung Galaxy S23 Ultra User Guide for Beginners and Seniors Rating: 3 out of 5 stars3/5Computer Science: A Concise Introduction Rating: 4 out of 5 stars4/5iPhone For Seniors For Dummies: Updated for iPhone 12 models and iOS 14 Rating: 4 out of 5 stars4/5iPhone 14 Pro Max User Guide for Beginners and Seniors Rating: 0 out of 5 stars0 ratingsiPhone Photography: A Ridiculously Simple Guide To Taking Photos With Your iPhone Rating: 0 out of 5 stars0 ratingsCompTIA A+ Complete Review Guide: Exam Core 1 220-1001 and Exam Core 2 220-1002 Rating: 5 out of 5 stars5/5CompTIA A+ Complete Review Guide: Core 1 Exam 220-1101 and Core 2 Exam 220-1102 Rating: 5 out of 5 stars5/5Exploring Apple iPad: iPadOS 15 Edition: The Illustrated, Practical Guide to Using your iPad Rating: 0 out of 5 stars0 ratingsDancing with Qubits: How quantum computing works and how it can change the world Rating: 5 out of 5 stars5/5Windows 11 For Seniors For Dummies Rating: 0 out of 5 stars0 ratingsiPhone X Hacks, Tips and Tricks: Discover 101 Awesome Tips and Tricks for iPhone XS, XS Max and iPhone X Rating: 3 out of 5 stars3/5Upgrading and Fixing Computers Do-it-Yourself For Dummies Rating: 4 out of 5 stars4/5Build Your Own PC Do-It-Yourself For Dummies Rating: 4 out of 5 stars4/5Raspberry Pi Electronics Projects for the Evil Genius Rating: 3 out of 5 stars3/5iPod and iTunes For Dummies Rating: 4 out of 5 stars4/5Embedded Systems: World Class Designs Rating: 5 out of 5 stars5/5Programming Arduino: Getting Started with Sketches Rating: 4 out of 5 stars4/5Macs All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsLinux All-in-One For Dummies Rating: 3 out of 5 stars3/5So you want to build a computer... Rating: 5 out of 5 stars5/5Computer Organization and Design: The Hardware / Software Interface Rating: 4 out of 5 stars4/5Creative Selection: Inside Apple's Design Process During the Golden Age of Steve Jobs Rating: 5 out of 5 stars5/5iPhone 12, iPhone Pro, and iPhone Pro Max For Senirs: A Ridiculously Simple Guide to the Next Generation of iPhone and iOS 14 Rating: 0 out of 5 stars0 ratingsComputers For Seniors For Dummies Rating: 0 out of 5 stars0 ratingsExploring Apple Mac - Ventura Edition: The Illustrated, Practical Guide to Using MacOS Rating: 0 out of 5 stars0 ratings
Reviews for Android High Performance Programming
0 ratings0 reviews
Book preview
Android High Performance Programming - Diego Grancini
Table of Contents
Android High Performance Programming
Credits
About the Authors
About the Reviewer
www.PacktPub.com
eBooks, discount offers, and more
Why subscribe?
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Introduction: Why High Performance?
Why does the performance of an application mean so much to so many?
Manual testing and automatic testing
ANR and delays in software
What triggers ANRs and how can I avoid them?
Android architecture
Dalvik Virtual Machine
Memory management
Energy consumption
Java language
Native Development Kit or how to develop with native code when needed
Three limits in application responsiveness
Business value of software quality
Summary
2. Efficient Debugging
Android Debug Bridge
Dalvik Debug Monitor Server
Capturing and analyzing thread information
Heap analysis and visualization
Allocation tracker
Network usage
Emulator Control
System status
Debugging the UI
Profiling with Hierarchy Viewer
Systrace
Android device debug options
Android Instant Run
GPU profiler
Running a trace
ClassyShark
Getting started
Summary
3. Building Layouts
Walkthrough
Rendering performance
Screen tearing and VSYNC
Hardware acceleration
Overdraw
Multi-window mode
Overview
Configuration
Management
Drag and drop
Performance impact
Best practices
Provided layout overview
Hierarchical layout management
Reusing layouts
ViewStub
AdapterViews and view recycling
The ViewHolder pattern
Custom views and layouts
Screen zoom
Debugging tools
The Design view
Hierarchy Viewer
Tree View
View properties
Tree overview
Layout View
On device tools
Debugging GPU overdraw
Profile GPU rendering
Systrace
Summary
4. Memory
Walkthrough
How memory works
Garbage collection
Shared memory
Runtime
Android N JIT compiler
Memory leak
Memory churn
References
Memory-side projects
Project Svelte and Android N
Best practices
Data types
Autoboxing
Sparse array family
ArrayMap
Syntax
Collections
Enumerations
Constants
Object management
Strings
String concatenation
Local variables
Arrays versus collections
Streams
Memory patterns
The object pool pattern
The FlyWeight pattern
Android component leaks
Activities
Static fields
Non-static inner classes
Singletons
Anonymous inner classes
Handlers
Services
Processes
The memory API
Main components and memory management
Debugging tools
LogCat
Dalvik
ART
The ActivityManager API
StrictMode
Dumpsys
Meminfo
ProcStats
Summary
5. Multithreading
Walkthrough
Threading basics
Multicore CPUs
Threads
Multithreaded applications
Thread safety
Android multithreading environment
Processes
Android application thread
The UI thread
Worker threads
The binder thread
Android thread messaging
Best practices
Threads
HandlerThread
When to use
AsyncTask
Methods
Generics parameters
States management
Executor
When to use
Loaders
LoaderManager
LoaderCallbacks
Provided loaders
AsyncTaskLoader
CursorLoader
When to use
Services
Life cycle
Started Service
When to use
Bound Service
When to use
IntentService
When to use
Inter-process communication
Remote Procedure Call
AIDL
Messenger
Advanced techniques
BroadcastReceiver asynchronous techniques
ContentProvider asynchronous techniques
Repeating tasks
Timer
ScheduledExecutorService
AlarmManager
Debugging tools
StrictMode
Summary
6. Networking
Walkthrough
Protocols
Methods
Headers
Timeout
Content
Compression
Response code
Connection types
Best practices
Latency gauging
Batching connections
Prefetching
Queuing connections
Caching responses
Cache control
Last-Modified
If-Modified-Since
Exponential back-off
Polling versus pushing
Provided APIs
SyncManager
Authenticator
SyncAdapter
Android N changes
Data Saver
Background optimization
GcmNetworkManager
The service implementation
Task scheduling
Task features
Task
OneoffTask
PeriodicTask
Debugging tools
Android N Networking ADB tool
Fiddler
Wireshark
Application Resource Optimizer
Network attenuation
Speed and delay emulation
Fiddler
Network Link Conditioner
Network Attenuator
Summary
7. Security
WhatsApp – the eternal showcase of no-gos
Going deeper into the code
Capturing an APK file
Pulling a file from the device
Capturing an APK using Wireshark
Using external websites
Autopsy of an APK file
Code injection
Opcodes
Injecting new code
Signing and rebuilding the application
Protecting our application
Insecure storage
SharedPreferences
InternalStorage
ExternalStorage
Deleting files
Using external or internal storage
Databases
Performance in databases
SQL injections
ORM frameworks
OrmLite
SugarORM
GreenDAO
Realm
Network
Encrypted communication
Sniffing
Summary
8. Optimizing Battery Consumption
Analysis
Monitoring battery level and charging status
How to identify changes in the charging status
Determining and reacting to changes in the battery level
Doze feature and App Standby
Understanding Doze
Avoiding useless network requests
Dealing with BroadcastReceivers on demand
Networking
Summary
9. Native Coding in Android
Getting started – setting up NDK in our system
JNI
Initial concepts
Creating our first HelloWorld-JNI
Creating a native activity with Android NDK
Debugging NDK
Android.mk
More variables in NDK
TARGET_PLATFORM
TARGET_ARCH
TARGET_ABI
NDK macros
Application.mk
Including existing libraries
Exporting header files
Summary
10. Performance Tips
Bitmaps
Loading
Processing
Caching
Displaying
Managing memory
Image optimization
Resolution
Compression
Serialization
JSON improvements
JSON alternatives
Protocol buffers
Flat buffers
Local serialization
Code improvements
Getters and setters
Inner classes
Java 8 in Android N
Setup
Features
Default interface methods
Static interface methods
Lambda expression
Repeating annotations
Jack toolchain
APK optimizations
Removing unused code
Removing unused resources
Summary
Index
Android High Performance Programming
Android High Performance Programming
Copyright © 2016 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 authors, 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: August 2016
Production reference: 1240816
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-895-1
www.packtpub.com
Credits
Authors
Enrique López Mañas
Diego Grancini
Reviewer
Emil Stefanov Atanasov
Commissioning Editor
Edward Gordon
Acquisition Editor
Larissa Pinto
Content Development Editor
Parshva Sheth
Technical Editor
Prajakta Mhatre
Copy Editor
Safis Editing
Project Coordinator
Ritika Manoj
Proofreader
Safis Editing
Indexer
Hemangini Bari
Graphics
Kirk D'Penha
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
About the Authors
Enrique López Mañas is a Google Developer Expert and independent IT consultant. He has been working with mobile technologies and learning from them since 2007. He is an avid contributor to the open source community and a FLOSS (Free Libre Open Source Software) kind of guy, being among the top 10 open source Java contributors in Germany. He is a part of the Google LaunchPad accelerator, where he participates in Google global initiatives to influence hundreds of the best startups from all around the globe. He is also a big data and machine learning aficionado.
In his free time he rides his bike, take pictures, and travels until exhaustion. He also writes literature and enjoys all kinds of arts. He likes to write about himself in third person. You can follow him on Twitter (@eenriquelopez) to stay updated on his latest movements.
Diego Grancini has a degree in telecommunications and IT engineering from Perugia University. He has developed his skills on Android development for more than six years leading and contributed to several projects, teaching and sharing his skills during his career.
He joined Engineering Ingegneria Informatica S.P.A. after his degree, defining his own knowledge about Java and Android development working as the lead Android developer for years. Then he joined J.P. Morgan & Chase, strengthening his skills about security and performance in software development and Android platform in particular.
I would like to express my gratitude to Murvin Bhantooa, for helping me with content, Gil McErlane and all of my colleagues in J.P. Morgan & Chase for supporting, and Gianluca Polegri and all of my ex-colleagues at Engineering Ingegneria Informatica S.P.A. for laying the foundations of my knowledge.
Thanks to my father Augusto for guiding and forming me, my mother Argia Flavia, my sister Agostina, the rest of my family and Utah Capo for supporting me.
Special thanks to Helen McKenna for building my language skills.
I would like to thank Enrique López Mañas, Parshva Sheth, and Emil Atanasov for the great job they did in the development of this book.
Last but not least, I ask forgiveness for having failed to mention people who were with me during my journey.
About the Reviewer
Emil Atanasov is an IT consultant with broad experience in mobile technologies. He has been exploring the field of mobile development since 2006.
Emil has a MSc in media informatics from RWTH Aachen University, Germany, and a MSc in computer science from Sofia University St. Kliment Ohridsky,
Bulgaria. He has worked for several huge USA companies and has been a freelancer for several years. Emil has experience in software design and development. He was involved in the process of redesigning, improving, and creating a number of mobile apps. Currently, he is focused on the rapidly growing mobile sector and manages a great team of developers that provides software solutions to clients around the world. He is a co-founder of ApposeStudio Inc.
As an Android team leader and project manager, Emil was leading a team that was developing a part of the Nook Color firmware—an e-magazine/e-book reader, which supports the proprietary Barnes & Nobel and some other e-book formats.
He is one of the people behind the Getting Started with Flurry Analytics book. He also contributed heavily to the book Objective-C Memory Management Essentials and Android Application Development Cookbook, Second Edition.
I want to thank my family and friends for being so cool. Thank you for supporting me even though I'm such a bizarre geeky person, who spends most of the time in the digital world. Thank you, guys!
www.PacktPub.com
eBooks, discount offers, and more
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 at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
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.
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
Preface
Performant applications are one of the key drivers of success in the mobile world. Users may abandon an app if it runs slowly. Learning how to build applications that balance speed and performance with functionality and UX can be a challenge; however, it's now more important than ever to get that balance right.
Android High Performance Programming will make you think about how to wring the most from any hardware your app is installed on, so you can increase your reach and engagement. The book begins by providing an introduction to state-of-the-art Android techniques and the importance of performance in an Android application. Then, we will explain the Android SDK tools regularly used to debug and profile Android applications. We will also learn about some advanced topics such as building layouts, multithreading, networking, and security. Battery life is one of the biggest bottlenecks in applications; this book will show typical examples of code that exhausts battery life, how to prevent this, and how to measure battery consumption from an application in every kind of situation.
This book explains techniques for building optimized and efficient systems that do not drain the battery, cause memory leaks, or slow down with time.
What this book covers
Chapter 1, Introduction: Why High Performance?, provides an introduction to the topic, the current state of the art in Android, and the importance of performance in an Android application.
Chapter 2, Efficient Debugging, covers the tools provided by the Android SDK (and some externals) that are regularly used to debug and profile Android applications.
Chapter 3, Building Layouts, will take you through the techniques used to optimize Android routines, write applications that use memory efficiently, and explain concepts from memory allocation to garbage collection.
Chapter 4, Memory, provides many insights to the UI design that need to be learnt in order to create an efficient UI that loads fast, without giving a lag perception to the user and that gets updated efficiently.
Chapter 5, Multithreading, explains all the different threading options in an Android application and when we should apply each. Some advanced techniques, such as IPC, will be also shown with practical code.
Chapter 6, Networking, shows techniques used to perform efficient network operations, and techniques to retrieve data from servers such as exponential back offs or avoiding polling.
Chapter 7, Security, covers techniques to secure an Android application, how to make use of the security encryption mechanisms provided by Android natively, and how to get information about connections or just be notified of connection changes.
Chapter 8, Optimizing Battery Consumption, provides typical examples of code that exhausts battery life, how to prevent it, and how to measure battery consumption from an application in every kind of situation; many developers don't know how to behave while developing apps to take a photo or a video, handling with previews, and saving data.
Chapter 9, Native Coding in Android, this chapter is a workaround in the world of native code and C++ in Android and its usage.
Chapter 10, Performance Tips, helps developer to be guided in common coding situations, where wrong choices can compromise app efficiency; this will be a best practice guide related to topics not dealt with in previous chapters.
What you need for this book
You will need the following hardware for this book:
PC/laptop running Windows, Linux, or Mac OS X
Android phone. A high-end model is recommended, with at least Android 5.0 installed.
Who this book is for
This topic is aimed at developers with advanced knowledge of Android, who want to push their knowledge and learn techniques to increase the performance of their applications. We assume they are comfortable working with the entire Android SDK, and have been doing it for years. They are familiar also with frameworks such as the NDK to use native code, which is crucial to performance.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: If you go to this folder and call the command adb, you will see on the screen a list of the available options.
A block of code is set as follows:
Any command-line input or output is written as follows:
adb shell dumbsys gfxinfo
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: To debug the overdraw on the device, Android provides a helpful tool that can be enabled inside the Developer options.
Note
Warnings or important notes appear in a box like this.
Tip
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <feedback@packtpub.com>, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
You can also download the code files by clicking on the Code Files button on the book's webpage at the Packt Publishing website. This page can be accessed by entering the book's name in the Search box. Please note that you need to be logged in to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Android-High-Performance-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Downloading the color images of this book
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from http://www.packtpub.com/sites/default/files/downloads/AndroidHighPerformanceProgramming_ColorImages.pdf.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <copyright@packtpub.com> with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at <questions@packtpub.com>, and we will do our best to address the problem.
Chapter 1. Introduction: Why High Performance?
According to the Cambridge dictionary, one of the acceptations of performance is How well a person, machine, etc. does a piece of work or an activity.
If we put it together with high
we can define it as the output or efficiency with which a task is being done.
High performance in software refers to the strategies that developers adopt to create pieces of software that can perform a process efficiently. When we are developing mobile software, this affects, but is not limited to, layout development, energy and battery management, security concerns, efficient multithreading, programming patterns, and debugging techniques.
There is a big difference between doing things, and doing things right. In a real world with deadlines, budgets, and managers, software engineers fall very often into acquiring technical debt. A technical debt is bought
(if we can use that verb) when a system is developed without a complete or proper design, moving problems forward instead of correctly addressing them. This has a snowball effect: at an advanced stage, the technical debt is so high that further development is very costly, and this leads to a dead point or astronomical damage to budgets in organizations.
While deadlines cannot always be avoided, adopting an efficient development process in any software development is vital to delivering good quality at a reasonable cost. It also means that the development skills become more mature in a developer, and instead of achieving tasks that fulfill the requirements, engineers can develop software that is efficient, robust, and that can be further extended in the future (what we called maintainability
).
This book introduces techniques for constructing high-performance software for Android devices.
Why does the performance of an application mean so much to so many?
Regardless of the industry, a decrease in the performance or quality of a software system can mean big losses. Software systems today control our finances and control the machines that take care of our health or our public transportation. There is almost no area in our lives that is not at least partially computerized. Not only losses: in a globalized and competitive world, a company producing low-performance software will soon be devoured by the more efficient and cheaper competition.
For a while, the only metric used in software development was Is the software correct? Is it doing what it is supposed to be doing?
. This made sense at the dawn of the computer systems era, when not every single process was computerized and we had not developed a culture of software engineering or good methods for quality control, team organization, and so on. Now, everybody demands more.
Graphs are an excellent way to display information. Let's analyze the smartphone penetration numbers:
The numbers are clear. In the last quarter of 2008, in almost every region of the world smartphone penetration was under 20%. Nowadays, in 2015, most developed countries present a penetration close to 80%, whereas developing countries are close to 40%. In 2020, it is estimated that developed countries will have a penetration close to 100%, and countries in development over 50%. There are a few countries with more mobile phones than inhabitants!
Mobile users nowadays do not only check their e-mail on a mobile phone. There are many operations that are performed on a mobile phone: the entertainment industry, banking and payment, tourism and traveling, gaming… This leads us to a conclusion: software must be not only correct but also efficient. Failure in software will lead to annoyed customers who might opt to use a different competitor with a better-performing product. In extreme cases, non-performing software can lead our business to lose its revenue—imagine the case of an application to make hotel reservations where you cannot proceed to payment.
Manual testing and automatic testing
One of the first thoughts that naturally arise is that testing plays a central role in increasing and improving application performance. This is partially true, or as we prefer to say: testing is a good complement to a smartly designed application, but not a substitute.
If we concentrate just on testing, there are two main types: manual testing and automatic testing. As in the previous case, both types of testing are mutually inclusive, and one should not be used in detriment to the other. Manual testing involves a real user playing around with an application and some defined use-case scenarios, but also with more free will and the ability to leave the road of predefined tests and explore new paths.
Automatic tests are tests written by developers to ensure consistency of the application throughout the evolution in the life of a system. There are a few different types: unit tests, integration tests, or UI tests, which will be familiar to the reader. Good test coverage provides robustness to the system when new changes are being applied, improving resistance against failures and performance problems. As in the previous case, we do not want to exclude manual tests in favor of automatic tests, or vice versa (at least until machines are able to pass the Turing test!).
ANR and delays in software
ANR stands for Application Not Responding, and is one of the several nightmares of an Android developer. The Android operating system analyzes the status of apps and threads, and when certain conditions are met it triggers an ANR dialog, blocking the user from any interactive experience. The dialog announces that the application stopped responding, and is not responsive anymore. The user can select whether he/she wants to close the application, or keep waiting until the application becomes responsive again (if this ever happens):
What triggers ANRs and how can I avoid them?
Android systems trigger ANRs in two different situations:
When there has been no response for an event in five seconds
If a BroadcastReceiver is still executing 10 seconds after its execution
This happens mostly when an operation is being executed in the UI Thread. In general, any operation expected to be time- or operation-consuming should be performed in a separate thread, keeping the UI Thread available for the user interaction, and only notifying the UI Thread when the operation has been finished. In Chapter 5, Multithreading, we will show some advanced techniques for multithreading and thread communication. There are also different classes that can be used to perform operations in different threads, each of them with its own advantages and disadvantages. In general, when developing an application, remember: ANR dialog appearance frequency is inversely proportional to user satisfaction.
Android architecture
As with any other development framework, Android defines its own architecture and module. Android is a Linux-based operating system, although the numerous abstract layers provided by the SDK hide the Linux kernel almost completely, and it is really rare that we will be programming directly at the kernel level:
Dalvik Virtual Machine
Each Android application runs in its own process inside a virtual machine called Dalvik. As we have seen, programs are typically written in Java and then compiled to bytecode. From the bytecode (.class files) they are afterwards transformed into DEX format, commonly using a special tool provided by the Android SDK called dx. This DEX format is more optimized and designed to have a smaller memory footprint in comparison with normal Java .class files, since mobile devices lack the computational capabilities of desktops. This is achieved through compression and merging/optimization of the multiple .class files.
Note
It is not completely accurate that the coding has to be strictly done in Java. Android allows using native code in our applications, too. Therefore, existing code that we were using before can be reused here. Also, in the computer vision area, there is a lot of code that has been reused from the OpenCV framework. This is achieved through the Native Development Kit (NDK), which is explored in Chapter 9, Native Coding in Android and Chapter 10, Performance Tips.
The Dalvik Virtual Machine also includes some Java Virtual Machine (JVM) features, such as garbage collection (GC). There has been a lot of criticism through the GC because of its non-generational nature; it's famous for driving developers crazy. However, since Android 2.3, an improved concurrent garbage collector makes some of the development easier.
Each application running on Dalvik has at least a total of 16 MB of available heap memory. This can be a real limitation for some applications, since we will likely need to deal with large amounts of image and audio resources. However, newer devices such as tablets or high-end devices have a higher heap limit to allow the usage of high-resolution graphics. We expect this situation to improve in the near future due to the fast evolution of mobile hardware.
Memory management
Memory is always, by definition, a scarce resource on any software platform. But when it comes to mobile devices, this is an even more constrained resource. Mobile devices often have less physical memory and processor capacity that their bigger peers, and having an efficient memory management is crucial to improving user experience and software stability.
Dalvik Virtual Machine routinely triggers garbage collection in a similar way to Java, but this does not mean that we can ignore memory management completely. One very common error in junior programmers is to create memory leaks. A memory leak happens when an object is stored in memory, but it cannot be accessed anymore by the running code. The size can vary a lot (from an integer to a big bitmap or structure of several megabytes), but in general they affect software smoothness and integrity. We can use automated tools and frameworks to detect memory leaks and also apply some programming techniques to avoid allocating objects unnecessarily (and equally important, to deallocate them when they are no longer needed).
An Android application has a maximal amount of RAM memory that it can manage. It is different for each device (yes, another problem of the system fragmentation), and can be particularly checked by calling the function getMemoryClass() on the ActivityManager. Early devices had a per-app cap