[Galaxy S II Themes and Apps] Faseeh Rehman:
Scripts for creating flashable zips and signing apk/zip files

Just wanted to share a few scripts that I have created for making things a little bit easier. These are bash scripts so they are only going to be functional in a bash shell such as Linux or OS X. If you're running on Windows then you might be able to execute these in Cygwin but I don't know that for certain. You could simply run the same commands in a DOS prompt. You might have to change syntax a little and manually execute them, but the basic process is the same.

I'm going to assume that you are familiar with adb. If not, go learn about it -- download the Android SDK, install the platform-tools, and make sure you can use it! You will probably want it to be in your PATH variable so that you can execute it at any time from a command line.

The next thing you will need is apktool installed, so that you can extract and build .apk files. This can be found here:

http://code.google.com/p/android-apktool/

Now we can move on to using these scripts. There are three files included inside of the scripts.zip file attached to this post. The zip file contains the following:

1. signit: This script will sign a .zip file or a .apk file. If you send in a .apk file as an argument then it will also zipalign the file. It uses testkeys to sign files. 2. createzip: This script will create a skeleton zip file for you. It will print out some instructions to help guide you with the creation. 3. update-binary: This file will never change. It's an interpreter for Edify scripts -- which is what YOU will be writing! You need to include an Edify script inside of the zip file so that recovery knows what exactly you want it to do. This file should be carried around inside of each zip as it converts your Edify script into something that recovery can actually understand and execute.

You will need to make sure that signit and createzip are executable. I would just use chmod +x on them.

The fourth and final thing you need is testsign.jar. You can get it here:

http://code.google.com/p/zen-droid/d...n.jar&can=2&q=

If you look at the top of the createzip/signit script you will see that the the variable binLoc gets set to /usr/local/bin. What this means is that you should place all four of the above files into that location. If you don't like /usr/local/bin then just change the binLoc variable to point to some other directory where you will store these 4 files, and make sure that directory is in your PATH variable.

We'll start by just modifying some images in a single file. For example, let's modify the battery icons in /system/framework/framework-res.apk. I'm not going to add or remove any icons that didn't previously exist, I'm simply going to change a few of them. If you really want to get into modifying some files then you will need to decompile the file in order to change any corresponding XML files (sidetrack -- if you're curious then look in res/drawable at the following 3 files: stat_sys_battery.xml, stat_sys_battery_charge.xml, zzz_stat_sys_battery_1.xml). I'm going to throw this step into the instructions, not because it's necessary here, but because it's a good idea to know how to do it anyway. So let's try and modify our icons!

The basic steps would be something like what I outline below. Before you try this I suggest you make a full backup in recovery just in case something goes wrong. I have reviewed the steps below several times and even walked through them to make sure they are correct so they should work for you.

1. Get the apk file:

Code: $ adb pull /system/framework/framework-res.apk

Copy this file somewhere safe because we are going to push it back to the phone later (you probably won't want to keep the edits we make in step 3 below indefinitely).

2. Decompile it with apktool:

Code: $ apktool d framework-res.apk

3. Update the necessary files:

Step 2 should have unpacked the file into a folder named framework-res. We want to update the necessary files inside of it. The battery icons are all stored res/drawable-hdpi. Navigate to that directory and look for files like stat_sys_battery_*.png. These are the images that get displayed while your phone is running on battery power. The charging images are stat_sys_battery_charge_anim*.png. If you have a set of icons that you want to use then simply paste those icons in and overwrite the existing ones. You can move on to the next step.

If you don't already have images lined up then we can just modify some existing ones to verify that your changes worked. I'll assume you have the stock MikG battery icons (or some way of knowing what your current charge level is) so take note of what your current charge level is, and then modify a few of those images around that area. For example, if your phone is currently at 65% then maybe you will want to modify stat_sys_battery_charge_anim66.png, stat_sys_battery_charge_anim67.png, stat_sys_battery_charge_anim68.png, etc. Just open up these files in GIMP, Photoshop, or whatever. Don't change their size but just change something -- maybe change the color from white to green. Or you could just draw some lines on them. Just make some changes that will be noticeable and then save the files.

4. Package it back up:

Code: $ apktool b framework-res framework-res.apk

5. Sign it using the signit script:

Code: $ signit framework-res.apk

6. Create a skeleton zip file using the createzip script:

Code: $ createzip system/framework framework-res.apk

Note that the createzip script accepts 2 arguments. The first is the directory where the file needs to go, and the second is the name of the .apk file. If you wish, you can run the script without any arguments and it will prompt you for the values. In either case it will give you instructions for what to do next. Here is an example in interactive mode (values that I typed in are highlighted in blue):

Code: $ createzip Please enter the directory that your apk file will go in Example: system/framework system/framework Please enter the apk file to put into this zip Example: framework-res.apk framework-res.apk Creating appropriate directory structure Directory structure complete. A sample updater-script file has been created for you. This sample script will push a file to the /system mount point.

Next steps: 1. If necessary, modify the file /home/gamblor/Desktop/scripts/zip/META-INF/com/google/android/updater-script

2. When script is complete then zip up the directory and sign it: $ cd zip $ zip -r name.zip * $ signit name.zip

7. Make the actual zip file:

Zip up the directory in a recursive fashion using the exact instructions supplied by the script. Note that the createzip script will build a sample updater-script file for you, which says to mount /system and push the files over to it (since that's what most flashable zips do anyway). For this reason, you shouldn't actually need to modify that script at all for the purposes of this tutorial. It doesn't do anything fancy like scroll text across the screen, but it should do the trick to get the file installed. Feel free to open it up with a text editor and see what it does. It will also copy the file framework-res.zip to the appropriate location so everything is in place and we can create our actual zip:

Code: $ cd zip $ zip -r battery-icons.zip * $ signit battery-icons.zip

8. Flash it like you would any other file:

At this point you should have a file named battery-icons-signed.zip. Copy that file to your sdcard and then reboot into recovery and flash it (remember to backup first, if you haven't already). If everything goes well then you should be able to reboot and then check the battery icons for the percentage level that you modified. If everything went well then you should see your modified icons. Congratulations!

As a side note, if you're making mods for yourself and don't plan on creating a flashable zip (or maybe not just yet), you can easily just push the file in adb. This saves you from having to make a zip file, sign it, copy it to your zip card, reboot, and then flash from recovery. So you can just replace step 6 above with the following commands and stop after this:

Code: $ adb remount $ adb shell stop $ adb push framework-res.apk /system/framework $ adb shell start

Obviously since this post was supposed to demonstrate creating a zip file, I included those instructions. But pushing stuff in adb is a shortcut if you're just looking to test things out on a single phone and don't want to waste time dropping into recovery. Remember that file that I told you save away in step 1? Well, if you want to restore your battery icons so that some of them don't have weird colors/marks on them then cd into the directory where you saved the original file away, and then just run the 4 commands above. It should replace the file with the original one, thus setting the battery icons back to the way they were before you ever ran any of the commands above.