In this post, you can find my pack/unpack notes for Android APKs(of course for educational purposes only ;)).

To accomplish this, you basically need JDK, Android-SDK(no surprise!) and apktool.

First we need to decode our application to “smali” code with apktool.

apktool.bat d  sample.apk .\sample\

I usually decode only code part of application with “-r” option if I don’t want to edit resources. (Resource decoding and building can sometimes be error-prone! )

Now we have the smali code under ".\sample\smali\" directory and can edit it easily. If you are familiar with java bytecode, it will be no hard to understand and edit this code. (much more reasonable it is!)

Here is a smali assembly of simple HelloWorld class.

.class public LHelloWorld;
.super Ljava/lang/Object;
.method public static main([Ljava/lang/String;)V
    .registers 2
    sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;
    const-string  v1, "Hello World!"
    invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
    return-void
.end method

You can always refer to Dalvik Opcodes table from here.

After we edited our code we can now reconstruct our application apk with apktool.

apktool.bat b .\sample\ .\sample_edited.apk

In order to install this apk to our phone or emulator, it needs to be signed properly. To do this we should generate a key for signing:

keytool -genkey -v -keystore my.keystore -alias myandroidalias -keyalg RSA -keysize 2048 -validity 20000

It will ask for some information that can be entered freely.

Now we can sign our apk by using generated keystore.

jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore my.keystore .\sample_edited.apk  -signedjar .\sample_signed.apk myandroidalias

Android needs apks to be aligned with 4-byte boundaries to be able to memory-map them.

zipalign.exe -v 4 .\sample_signed.apk .\sample_aligned.apk

Finally if all operations were executed successfully, we can install our edited application for testing. (in my case, to emulator!)

adb -e install .\sample_aligned.apk

That is it! You can enjoy your non-restricted, ad-free and maybe new-featured application ;)



Ali Demiroz

I am a software developer, life-long learner and time-pemitted gamer!

blog comments powered by Disqus