« | Main | »

May 02, 2010

APK files are the archive files on which Android applications are transported. In these is a manifest, which is essentially an XML file with meta data about the application and its components. I thought it would be useful to have something on the command line to find the version code and version name of an APK. On the outset it also seemed pretty straight forward, since an APK is just zipped, so I could just extract the manifest, do some filthy XML parsing to get these attributes and print them out. I didn't know the file was encoded in some undocumented binary XML format, so I thought I'd post the results if anyone is idiotic to need the same utility, to save a couple mouse clicks:
APK.java
Some sample output (don't let this give you sticky pants...):
Results -----------------------
versionCode         : 12121212
versionName         : 10.114
The bulk of the work is done in a creatively-named method, read, where there are some comments on the apparent file format. I think there's basically this:
  • A magic number
  • Stuff
  • A string table containing
    • A list of offsets into this table
    • The strings themselves
  • Binary encoded XML consisting of recursively-defined:
    • Tag reference into the string table
    • Flags identifying the node
    • More information
There's lots of stuff in there I couldn't figure out, or really cared about. Luckily, the versionCode and versionName attributes lie in the top-most tag, manifest and weren't too hard to pick out.

Posted by jeff at May 2, 2010 05:03 PM