Reader small image

You're reading from  Android High Performance Programming

Product typeBook
Published inAug 2016
Reading LevelBeginner
PublisherPackt
ISBN-139781785288951
Edition1st Edition
Languages
Tools
Right arrow
Authors (3):
Emil Atanasov
Emil Atanasov
author image
Emil Atanasov

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 Unversity "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. As an Android team leader and project manager, Emil was leading a team that was developing a part of the Nook Color firmware -a 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 largely to the book "Objective C Memory Management". "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 is spending most of the time in the digital world. Thank you, guys!"
Read more about Emil Atanasov

Enrique López Mañas
Enrique López Mañas
author image
Enrique López Mañas

Enrique Lpez Maas 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.
Read more about Enrique López Mañas

Diego Grancini
Diego Grancini
author image
Diego Grancini

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.
Read more about Diego Grancini

View More author details
Right arrow

Chapter 10. Performance Tips

This chapter is about techniques and hints and tips about topics not covered in the previous chapters.

Therefore, we want to define here best practices for image handling: images are widely used in many applications in the store. For this, we want to know how to manage images in an Android application to improve overall performance. For this topic, concepts from various previous chapters are needed.

Beyond bitmap management, we will go through alternatives to largely used, but not performant, serialization formats such as XML and JSON in order to find a better way to speed up client/server communications and limit encoding/decoding time and resource consumption.

In conclusion, the last part of the chapter will discuss a couple of measures to improve the application before the building process. These include the reduction of resources and how to clean the APK so as to have a smaller APK file to distribute through the store in order to be compliant with store limitations...

Bitmaps


One of the biggest challenges with our application is to handle images in an efficient way because there are a lot of different perspectives that impact the resulting application. This is a particular topic that covers almost everything we discussed in previous chapters:

  • Bitmaps need to be in a layout to be displayed correctly. Hence, what we discussed in Chapter 2, Efficient Debugging, is particularly important here.

  • Bad bitmap handling can lead to memory issues due to leaks or because bitmaps are used badly as variables instead of being read when needed. Hence, Chapter 4, Memory, can be helpful to keep in mind key concepts while saving and reading large images.

  • Too many times, we try to process significant amounts of data coming from images on the main thread; we will use the topics discussed in Chapter 5, Multithreading, to understand how to handle bitmaps efficiently with no impact on user experience.

  • Most of the time, images come from a remote resource. We will discuss how to retrieve...

Image optimization


In the previous pages of this chapter, we discussed how to handle images when they are ready to be loaded and displayed. We want now to discuss the way they get into the device and how to improve this process. It is now clear that images are big memory boulders, and instead of improving the user experience of our application, they can break it up if we do not take care of handling them properly. So, we can design the best framework to download images from a remote server, but if they are too large, or if their compression is not high enough, our application will be perceived as slow and expensive. Images need time and bandwidth to be downloaded. Hence, our aim is to find the right way to reduce their size as much as possible, without compromising their quality.

Tip

An application that displays images always needs a good design to ensure the download process is fast. To do this, images must be as small as possible regarding bytes used, to make it easier to transfer them from...

Serialization


The same considerations related to lowering image sizes to speed up transfers can be used for text files as well. So, let's have a quick overview of a typical format to transfer data over our client/server architecture. Until a couple of years ago, the XML format was the most used. Then developers changed it to JSON format. Both are human readable, but JSON is simpler to write because of its syntax. It has no need for tags and attributes. For these reasons, JSON is lighter and more preferred and used than XML.

JSON improvements

Google provide an easy-to-use library to handle JSON serialization and deserialization, called GSON. In principle, it uses reflection to find the getters and setters of a Java bean; then, if everything is in the right place inside the bean, it can be deserialized by providing just the wanted class, to create a new object filled with all the data inside the JSON file.

To improve serialization/deserialization performance and transfer timings, we need to improve...

Code improvements


We want to discuss in the following pages a couple of optimizations related to particular coding situations and common patterns. These tips are examples of how common habits in practical everyday development work may lead to performance faults.

Getters and setters

One of the core concepts used in object-oriented programming is encapsulation; as you know, it means that the fields of an object should not be accessed directly by other objects. So, you can encapsulate an object's fields in Java by using the private modifier and by creating getter and setter methods to let other objects access them. This guarantees that the class itself has complete control over its own fields and no one else can use it. Then, you are free to create read-only or write-only fields, simply defining just the related method and avoiding defining the other one.

The benefits of encapsulation are not at issue, but they come with a cost. Accessing fields directly is three times faster than using a getter...

Java 8 in Android N


The new Android N SDK provides support to the new features Java 8 introduced on its release. In the following pages, we will go through them to understand how they can be helpful while developing our application, and go through the new toolchain introduced to improve timings while building the APK file.

Setup

In order to use the new Java 8 features, we need to target the new Android N and use the new Android Studio 2.1 that supports Android N, otherwise, those features won't be available. At the time of the writing this book, the new Android Studio 2.1 is in preview version. However, we can use it to have a better understanding of the steps to follow to use Java 8 and its new features in our projects. This is because the new Jack toolchain, introduced in Android MarshMallow (API Level 23), which we will discuss in greater detail in the following pages, with the new Gradle plugin, is the only way to compile through Java 8 and use the features we will go through in the following...

APK optimizations


When everything is done, the code is developed and tested, and users are waiting for an update of our application, we use it to build an APK file to distribute through the Google Play Store or anywhere else. However, due to multiple factors, the resulting APK file is forever getting bigger: new feature implementations, new, different configurations to support, new Android versions, more libraries used in the application, and so on. This way, we are forcing our users to use more bandwidth to update it and more storage to save it. In addition, there is a limit to the APK size that can be uploaded and distributed via the store. So, are we sure that we are doing well? What can we do to reduce the file size? Let's try to give an answer to these questions in the following pages from different points of view.

Removing unused code

High-level languages consider reusability of the code to improve development times and reduce debugging. It is also helpful to minimize the APK file size...

Summary


We started this chapter discussing the importance of the good management of images from different points of view, because it is critical for every application that handles them:

  • Loading: Images are the biggest weight on the memory. Many times we use them as they are, without processing them properly to reduce their pressure on overall system performance. For this reason, scaling operations are always needed in such a fragmented market as that of Android devices. Hence, we discussed the proper way to enhance performance while scaling them by using the commonly provided Android API.

  • Processing: Operations over images are expensive, and they need to be executed in a worker thread in order to free the main one from unnecessary computations. We looked at a way to elaborate images safely from a responsiveness perspective.

  • Caching: The best way to save external communication is to save data for future reuse. That's why we improved methods and algorithms to cache images, maximizing their...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Android High Performance Programming
Published in: Aug 2016Publisher: PacktISBN-13: 9781785288951
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (3)

author image
Emil Atanasov

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 Unversity "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. As an Android team leader and project manager, Emil was leading a team that was developing a part of the Nook Color firmware -a 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 largely to the book "Objective C Memory Management". "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 is spending most of the time in the digital world. Thank you, guys!"
Read more about Emil Atanasov

author image
Enrique López Mañas

Enrique Lpez Maas 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.
Read more about Enrique López Mañas

author image
Diego Grancini

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.
Read more about Diego Grancini