Asynchronous Android Programming - Second Edition
()
About this ebook
- Construct scalable and performant applications to take advantage of multi-thread asynchronous techniques
- Explore the high-level Android asynchronous constructs available on the Android SDK
- Choose the most appropriate asynchronous technique to implement your next outstanding feature
This book is for Android developers who want to learn how to build multithreaded and reliable Android applications using high-level and advanced asynchronous techniques and concepts.
Related to Asynchronous Android Programming - Second Edition
Related ebooks
Android Programming: Pushing the Limits Rating: 0 out of 5 stars0 ratingsMicroservices with Azure Rating: 0 out of 5 stars0 ratingsMastering Android NDK Rating: 0 out of 5 stars0 ratingsMastering ServiceStack: Utilize ServiceStack as the rock solid foundation of your distributed system Rating: 0 out of 5 stars0 ratingsAsynchronous Android Rating: 4 out of 5 stars4/5Mastering Spring 5.0 Rating: 5 out of 5 stars5/5Android Design Patterns and Best Practice Rating: 5 out of 5 stars5/5Core Java: Made Simple: A popular language for Android smart phone application, favoured for edge device and Rating: 0 out of 5 stars0 ratingsHands-On Network Programming with C# and .NET Core: Build robust network applications with C# and .NET Core Rating: 0 out of 5 stars0 ratingsLearning Reactive Programming with Java 8 Rating: 0 out of 5 stars0 ratingsMicrosoft Windows Communication Foundation 4.0 Cookbook for Developing SOA Applications Rating: 0 out of 5 stars0 ratingsJava 9 Concurrency Cookbook - Second Edition Rating: 0 out of 5 stars0 ratingsMastering Xamarin.Forms Rating: 3 out of 5 stars3/5Java Coding Problems: Improve your Java Programming skills by solving real-world coding challenges Rating: 0 out of 5 stars0 ratingsProfessional Test Driven Development with C#: Developing Real World Applications with TDD Rating: 0 out of 5 stars0 ratingsJBoss AS 5 Development Rating: 0 out of 5 stars0 ratingsjBPM6 Developer Guide Rating: 0 out of 5 stars0 ratingsMastering Java EE Development with WildFly Rating: 0 out of 5 stars0 ratingsBeginning Visual C# 2010 Rating: 0 out of 5 stars0 ratingsAce the Trading Systems Developer Interview (C++ Edition) : Insider's Guide to Top Tech Jobs in Finance Rating: 5 out of 5 stars5/5Getting Started with Hazelcast - Second Edition Rating: 0 out of 5 stars0 ratingsElements of Android R Rating: 1 out of 5 stars1/5Raspberry Pi 3 Projects for Java Programmers Rating: 0 out of 5 stars0 ratingsAndroid App Development For Dummies Rating: 0 out of 5 stars0 ratingsAndroid Studio Development Essentials Rating: 5 out of 5 stars5/5Akka Cookbook Rating: 2 out of 5 stars2/5Android Things Projects Rating: 0 out of 5 stars0 ratings
Software Development & Engineering For You
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers Rating: 4 out of 5 stars4/5Hand Lettering on the iPad with Procreate: Ideas and Lessons for Modern and Vintage Lettering Rating: 4 out of 5 stars4/5Level Up! The Guide to Great Video Game Design Rating: 4 out of 5 stars4/5How to Write Effective Emails at Work Rating: 4 out of 5 stars4/5Agile Project Management: Scrum for Beginners Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL For Dummies Rating: 0 out of 5 stars0 ratingsPython For Dummies Rating: 4 out of 5 stars4/5Beginning Programming For Dummies Rating: 4 out of 5 stars4/5How to Build and Design a Website using WordPress : A Step-by-Step Guide with Screenshots Rating: 0 out of 5 stars0 ratingsCreative Selection: Inside Apple's Design Process During the Golden Age of Steve Jobs Rating: 5 out of 5 stars5/5PYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5Thinking Beyond Coding Rating: 5 out of 5 stars5/5Ry's Git Tutorial Rating: 0 out of 5 stars0 ratingsAndroid App Development For Dummies Rating: 0 out of 5 stars0 ratingsDevOps Handbook: What is DevOps, Why You Need it and How to Transform Your Business with DevOps Practices Rating: 4 out of 5 stars4/5The Photographer's Guide to Luminar 4 Rating: 5 out of 5 stars5/5RESTful API Design - Best Practices in API Design with REST: API-University Series, #3 Rating: 5 out of 5 stars5/5Coding All-in-One For Dummies Rating: 0 out of 5 stars0 ratingsBICSI RCDD Registered Communications Distribution Designer Exam Prep And Dumps RCDD-001 Exam Guidebook Updated Questions Rating: 0 out of 5 stars0 ratingsGray Hat Hacking the Ethical Hacker's Rating: 5 out of 5 stars5/5Python Handbook For Beginners. A Hands-On Crash Course For Kids, Newbies and Everybody Else Rating: 0 out of 5 stars0 ratingsHow to Build VST Plugin Path to Guru Rating: 2 out of 5 stars2/5Programming Problems: A Primer for The Technical Interview Rating: 4 out of 5 stars4/5LÖVE for Lua Game Programming Rating: 5 out of 5 stars5/5System Design Interview: 300 Questions And Answers: Prepare And Pass Rating: 0 out of 5 stars0 ratingsThe Coder Habits: The #39# Habits of the Professional Programmer Rating: 5 out of 5 stars5/5
Reviews for Asynchronous Android Programming - Second Edition
0 ratings0 reviews
Book preview
Asynchronous Android Programming - Second Edition - Helder Vasconcelos
Table of Contents
Asynchronous Android Programming Second Edition
Credits
About the Author
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
Errata
Piracy
Questions
1. Asynchronous Programming in Android
Android software stack
Dalvik runtime
ART runtime
Memory sharing and Zygote
Android process model
Process ranks
Process sandboxing
Android thread model
The main thread
The Application Not Responding (ANR) dialog
Maintaining responsiveness
Concurrency in Android
Correctness issues in concurrent programs
Liveness issues in concurrent programs
Thread coordination
Concurrent package constructs
Executor framework
Android primary building blocks
Activity concurrent issues
Manipulating the user interface
Service concurrent issues
Started services issues
Bound services issues
Service in a separate process
Broadcast receiver concurrent issues
Android concurrency constructs
Summary
2. Performing Work with Looper, Handler, and HandlerThread
Understanding Looper
Understanding Handler
Sending work to a Looper
Scheduling work with post
Using Handler to defer work
Leaking implicit references
Leaking explicit references
Updating the UI with Handler
Canceling a pending Runnable
Scheduling work with send
Cancelling pending messages
Composition versus inheritance
Multithreading with Handler and ThreadHandler
Looper message dispatching debugging
Sending messages versus posting runnables
Applications of Handler and HandlerThread
Summary
3. Exploring the AsyncTask
Introducing AsyncTask
Declaring AsyncTask types
Executing AsyncTasks
Providing indeterministic progress feedback
Providing deterministic progress feedback
Canceling an AsyncTask
AsyncTask Execution State
Handling exceptions
Controlling the level of concurrency
Common AsyncTask issues
Fragmentation issues
Memory leaks
Activity lifecycle issues
Handling lifecycle issues with early cancellation
Handling lifecycle issues with retained headless fragments
Applications of AsyncTask
Summary
4. Exploring the Loader
Introducing Loaders
Loader API
Loader
Loader Manager
LoaderManager.LoaderCallbacks
Loader lifecycle
Loading data with Loader
Building responsive apps with AsyncTaskLoader
Building responsive apps with CursorLoader
Combining Loaders
Applications of Loader
Summary
5. Interacting with Services
Introducing Service
Started service
Building responsive apps with IntentService
Handling results
Posting results with PendingIntent
Posting results as system notifications
Applications of IntentService
HTTP uploads with IntentService
Reporting progress
Bound Service
Communicating with a Local Service
Broadcasting results with intents
Detecting unhandled broadcasts
Applications of Services
Summary
6. Scheduling Work with AlarmManager
Introducing AlarmManager
Scheduling alarms with AlarmManager
Setting alarms in recent Android versions
Testing your alarms in Doze Mode
Setting a Window alarm
Debugging AlarmManager alarms
Canceling alarms
Scheduling repeating alarms
Scheduling an alarm clock
Handling alarms
Handling alarms with Activities
Handling alarms with BroadcastReceiver
Working with BroadcastReceiver
Asynchronous work with goAsync
Handling alarms with Services
Staying awake with WakeLocks
Resetting alarms after a system reboot
Applications of AlarmManager
Summary
7. Exploring the JobScheduler API
Introduction to JobScheduler
Setting running criteria
Scheduling a job
Implementing the JobService
Listing pending jobs
Canceling a job
Scheduling a periodic job
Applications of the JobScheduler
Summary
8. Interacting with the Network
Introducing Android HTTP clients
AndroidHttpClient
HttpURLConnection
Performing HTTP requests asynchronously
Retrieving a text response
Interacting with JSON web APIs
Converting Java objects to JSON
Interacting with XML web APIs
Converting Java objects to XML
Converting XML to Java objects
Customizing HTTP timeouts
Communicating securely over SSL sessions
Summary
9. Asynchronous Work on the Native Layer
Introduction to JNI
Android NDK (Native Development Kit)
Calling C functions from Java code
Calling C++ functions from native code
Accessing Java objects from native code
Executing native background work on Java threads
Executing asynchronous work on a native thread
Attaching and detaching native threads from JVM
JNI references explained
Interacting with UI from native threads
Starting the native threads
Stopping the native threads
Handling Java exceptions in the native layer
Interacting with a Java monitor from native code
Wrapping native data objects
Summary
10. Network Interactions with GCM
Introduction to GCM
Setting up and configuring GCM for your application
Registering the GCM Receiver
Setting up a registration service
InstanceID listener
Receiving downstream messages
Receiving messages from topic
Sending upstream messages
GcmListenerService delivery callbacks
Executing tasks with GCM Network Manager
Building a one shot task
Summary
11. Exploring Bus-based Communications
Introduction to bus-based communication
EventBus library
Defining events
Submitting events
Registering sbscribers
Thread mode
Posting sticking events
Removing sticky events
Summary
12. Asynchronous Programing with RxJava
Introduction to RxJava
Cold versus Hot Observable
RxJava setup
Creating Observables
Transforming Observables
Understanding Schedulers
Performing IO operations with Schedulers
Canceling subscriptions
Composing Observables
Monitoring the event stream
Combining Observables
Observing UI Events with RxJava
Working with Subjects
Summary
Index
Asynchronous Android Programming Second Edition
Asynchronous Android Programming Second Edition
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 author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: July 2016
Production reference: 1260716
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78588-324-8
www.packtpub.com
Credits
Author
Helder Vasconcelos
Reviewer
Gavin Matthews
Commissioning Editor
Edward Gordon
Acquisition Editor
Indrajit Das
Content Development Editor
Siddhesh Salvi
Technical Editor
Danish Shaikh
Copy Editor
Vibha Shukla
Project Coordinator
Nidhi Joshi
Proofreader
Safis Editing
Indexer
Mariammal Chettiyar
Graphics
Disha Haria
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta
About the Author
Helder Vasconcelos is a Portuguese Software Engineer based on Dublin, Ireland, with more than 10 years of experience designing and developing real-time/multithreaded Java and C++ applications for the telecommunications and aviation industries. Apart from his day-to-day job, he occupies his spare time building native Android applications for Bearstouch Software and other third-party companies.
He graduated with a degree in Electronic and Telecommunications Engineering from the University of Aveiro in January 2006. During his career, he has worked as a Software Engineer for companies such as PT Inovação (Portugal), Airtel ATN (Dublin, Ireland) and Axway (Dublin, Ireland). You can find Hélder on LinkedIn at (https://ie.linkedin.com/in/heldervasc/en) or on his website at (http://hvasconcelos.github.io).
I would like to sincerely thanks all technical reviewers, but especially Gavin. I really appreciate your invaluable feedback and commit that shaped the quality of the book. A special thanks to my awesome wife Tania for encourage me when the lack of motivation was killing my productivity. It would not have been possible without your precious support. Thanks also to my parents and family for their awesome effort in my education. Additionally, I would like to thank my friends, colleagues, clients, and teachers for helping me to shape and improve my skills and perspectives during my career.
About the Reviewer
Gavin Matthews is a veteran software engineer specializing in enterprise scale B2B, MFT and EFSS systems.
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.
eBooks, discount offers, and morehttps://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
Whether you are Android beginner developer or an Android seasoned programmer, this book will explore how to achieve efficient and reliable multithreaded Android applications.
We'll look at best asynchronous constructs and techniques, commonly used by Android Developer community, to execute computation intensive or blocking tasks off the main thread, keeping the UI responsive, telling the user how things are going, making sure we finish what we started, using those powerful multicore processors, and doing it all without wasting the battery.
By using the right asynchronous construct, much of the complexity is abstracted from the developer, making the application source code more readable and maintainable and less error prone.
Using step-by-step guidelines and code examples, you will learn how manage interactions between several threads and avoid concurrency and synchronization problems that might occur when two or more threads access a shared resource to complete a background job, to update the UI or retrieve the latest application data.
At the end of this journey you will know how build well-behaved applications with smooth, responsive user-interfaces that delight users with speedy results and data that's always fresh.
What this book covers
Chapter 1, Asynchronous Programming in Android, gives an overview of the Android process and thread model, and describes some of the challenges and benefits of concurrency in general, before discussing issues specific to Android.
Chapter 2, Performing Work with Looper, Handler and HandlerThread details the fundamental and related topics of Handler, HandlerThread, and Looper, and illustrates how they can be used to schedule tasks on the main thread, and to coordinate and communicate work between cooperating background threads.
Chapter 3, Exploring the AsyncTask, covers the most common concurrent construct of programming in Android. We learn how AsyncTask works, how to use it correctly, and how to avoid the common pitfalls that catch out even experienced developers.
Chapter 4, Exploring the Loader, introduces the Loader framework and tackles the important task of loading data asynchronously to keep the user interface responsive and glitch free.
Chapter 5, Interacting with Services, we explored the very powerful Service Android component, putting it to use to execute long-running background tasks with or without a configurable level of concurrency. This component gives us the means to perform background operations beyond the scope of a single Activity lifecycle and to ensure that our work is completed even if the user leaves the application.
Chapter 6, Scheduling Work with AlarmManager, introduces to us a system API that could be used to defer work or create periodic tasks. The scheduled task could wake up the device to complete the work or alert users to new content.
Chapter 7, Exploring the JobScheduler API, covers a job scheduling system API introduced with Android Lollipop that allows us to start background work when a set of device conditions, such as energy or network, are fulfilled.
Chapter 8, Interacting with the Network, we cover in detail HttpUrlConnection Android HTTP client. With the HttpUrlConnection HTTP client, we will create an asynchronous toolkit that is able to fetch JSON documents, XML or text from a remote server.
Chapter 9, Asynchronous Work on the Native layer, introduces the JNI interface, an Java standard interface that will allow us to execute concurrent tasks on native code (C/C++), interact with the Java code from the native layer or update the UI from the native code.
Chapter 10, Network Interactions with GCM, we will learn how to use the Google GCM to efficiently push and pull efficiently realtime messages from your server and how to schedule work with Google Play Services framework.
Chapter 11, Exploring Bus-based Communications, we will introduce to the reader the publish-subscribe messaging pattern and the Event Bus Library, a publish-subscribe implementation that allow us to deliver asynchronous messages between Android application components.
Chapter 12, Asynchronous Programing with RxJava, we will introduce RxJava, a library used to easily compose asynchronous and event-based tasks on Java by using observable data streams.
What you need for this book
To follow along and experiment with the examples, you will need a development computer with a Java 7 (or 8) SE Development Kit and the Android Software Development Kit Version 9 or above (you will need at least Version 21 to try all of the examples).
You will also need Android Studio IDE. The examples have been developed using Google's new Android Studio IDE and use its integrated build system, Gradle.
While you can run the examples using the emulator provided by the Android SDK, it is a poor substitute for the real thing. A physical Android device is a much faster and more pleasurable way to develop and test Android applications!
Many of the examples will work on a device running any version of Android since 2.3, GingerBread. Some examples demonstrate newer APIs and as a result, require a more recent Android version—up to Android 5, Lollipop.
Who this book is for
This book is for Android Developers who want to learn how to build multithreaded and reliable Android applications using high level and advanced asynchronous techniques and concepts.
They want to learn this technology because they want learn how to build efficient applications that are able to interact orderly with internal/external services and frameworks using Android standard constructs and APIs.
No prior knowledge of of concurrent and asynchronous programming is required. This book is also targeted towards Java experts who are new to Android.
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: We can include other contexts through the use of the include directive.
A block of code is set as follows:
[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)
Any command-line input or output is written as follows:
# cp /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample /etc/asterisk/cdr_mysql.conf
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: Clicking the Next button moves you to the next screen.
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/Asynchronous-Android-Programming. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
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. Asynchronous Programming in Android
Asynchronous programming has become an important topic of discussion in the past few years, especially when using the concurrent processing capabilities available on the most recent mobile hardware.
In recent years, the number of independent processing units (cores) available on the CPU have increased, so to benefit from this new processing power, a new programming model called asynchronous programming has appeared to orchestrate the work between the several independent hardware-processing units available on the device. Asynchronous programming comes to the rescue to solve the problems that could arise from this new processing paradigm.
Android applications, since they mostly run on devices with multiple units of processing, should take advantage of asynchronous programming to scale and improve the application performance when blocking operations, and when CPU-intensive tasks are required.
Android is an open source operating system (OS) based on Linux kernel that was devised in 2003 by Andy Rubin, Nick Sears, Chris White, and Rick Miner, and then acquired by Google in July, 2005.
The Android OS, actually maintained by Google and the Open Handset Alliance, was created to provide an open mobile-device platform for devices with limited resources of computation, memory, and energy.
The platform has been incorporating advanced mobile devices standards, such as NFC and Bluetooth LE, and its scope has grown from a pure smartphone platform to a broader software platform for smart watches, TVs, tablets, and consoles.
The maintainers have been regularly updating the platform with great features and some improvements over minor and major releases since the first release.
The following diagram displays the Android versions over time:
Asynchronous Programming in AndroidAndroid software stack
Android software stack (C libraries and Java frameworks), orchestrated by the Android runtime (Dalvik VM, and most recently, ART) was created around the Linux kernel to provide highly interactive user experiences over a well-proven group of technologies.
In each new OS version, a well-defined application interface (API) is provided to the developer in order to create applications around the new features and standards introduced with the release.
The Android application compiled code (bytecode), typically a Java compiled code, runs on a virtual machine based on Dalvik or ART.
Dalvik runtime
The Dalvik VM (DVM) runtime, created by Dan Borstein, was the first runtime for the platform and is a register-based virtual machine that was created to run the Java code efficiently in a constrained runtime with a limited amount of power processing, RAM, and electric power.
Dalvik's creators claim that the DVM is, on an average, around 30% more efficient than the standard Java VM (Oracle). According to Bornstein, it requires 30% less instructions and 35 % less coding units.
Clearly, Google has gone to great lengths to squeeze every drop of performance out of each mobile device to help developers build responsive applications.
The virtual machine, which runs the Java code compiled and transformed to the dex format over the dx tool, runs on a Linux process with its own memory space and file descriptors. It also manages its own group of threads.
In more advanced architectures, an Android application might run a service in a separate process and communicate over the IPC mechanism, but most of the time, it runs on a single self-contained process.
The dex file and application resources are packed in an Android application package (APK) by the AAPT and installed over Google Play in the end user devices.
Note
The application store distribution model has become extremely popular on the mobile platforms since the launch of the Apple iPhone in 2007.
Since Android 2.2 the DVM comes with a trace-based Just-In-Time (JIT) compilation feature that actively optimizes every time the application runs some short segments of frequently used bytecode called traces.
The generated machine code provides significant performance improvements in the application execution and on the time spent on some intensive CPU tasks, and thereafter, decreases the battery power used.
ART runtime
The ART runtime is a new version of the DVM and was introduced to improve the runtime performance and memory consumption. The new runtime was introduced in Android 4.4 KitKat as an experimental runtime, and since the Android 5.0 Lollipop, it has become the main Android runtime.
This new runtime, making use of the ahead-of-time (AOT) compilation, brings new app-performance optimizations on startup time and application execution. The AOT, as opposed to DVM JIT (Just in Time), compiles the dex files during the installation time using the device dex2oat tool. The compiled code generated from the dex2oat tool generates system-dependent code for the target device and removes the delay introduced by the JIT compilation during each application execution.
The AOT compiler also reduces the number of processor cycles used by the application as it removes the time spent by the JIT compiler to convert the code into machine code, and then uses less battery power to run the application.
One of the drawbacks of the AOT compilation is the larger memory footprint in comparison with the JIT used by DVM.
With the new runtime, some improvements were also introduced in the memory allocation and on Garbage Collection (GC), resulting in a more responsive UI and better application experience.
Memory sharing and Zygote
Basically, the platform runs an instance of DVM/ART for each application, but large optimization of the platform is brought about by the way a new DVM instance is created and managed.
A special process called the Zygote (first life cell in an animal's reproduction)—the process that all the Android applications are based on—is launched when an Android device initially boots.
The Zygote starts up a virtual machine, preloads the core libraries, and initializes various shared structures. It then waits for instructions by listening on a socket.
When a new Android application is launched, the Zygote receives a command to create a virtual machine to run the application on. It does this by forking its pre-warmed VM process and creating a new child process that shares some memory portions with the parent, using a technique called copy-on-write (COW).
The COW technique, available on most Unix systems, only allocates new memory on the child process when the process tries to change the memory cloned from the parent process.
This technique has some fantastic benefits, as listed in the following:
First, the virtual machine and core libraries are already loaded into the memory. Not having to read this significant chunk of data from the filesystem to initialize the virtual machine drastically reduces the startup overhead.
Second, the memory in which these core libraries and common structures reside is shared by the Zygote with all other applications, resulting in saving a lot of memory when the user is running multiple apps.
Android process model
Android is a multiuser, multitasking system that can run multiple applications in parallel, where all the applications attempt to acquire CPU time to execute its job.
Each application runs independently on an isolated Linux process cloned from the Zygote process, and by default, all the Android components run within the same process with the same name as the application package specified in Android Application Manifest (AAM).
The Linux kernel will fairly allocate small amounts of CPU time for application execution called CPU time slices. This time-slicing approach means that even a single-processor device can appear to be actively working in more than one application at the same time, when in fact, each application is taking very short turns on the CPU.
Process ranks
The Android operating system tries to maintain the application running for as long as possible, but when the available memory is low, it will try to free resources in the system by killing the processes with lower importance first.
This is when process ranking comes into the picture; the Android processes are ranked in the next five categories from the higher priority to the lower priorities:
Foreground process: This is a process that hosts an activity or service that the user is currently interacting with: a service started in the foreground or service running its life cycle callbacks
Visible process: This is a process that hosts a paused activity or service bounded to a visible activity
Service process: This is a process that hosts a service not bound to a visible activity
Background process: This is a process that hosts a non-visible activity; all background processes are sorted over a Least-Recently-Used (LRU) list, therefore, the most recently used processes are the last killed processes when they have the same rank
Empty process: This is a process used to cache inactive Android components and to improve any component startup time
When the system reaches a point that it needs to release resources, the processes available to be killed will be sorted, taking into account the process rank, last used processes, and components running.
Process sandboxing
The Android application always runs under a unique Linux user ID (UID) assigned to the application during the application installation so that the process runs on a sandboxed environment, which by default, isolates your data and code execution from other apps.
In some cases, a user could explicitly be required to share the UID with another application to have access to their data:
USER PID PPID VSIZE RSS PC NAME
root 319 1 1537236 31324 S zygote
….
u0_a221 5993 319 1731636 41504 S com.whatsapp
u0_a96 3018 319 1640252 29540 S com.dropbox.android
u0_a255 4892 319 1583828 34552 S com.accuweather.android…
The preceding table that results from running the adb shell ps command in the computer with Android SDK Table is a list of Android running processes.
The first column shows the user identifier (UID) assigned at the time of installation, the second column is the process ID (PID), the third column shows the parent process ID (PPID) that for Android applications is the Zygote process, and the last column shows the application package.
From this list, we can assure that the WhatsApp application is running under the user ID u0_a221 with the process ID 5993 and the parent process is the Zygote process with the PID 319.
Android thread model
Within an Android process, there may be many threads of execution. Each thread is a separate sequential flow of control within the overall program—it executes its instructions in order, one after the other, and they also share allocated slices of CPU time managed by the operating system task scheduler.
While the application process is started by the system and prevented from directly interfering with data in the memory address space of other processes, the threads may be started by an application code and can communicate and share data with other threads within the same process. Apart from the shared data that all the threads share in the same process, a thread can use its own memory cache to store its data in its own memory space.
The main thread
When the application process starts, apart from DVM housekeeping threads, the system creates a thread of execution called main. This thread, as the name explains, plays a crucial role in the application lifetime as it is the thread that interacts with the Android UI components, updating the state and their look on the device screen.
Moreover, by default, all the Android application components (Activity, Service, ContentProvider, and BroadcastsReceiver) are also executed over the main thread line of execution. The following image shows the lists of threads running inside an application process with the main thread at the top of the list with a unique thread ID (TID) assigned by the system:
The main threadThe main thread, also known as UI Thread, is also the thread where your UI event handling occurs, so to keep your application as responsible as possible, you should:
Avoid any kind of long execution task, such as input/output