Thursday, November 24, 2011

Taking a peek at Android source code (decompiling)

Are you curious about some Android apps behavior?
Do you want to learn how some things are done on Android?

Android apps are written in Java, so it's easy to assume that a .apk file is the same as a .jar file. And if you know a bit of the Java world, you know that a .jar file is nothing more than a renamed .zip file containing compile binaries (.java files).
In the case of .apk files you can find a single classes.dex file (if the app is optimized, you'll find an .odex file) for code and resources and manifest related files. These files are generally not directly accessible. For reading them you'll need some tools.

For some info about the difference between .dex and .odex files you can go here. I won't talk about odexing apps in this article.

Remember that when decompiling an app you won't get the exact original source code, but an approximate that may not even compile back.

Here are the tools for you to open an .apk file:
- dex2jar;
- JD-GUI (Java Decompiler);
- apktool;

You can obtain two different results depending on the tools you use. With dex2jar you can convert a classes.dex file to a regular .jar which can be decompiled by Java Decompiler.
With apktool you can obtain the AndroidManifest.xml, all resource files and the code in a kind of assembler language.

Both of the tools are easy to use:
dex2jar (windows) --> dex2jar.bat classes.dex
apktool (windows) --> apktool.bat d .apk

Now to the hard part. Go play!

No comments:

Post a Comment