While developing Guns of Legend for Android, we learned quite a few things about the Android build process. Firstly, it’s not very well documented. As far as we could tell, it didn’t perform much before-time optimizations on bytecode. The dex compiler performs some optimizations, but we had observed a noticeable difference between debug and release builds made with Proguard. Lastly, some common programming practices designed to improve code quality cause performance bottlenecks in unoptimized Java bytecode. J2SE Virtual Machines solved this problem by implementing “just-in-time” compiling which performs whole program optimization while the bytecode loaded into memory. We put together a set of Apache Ant build scripts to streamline the process of building, packaging, and fully optimizing an Android application before deployment.
Attached to this post is a copy of a template script designed to streamline your Android build process, protect your code from reverse engineering, and perform crucial optimizations on your code. Additionally, Proguard can remove code that should only be left in for debugging. It is common practice to write redundant checks for null arguments in functions. Though these are often times handy in diagnosing problems, they can slow down performance. If you were to isolate the checks to static methods in a single class, it is very easy to have Proguard simply throw out the class from the build all together. It will make it more difficult to interpret crash stack traces but it will provide better performance. When using this feature of Proguard, it is your responsibility to ensure that the removed code will not affect the normal behavior of the application. Only remove classes/methods which are well isolated from the rest of the program, or else you may find that obfuscated builds crash while debug builds don’t.
Lastly, please bear in mind that this script was originally written for pre-1.5 (Cupcake) releases of the Android SDK then later revised. Parts of this script can be integrated into the ones auto-generated by the Android SDK, or you can use this with a little bit of modifications. Please let me me know in the comments if you need help using this script or tailoring it to your particular application. I hope that the Android community will find it useful in some way!