Saturday, May 25, 2013

Ten Tips for Android Application Development

Beginner tips

1: Learn SQL

Android has an excellent persistence system; use it to make your applications more robust in case of failure or shutdown, and to make suspending and resuming more efficient. Android's SQLite-based storage system is thoroughly integrated with Android's user interface classes.

Android supports the observer pattern in Cursor, Adapter, and View classes, enabling the user interface to connect directly to the results of database queries. Using these capabilities means less work when Android wants to stop your application because you have little or no data that needs to be to be explicitly saved, and more reliability in running on mobile devices that can be out of battery at any time.

The database classes in the Android framework manage caching and are more efficient than reading a whole data model into objects in memory. But, in oder to take maximum advantage of the Android way of designing data models, you will need to know some SQL.

2: Learn XML

You don't have to know much about XML to use the visual editing tools for defining layouts and other resources. You are, in theory, hardly ever required to edit XML "by hand." But these tools are imperfect. The more you know about how Android uses XML, and about the Android XML schema, the easier it is to fix problems that occur in XML in Android applications, and the more comfortable you will be using XML to maximum effect in developing Android applications. A general XML book, like XML in a Nutshell (O'Reilly), or Learning XML (O'Reilly) can be useful.

3: Learn Eclipse IDE

Java can be a verbose language, and without code completion, Javadoc pop-ups, and advanced refactoring, it can be a drag compared to dynamic languages. In other words, Java without Eclispe's productivity features would be no fun at all.

Eclipse, however, can have a steep learning curve for beginners. Getting comfortable with this big, powerful, and sometimes perplexing IDE is a big part of being productive in Android application development. Steve Holzner's book Eclipse (O'Reilly) is an excellent introduction and will get you up to speed.

Within the Eclipse documentation pages, you can get a good start by reading the pages covering Eclipse concepts. Get to know these concepts early on, otherwise the Eclipse menus and documentation will seem full of jargon. Then go on to thebasic tutorial in the Java development section.

4: Use automated testing

Use the Monkey to stress test your application. It generates a stream of events, simulating random inputs, and reports application crashes or lack of response. Use the Instrumentation Framework and JUnit to write unit tests that can be run from Eclipse using adb and the InstrumentationTestRunner.

Learn and use the application and service lifecycles effectively. Enable your application to be efficiently stopped and restarted by the Android system. Unlike many runtime environments with almost trivial application lifecycles, Android's runtime environment frequently stops or terminates processes to keep application resource consumption low, being prepared to restart them if the user navigates back to an application.

Android expects applications to override and respond to a set of application lifecycle methods and cooperate with the way Android manages applications as they become visible on the screen or are obscured by other applications. Get this code into your application early on in its development, so that you do not end up retro-fitting these method overrides. Use the debugger to set breakpoints in the application lifecycle methods so that you get a feel for how the Android application lifecycle works.

Advanced tips

5: Use static analyzers

Java lends itself well to meaningful compiler warnings, and to warnings and "advice" based on static analysis. Every potential bug you kill before your application leaves development brings you closer to a 4-star user rating instead of a 2-star one. Use the compiler warnings, FindBugs, and try other static analyzers, like PMD available as Eclipse plug-ins to find the tools that find the most problems and the fewest false positives.

6: Use and be used

Use Intent objects and StartActivity (and related methods) to "borrow" functionality from other applications, and write your application to respond to Intent filter matches and provide functionality to other applications. Appications can also share data through the ContentProvider system and through remote method interfaces.

7: Divide large applications

If you are writing a large application, consider dividing it into a suite of applications and services. Smaller applications load faster and use fewer resources. Making a suite of applications, content providers, and services makes your code more open to incorporation into other applications as described the "Use and be used" tip.

8: Design for low power consumption

Things that are benign on a personal computer, like polling a server every 10 minutes, can cut a handset's battery life in half or worse. Code your application to do as little as possible until the user brings it to the foreground or some external information arrives that requires action. Use the "Battery use" in the "About phone" menu in "Settings" to find the applications and other system functions using the battery.

9: Use the NDK to access library functions

The Android NDK is an optional companion to the Android SDK that enables use of native compiled code. Use the NDK to incorporate existing C and C++ libraries where they provide useful functionality. Code CPU-intensive computations, if they must be done on the handset, in C or C++. Use the NDK's tools to call that code from Java using the Java Native Interface (JNI).

10: Avoid hierarchy

Don't design your application around a central location, "home" screen, or fixed starting place. Mobile device users want instant access to functionality. They won't tolerate moving up and down menu hierarchies. Furthermore, you want to provide stand-alone activities that are easy for other applications to invoke through the Intent interface. Allow each activity to be used independently, and include a menu in it that allows access to every other activity. In short, embed your application into a seamless Android user experience.

No comments:

Post a Comment