Pragmatic tips and tricks for the Android programmer

A few days back I deployed a couple of Android apps to the play store. Here are some things I wish I knew before deploying to the store. They could have saved me several hours in productivity.

Test your app on the phone and not the emulator

  • Time – The emulator is slow. It takes up to 2-5 minutes to load the app on the first run and it can take 40+ seconds to load it from a snapshot.
  • Resources – The emulator is bulky and can slow your computer down. Not what you want when in any situation.
  • Hardware – Features such as recording audio need access to hardware on the phone. While there are options to enable certain hardware access on the emulator,  it never works consistently. Some of our code would work seamlessly on the phone but fail on the emulator.

There are solutions out there that attempt to provide better / faster emulation. Out of personal experience I’ve found that nothing beats the real thing.

Parsing data structures ? Watch out

Memory

It is common to come across this code snippet while parsing certain data structures (XML / JSON etc)

 

While this would work in most cases, there are phones out there whose memory limit cannot handle large chunks of strings. Even when data structures are reasonably short, the application can force close. Use a JsonReader. It parses the json in stream and occupies less memory.

GSON

Packages

Have you packaged gson with your build ? Change the package name of the gson jar to ensure it parses structures correctly at runtime. Some HTC phones have old versions of gson pre-packaged in them. Parsing json on these phones will fail.

Proguard

Gson and proguard do not work well with each other unless you follow the recommended rules set out in the google-gson project.

Exception handling

Handle exceptions aggressively. No one likes to see an ‘Application force closed’ message. Use frameworks like ACRA to report error back to you. Fix them quickly. Whatever you do, do not use a google spreadsheet for bugs reported by ACRA. The spreadsheet will eventually stop you from exporting data in any format. ACRA has also been contacted by Google to stop using spreadsheets as the default method to report bugs. Use another provider instead or write your own.

Debugging

Service

Services cannot be debugged by default. You must explicitly tell the device that you wish to connect to the debugger by writing a line of code. I lost an hour on this one alone !

Memory leaks

Attaching a debugger to your program can make it seem like there are memory leaks. Quoting the android documentation on this

The debugger and garbage collector are currently loosely integrated. The VM guarantees that any object the debugger is aware of is not garbage collected until after the debugger disconnects. This can result in a buildup of objects over time while the debugger is connected. For example, if the debugger sees a running thread, the associated Thread object is not garbage collected even after the thread terminates.

So if you ever wondered why your program crashes eventually when you try to debug it but runs fine when it is run in stand-alone mode, this could be the reason.

Async task and the UI thread

Async tasks should be instantiated from a UI thread. Quoting the Android documentation.

The AsyncTask class must be loaded on the UI thread. This is done automatically as of JELLY_BEAN.

Failure to do this will result in weird runtime errors. Even if you instantiate the first Async task incorrectly, it can affect the others.

Prepare for release

Dry run

If this is the first time you are releasing to the android store, release a simple app before releasing the one you were working seriously on. The dry run will give you a fair idea of what the process is and what to expect.

Android already offers guidelines on what to expect when you upload your app. There is also a separate guideline on graphical assets associated with your project. Take a look at the preparing for release guide before you take the plunge.

Shameless plug

You can find these tips implemented on my voice messaging app – PhonOn. Check it out on the play store.

PhonOn on Android

Like this post ? Follow me on twitter

Comments are closed.