http://www.developer.com/ws/android/programming/android-ui-design-principles.html

Back to article

10 Android App Performance Tips for Developers


November 15, 2011

As any mobile platform evolves and its application offerings improve, quality becomes paramount to success. These days, users demand responsiveness and reasonable performance from the Android applications they choose to install on their devices. And if those applications fail to deliver both exciting functionality and a stable user experience, then the application is quickly uninstalled.

Although Android smartphones and tablets are getting faster every day, developers need to remember that their applications are still running in a resource-constrained environment powered primarily by battery and processors that do not rival the latest desktops or laptops. Here are some ways to slim down your applications so they run optimally on the Android devices of today and tomorrow.

Let's start with some coding tips for responsive applications.

Android App Performance Tip #1: Start with Good Coding Practices

Be a good steward and use common sense, well-established algorithms and standard design patterns. Resource-wise, if you open it, close it. Acquire late, release early. These long-standing coding mantras apply to your Android applications as well, especially if they use underlying device services.

For example, let's say you've written an application that relies upon location-based services. Don't start register for location updates until you absolutely have to, and make sure you un-register for updates as soon as you no longer need the information. This will help you not drain the device battery or hog the system unnecessarily.

Android App Performance Tip #2: Keep Blocking Operations Off the Main UI Thread

Keep your applications nimble by using an AsyncTask, thread, IntentService, or custom background service to do the dirty work. Use loaders to simplify state management of long loading data, such as cursors. You cannot afford for your application to lag or freeze while some processing is going on.

If an operation takes time and resources, offload that processing and perform it asynchronously so that your application remains responsive and the user can go about their business. This applies to operations such as: reading and writing to disk, accessing content providers, databases and the Internet, as well as parsing and other lengthy tasks.

Android App Performance Tip #3: Use the Latest Android SDK Versions, APIs and Best Practices

Keep your applications up-to-date and using the latest that the Android platform has to offer. As the Android platform evolves, it is improved. Some features may be retired, or replaced with better options. Core APIs receive bug fixes and performance improvements. New APIs like loaders have been introduced to help developers write more stable and responsive applications.

Did you know you can enable hardware acceleration in your Android 3.0 apps? Do it! Understand that best practices change over time. Smart developers stay on top of what's new in the platform and what's no longer recommended.

Android App Performance Tip #4: Check Out Strict Mode

You can use an Android API called StrictMode to help you track down violations in several good coding practices. StrictMode will help you identify if your application is leaking memory, as well as detect whether or not your application is trying to perform lengthy blocking operations that should be offloaded to threads or otherwise (see #2).

The StrictMode class (android.os.StrictMode) was introduced in Android 2.3.

Android App Performance Tip #5: Disable or Minimize Debugging and Diagnostics Prior to Publication

If your Android application has taken more than a few minutes to develop, you've probably got some logging and debugging code built into your application. Writing to logs and other such output comes at a performance hit. Make sure these features are minimized or completely disabled prior to release.

Now let's talk about how to make your application screens load faster with good user interface design principles:

Android App Performance Tip #6: Keep Your Layouts Simple, Elegant, and Shallow

Simple screens make for the easiest reading and simple layouts load the fastest. You don't want to nest your layouts too deeply or clutter your screens with more View controls than absolutely necessary. Spend the time to develop elegant user interfaces that users can use effectively instead of trying to cram too much functionality onto a single screen. Not only will this help application performance, but it will help make your application more efficient for users.

Fragments can help compartmentalize user interface functionality without sacrificing flexibility for designing for different types of devices.

Android App Performance Tip #7: Tailor Your Application Resources To Your Target Devices

Include resources that are tailored to specific device configurations, so that they load as efficiently as possible. This is especially important when we're talking about graphics resources. If you include huge drawable image resources that require loading and resizing, you're not using alternative application resources effectively.

Another trick? To keep your application package file reasonably sized when targeting many devices, only include core resources needed to run the application initially, then have the application download content tailored to the device.

Android App Performance Tip #8: Use the Hierarchy Viewer Tool

The Hierarchy Viewer tool helps you debug your application layouts. It also provides valuable profiling information about how long it takes each View control within a layout to measure, render, and draw. Pinpoint problem areas and fix them easily.

Android App Performance Tip #9: Use the layoutopt Tool

The layoutopt tool is a simple command-line tool that can help you identify unnecessary nesting of controls and other ways to collapse your layout resources to make them as slim as possible. It will give you hints on what layout controls may be redundant or unnecessary. Fewer controls and shallower layouts perform better.

And finally… So you think your app is the best it can be? Now it's time to put it to the test.

Android App Performance Tip #10: Use Traceview and Other Android Tools for Profiling

The Android SDK comes with a number of tools for profiling your applications. Perhaps the most popular tool is Traceview, a graphical tool that can help you debug and find performance bottlenecks in your apps. Check out some debugging tools discussed in the Android documentation.

Conclusion

There are many ways to speed up the performance of your Android applications. Some involve using specific algorithms and others rely upon tried and true debugging and performance monitoring techniques. Luckily, the Android platform comes with numerous free utilities to help track down and eradicate performance problems within your applications. You've got 'em, now use 'em!

About the Authors

Shane Conder Shane Conder and Lauren Darcey--Contributing Editors, Mobile Development--have coauthored two books on Android development: an in-depth programming book entitled Android Wireless Application Development (ISBN-13: 978-0-321-62709-4) and Sams Teach Yourself Android Application Development in 24 Hours (ISBN-13: 978-0-321-67335-0). When not writing, they spend their time developing mobile software at their company and providing consulting services.

        Email        |        Blog        |        Twitter

Lauren Darcey

Sitemap | Contact Us