There's no lack of books to get you started in Android development out there, but when it comes to more advanced topics, it can be difficult to find a great book, one which will always be close at hand when you're working on your app. Android High-Performance Programming may be that book.
Written by Enrique López Mañas, this book starts off with the intent to make sure you understand the importance of performance and know how to use debugging tools available to find potential issues in your app. While this sounds basic, it's essential groundwork for what's to come.
Each chapter in the remainder of the book covers a particular topic, broken into three sections: a walkthrough, best practices, and debugging tools that can be applied.
You'll find yourself taken through the following:
As you examine the potential performance tweaks that you can make to each of the above areas, you'll find the book starts to change your development approach. These chapters force you to understand why certain approaches will slow your app down, and then give you some ways to get around it. The importance of this cannot be understated; if you wish to become a knowledgeable and respected app developer, you need to know this stuff.
The final section of the book focusses on one of the most important topics for developers out there, Optimizing Battery Consumption, as well as a run through Security and Native Coding in Android. Burning through battery is something that many apps are guilty of. Suggestions are included such as reacting to changes in battery level and avoiding useless networking requests.
The security chapter is one that might keep you awake if you've already released an app to the Play Store. In fact, read this chapter first; it contains essential tips on how you apps code can be reverse engineered and talks about insecure storage and unencrypted communication.
The book then concludes with a number of general performance tips with everything for image optimization, using Java 8 in Android N and APK optimizations.
Can you get all this data elsewhere? Of course. But I have never seen all of this information packaged up in such a concise and clear manner. Scan through the book once to find some small wins you can add to you app. Put the book on your developer shelf, and have it close to hand for when you hit the inevitable performance crises in the months ahead.
I got a chance to catch up with the author, Enrique López Mañas, to ask him more questions about his background, the book and more tips around performance in Android apps.
DZone: Can you introduce yourself and your background?
Enrique: I am an independent consultant based in Munich who has been programming for Android since its conception and public release around 2008. I am part of the Google Expert crew, where I regularly participate in Google global initiatives to help influence hundreds of the best startups from all around the globe.
DZone: What apps have you worked on?
Enrique: My first steps in Android where just pet projects. I do enjoy learning language, so I started making a few applications for myself to check if my models and methodologies for language learning were working.
Rapidly I got contacted by a few companies to start working with them. I spent a year in Barcelona where I worked with some of the main banks based there: ING Direct, Banc Sabadell, Caixa Catalunya.
After that, I moved to Germany, and started a three years adventure at Sixt, the car rental company. There I worked on very compelling products: DriveNow, a car sharing product right now active in a bunch of European countries. Sixt, the flagship application of the company and myDriver, the application for a limousine service. At the moment I am working at the Stadtwerke München on an application that provides bike sharing services and unifying the car sharing services and transportation of the city, and as well at a startup in Stealth mode with a disruptive project.
DZone: How challenging was this book to work on?
Enrique: I got first contacted by the publisher, Packt Publishing. They were very interested in writing a book on this topic, since it was getting very hot and there was a lot of hype about it. When I program I always thought I was taking care of performance, but writing a book? Damn, it was ambitious!
The rule of thumb is that for each hour spent writing I did spend 3 hours researching, testing my code and my assumptions. One of the biggest mistakes a technical writer can make is to underestimate these side tasks. I thought it would be all a path of roses, but I found myself continuously behind deadlines. A lot my assumptions about programming and performance were wrong: when you are writing a book everything must be strictly right, and it involved a lot of compiling, decompiling to check the size of the files generated, debugging, etc. Writing the book had a very positive side effect on me, and I learnt a lot while writing it.
DZone: Can you give us examples of some apps that hit the mark with good performance?
Enrique: The Quora app is great, as is their service. They have a lot of magic happening underneath, such as a lot of offline mechanisms. It's the same in the Twitter app, which has many hidden features and can be considered to be a very stable application in general. Facebook has had its ups and downs with their Android application, but now I think they are on the way to greatness, and that is very hard if you consider the size of the team they gathered together to develop their Android app.
DZone: Is there a checklist that developers can use to see how they fare in the range of good to bad?
Enrique: There is no such thing as a checklist, although that could be a great idea! Besides the perceptions on responsiveness when using the application (immediate replies from the device after touching it, not having excessive loading times, etc.) there are always a few points worth checking.
Memory usage should always be as low as possible. There are a few free tools in the Android Store that developers can use to test their application against another ones. You want your application to be among the ones using less memory. This is also very easy to check with the help of the DDMS tool in Android.
Knowing all the multithreading options and using them. This is a huge field in Android, and developers need to know all the different options available for them. If you are not using a multithreading mechanism in your application you might want to revise them again. There is a big probability that your application can benefit from this.
Battery consumption: this is generally correlated with some other aspects. If your application is consuming a lot of battery you are doing something wrong. You might want to check this against some other applications, to see what the perception of a final user is going to be.
Networking: making network requests and downloading data from the Internet is one of the operations that consume more battery in a mobile device. You can use many different mechanisms to improve this, and I am not only talking about caching. You can batch your connections, use latency gauging, an exponential back-off to communicate with a backend, etc. I am still surprised of how few companies actually do this.
DZone: You talk about a lot of different aspects from networking to battery consumption. What is the most common area that apps find themselves lacking?
Enrique: As a consultant I have the chance of checking a lot of source code from different companies, and I am surprised of how little companies think about High Performance. The predominant thinking seems to be "If the functional criteria are fulfilled, why bother?" We are always in a rush to develop new features, and we would rather present something visual in our next Sprint demo rather than caring about something that nobody will see at a first glance. That being said, I think the optimisation for battery consumption and networking generally rank very low in priorities.
DZone: Native Coding in Android is an interesting chapter, but it seems a bit extreme with the wealth of APIs and libraries available. What cases should someone consider native code?
Enrique: Native Code can provide a clear advantage when we want to use libraries or an exising C/C++ codebase, since we do not need to write our own wrappers in Java. In general, any task that will be performing intense data computation or deals with graphics will perform better if it is done with the NDK. Actually, as a proof of concept, I did release some time ago a library to perform a blur effect on Android, called Android Stackblur. The library offers three different methods: Java, NDK and RenderScript. The difference among them are astonishing.
Adding a base of Native Code to our application adds complexity, and generally we might want to make sure our developers in the team have the required skills to work further with it. There is a always a balance to be found in Software Development, so before you take the decision be sure that the trade-off is on your side.