Wednesday 7 August 2013

Internals of Android applications .apk files

Ever since I saw an article on creating a basic android application I have been curious about how some android applications have been created and since I have a working android installation it is time to take a look at how they work.

My android install is on a qcow2 disk image so I can mount it on my system via ndb, just like I did when building my LFS system.

I copied off an apk file to test.

The apk file is effectively just a zip file, so I could just unzip it to get the files, however when I do this most of the files are in a binary format (serialized objects?) and unreadable. Since the discussion a few months back on android bug 8219321 I was aware of the apktool utility to unpack an apk file.

So used apktool to unpack the apk file, like so

apktool d <apkfile> <dir to unpack to>

I now have manifest xml files which are readable and code. I was expecting to get java sourcecode files from this process, but all the files were smali (Dalvik byte code). This does not look too hard to understand but I wanted something I was a little more familiar with, i.e. java.

Therefore I needed to extract files from within the classes.dex file (Dalvik executables) and convert them to java classes.
So I just extracted the classes.dex file from the original apk file using unzip (jar would work as well if a jdk was installed).

I now need to convert the Dalvik executables to java class files, to do this I used the utility dex2jar e.g.
d2j-dex2jar.sh classes.dex

This has converted the dex file into a jar file, again I could use the jar utility to extract the class files but as it is basically a zip file I will use unzip.

I now have a bunch of java class files which I need to disassemble and look at. In the past I have not had much luck with java dissassemblers, however, I have seen one recommended called jd-gui.

This is a 32bit application and as I am running a 64bit OS I needed to install the 32bit libXxf86vm runtime library.