Thursday, March 8, 2012

Android APK Extractor

I have been working on Android since last couple of years and its really amazing to watch how SDK takes care of few brilliant stuff, like XML based resource mapping, Delvik class generator (.dex or .odex file). So, thought lets explore internals of APK file structured and create a APK parser. Somehow any Hex Dump fascinates me enough to explore its real meaning, crazy? this is how it is :-)
I did google and found few solutions which require Android SDK AAPT (Android Asset Packaging Tool) to decode APK resource. So, I thought to create an alternate parser (independent of Android SDK) which developer community can use to extract content of APK.
 Android APK is a zip file, any ZIP parser/program can parse and extract its content. But this is the easy part, the tricky part is to convert resource files stored inside the APK (i.e. Manifest, Layouts, String.xml etc.) to plain text and convert Dex/ODex file to readable JAR. To speedup the run time processing of these resource files, AAPT encodes XMLs to Binary format. This binary format is not WBXML, it has its own structure and it took me a while to understand the same.

To hide all these complexity of parsing Android binary XML structure, I have created a simple Parser interface, which you can use in any kind of project- an Web Service which can extract uploaded APK file and show its content or an APK analyzer. 

There are few known limitations and I'm continuously working on the same, so you'll be getting updates pretty soon. Please download  APK Extractor and share your comments.

15 comments:

  1. give me the usage of tool & your xml parser interface in the java standalone application.

    ReplyDelete
  2. Great! can't wait the complete version

    ReplyDelete
  3. Is it supposed to work on Linux?

    I get:

    $ ./run.bat
    off
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 6: fg: no job control
    ./run.bat: line 7: $'\r': command not found
    Exception in thread "main" java.lang.NoClassDefFoundError: com/pras/Extract
    Caused by: java.lang.ClassNotFoundException: com.pras.Extract
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    Could not find the main class: com.pras.Extract. Program will exit.
    ./run.bat: line 9: $'\r': command not found

    Send your feedback to- http://prasanta-paul.blogspot.com/
    ./run.bat: line 12: pause: command not found
    ./run.bat: line 12: $'\r': command not found

    ReplyDelete
  4. Hi Teo,
    run.bat is only for Windows. You need to create an equivalent .sh (Shell Script) or manually execute the Java files.

    Thanks,
    Prasanta

    ReplyDelete
  5. my run.bash script for mac (linux should be same):

    #!/bin/bash

    APK="./apk/Fragment.apk"
    DBG=2
    DX=./lib/dex2jar
    EXTRACTOR=./apk-extract-1.0.jar
    LIBS=$EXTRACTOR:$DX/asm-all-3.3.1.jar:$DX/commons-cli-1.2.jar:$DX/commons-lite-1.9.jar:$DX/dex-ir-1.6.jar:$DX/dex-reader-1.9.jar:$DX/dex-tools-0.0.0.4.jar:$DX/dex-translator-0.0.9.8.jar:$DX/dx.jar:$DX/jasmin-p2.5.jar:$DX/p-rename-1.0.jar

    java -cp $LIBS com.pras.Extract $APK $DBG

    echo
    echo Send your feedback to- http://prasanta-paul.blogspot.com/

    ReplyDelete
    Replies
    1. damn I just spent half an hour to rewrite the .bat with my poor bash skills. I just came here to share my solution.

      Note to self: Next time look first then code by yourself.

      @prasanta: thx for your work. Helped a lot!

      Delete
    2. How to again decode the apk after making changes in the AndroidManifest.xml

      Delete
  6. Thanks, works great in Windows 8.
    Excellent work.
    Regards,
    John.

    ReplyDelete
  7. Hi I am getting following error. Where should I keep the apk file.

    Log Level: 1
    1- Debug, 2- Production. Debug is slow.
    Log File: /home/pawan/Dev/WorkSpace/APKExtractor/apkExtractor_log.txt
    Parsing data, please wait...
    java.io.FileNotFoundException: Fragment.apk (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.(FileInputStream.java:120)
    at java.io.FileInputStream.(FileInputStream.java:79)
    at com.pras.Extract.unZip(Extract.java:85)
    at com.pras.Extract.main(Extract.java:283)

    ReplyDelete
    Replies
    1. Hi Prasanta, can you please tell me how to use your project..I have imported it eclipse and when I run it I get above error

      Also can you please tell me if I can use your code not just for parsing but also for modifying arsc file

      Delete
    2. Hi Prasanta,

      I am able to run the code but it seems like the part I was most concerned with (parsing resource.arsc) is commented out. Any update on this, is it complete?

      Delete
  8. I needed to modify versionCode in compiled AndroidManifest.xml to easily create separate APK files for different hardware configurations. Used your APKExtractor to learn how to read such file. See more at http://stackoverflow.com/a/19554367/1477347. Thanks!!!

    ReplyDelete
  9. Thanks alot. This saved my skin. A subcontractor was supposed to deliver source code and he didn't deliver the expensive 3rd party libs that we paid for. This tool showed us that. Really appreciate it.

    ReplyDelete
  10. thanks alot, after wasting at least 5 hours searching and trying diffirent decompilers, only urs works..

    but pls how can I re-compile the decompiled apk after making changes
    ???????????????????????????

    ReplyDelete
  11. thanks alot, after wasting at least 5 hours searching and trying diffirent decompilers, only urs works..

    but pls how can I re-compile the decompiled apk after making changes
    ???????????????????????????

    ReplyDelete