The greatest thing about Android and the main reason that will keep me a die-hard Android fan is that you can change just about every element of the U.I, and it’s quite easy to do too. I created this thread to share with you what I've learned during my time here at XDA, and to be a reference of information related to .apk modding and Android theming.
Download APKTool_2.2.1.zip to a location of your choice, preferably the C:\ drive which will give you a folder to work from.
This thread will cover the following…
...in an easy to follow manner that will have you theming your own .apks in no time.
- Pulling files from your Phone.
- .apk De-compiling
- .apk Recompiling
- .apk Signing
- .apk Zipaligning
- Pushing Files to your Phone
- .9 images
- CWM zips
- .apk files when on your computer can be browsed like .zip files, so set your default zip program as the default program for handling apks which will makes things much easier when working with them.
- Download themes for your Phone, for other Phones too even and compare the res folders and screen shots of the theme as that will give you a good idea of what image you need to look for when modding your own apks.
- If all you want to do is change images then you don't even need to decompile, you can just drag and drop images in to an apk to replace a stock image so long as they have the same file name.
- For more complex mods like changing text colors and layouts (.xml files) and editing .9 png images then you will have to decompile.
Once your folder is setup, this is how mine currently looks...
…java and other tools installed, you need to get any apks you want to theme off your Phone and there are numerous ways to do this. To use adb commands to pull files to your computer we need to open a command prompt window in your Apktool folder. To do this simply hold the shift key then right click your mouse in the Apktool folder and select "open command window here", or just click on the included command prompt.bat.
I like to pull the whole system as soon as I'm rooted as it gives me a backup of stock files which I can then copy and deodex to be themed.
The command to pull your whole system is...
...you can choose something other than Z1C_368 as your folder name but it will create that folder in your Aktool folder. Alternatively, you can just download individual folders and the commands to do that are…Code:adb pull /system/ Z1C_368
...or to pull individual files...Code:adb pull /system/app/ Z1C_368 app adb pull /system/priv-app/ Z1C_368 priv-app adb pull /system/framework/ Z1C_368 framework
...so now you should have a APKTool_CMD folder with another folder within it and at least your app, priv-app and framework folders in that, which we can use for the next step.Code:adb pull /system/framework/framework-res.apk
“Deodexing is basically repackaging of these APKs in a certain way, such that they are reassembled into classes.dex files. By doing that, all pieces of an application package are put together back in one place, thus eliminating the worry of a modified APK conflicting with some separate odexed parts.” – What Is Odex And Deodex In AndroidThe tool I use is this - [Tool/Utility] JoelDroid Lollipop Batch Deodexer. I only deodex the apps I need to theme rather than run a fully deodexed rom.
Before proceeding you need to install your framework-res.apk and SemcGenericUxpRes.apk/twframework.apk, which will put two apks at C:\Users\your-pc-name\AppData\Local\apktool\framework which become the resources apktool uses to work its magic. The two commands to install your resource apks are...
Code:apktool if framework-res.apk apktool if SemcGenericUxpRes.apk
The command for decompiling is...
Code:apktool d framework-res.apk
...which will create a folder named like for like except for the .apk extension, and in this folder is where you'll find a couple of folders, the main one always being the res folder as it's in this folder that you'll find your images and xmls to edit. First glance of a decompiled res folder can be overwhelming, 596 folders in my framework, but we only need to focus on a handful in the majority of apks, and they are...
- drawable-xhdpi-v4 (drawable-xxhdpi-v4 depending on your device)
Once you've finished making your mods you need to recompile your apk using the following command...
Code:apktool b framework-res
...notice you only use the folder name here. If you get any errors here due to a bad edit apktool will usually tell you, in a roundabout way, you just need to read the code it throws up then retrace your steps, correct any bad edit and try again. If all goes smoothly you'll now have a new folder called dist, which contains your newly modified apk, but you can't use that just yet.
This is where 7-Zip or WinRar comes in to play. To sign stock system .apks we need to extract from the original apk the Android.Manifest.xml and META-INF folder, into the dist folder, then from the new apk extract the resources.arsc also into the dist folder, then drag and drop all three files back into the new apk, but NOTE, it's important that you choose store as the compression method, any other will break the apk, and if you replace a broken framework-res.apk you will BOOTLOOP!.
And signing is as simple as that. For 3rd party apps you can use the following command...
...notice the extra "S", the output name has to differ from the input name otherwise it will give an error, if done right it will create a new apk called ChromeS.apk, just remember to rename it back to the original before using it on your Phone.Code:java -jar signapk.jar testkey.x509.pem testkey.pk8 Chrome.apk ChromeS.apk
Signing For The Play Store
If you plan on Publishing a Theme or any other app/apk to the Play Store then using the above method to sign your apks just wont work because you need to give it an individual signature that is unique to you, thankfully @AndroidGraphix has written a great guide that will show you how, it's a bit more fidgety than the above method but simple enough to follow...
DISADVANTAGES OF UNALIGNED APKs; Quite understandably, situation would be reserved for unaligned application packages. Resource reading would be slow and memory usage would be on the higher end of the spectrum. It would also depend on how many unaligned applications are present. For example, if less number of applications with an unaligned home application, you’d see slower application launch times. This is the best case scenario. For a worst case scenario, having a number of unaligned applications will result in the system repeatedly starting and killing processes, struggling with lags and huge battery drain. - SOURCEMove your new signed apk into your APKTool folder, you can overwrite your original if you want, and use the following command...
Code:zipalign -v 4 framework-res.apk ZAframework-res.apk
...notice the ZA, zipaligning creates a new apk which has to be named different from the original, but use what you want, it'll need renaming again before you use it.
Rename your zipaligned apk to it's original name, then we need to mount the system before we can push direct to the system, and the command for that is...
...then we need to make sure we're pushing our apk to the right folder on the Phone, so for the framework-res it's...Code:adb remount
...yet for the SystemUI it's...Code:adb push D:\Xperia\Z1C_368\APKTOOL\framework-res.apk /system/framework
...if it's any one of those two then generally you need to reboot, so just type...Code:adb push D:\Xperia\Z1C_368\APKTOOL\SystemUI.apk /system/priv-app/SystemUI
...your Phone will restart and if you've done everything right your new mods should be displayed, happy days.Code:adb reboot
And that's it for how to use command prompt for modifying apks.
Use Linux or a Mac? See the following threads for more information on using apktool on other Operating Systems...
There are a number of programs you can use that automate the above processes, the three best/most popular GUI programs are...
- @Diamondbacks - Virtuous Ten Studio
- @Ticklefishs - [TOOL] Tickle My Android
- @BDFreaks - [Tool]Advanced ApkTool
...there are others but these are I what I tend to see referred to the most.
Restoring modified files if you mess up is easier and quicker with a CWM.zip, especially if you're modifying any framework apks! Just because an apk might build with no errors doesn't mean it won't give you any when installed to the Phone.
ClockworkMod Recovery zips are used for flashing individual files to whole ROMS and can be a life saver if you push a badly edited apk to your Phone that results in a bootloop. There's also a few programs that can assist you with that too, but if all you're doing is replacing a few files all you really need is a template, then it's just a matter of creating the Phones folder setup in the zip which is simply creating folders: system\app - system\media\audio etc then dragging and dropping your files into it. A basic understanding of the updater script is essential, a ROM.zip will more than likely wipe your Phone whereas a Theme.zip may just replace files but it might delete some also.[/QUOTE]
See below for a CWM.zip Template but first some helpful links...
This is my Restore_zip which is set up so you can add your own files to the framework and apps folders in the main system location, but you can easily add other folders to it if you want to add things like fonts, tones or anything else, just remember to add folders within folders if that's where the files you want to include are such as tones, media\audio\alarms for example.
- [Tool]easy flasher v4 (ur own update.zip)
- [Tool][Windows/Linux] Android Flashable Zip Creator
- ERRORS encountered in CWM Recovery
- [TUT] [for NOOB] editing updater-script for noobs!
- [TUTORIAL] Making Flashable ZIPs, EDIFY Script, ZIP Signing & Key Creation
If you navigate to META-INF\com\google\android you will find the updater-script which controls how the zip works and the text you see when you run it in CWM. As this zip is just for basically over writing files you don't need to edit anything except for a few lines...
...this is how I've edited it for my theme...
...so just edit it using Notepad++ to your liking.
When adding files to your zip add them using Normal as the Compression Method...
...Remember to remove the text files I've added otherwise they will be flashed to your folders too.
And that's pretty much it, the only way you're really going to learn is by reading then putting into practice what you've read and picking stuff up along the way, fortunately there are numerous guides for numerous mods and tools to help with a variety of tasks.
I hope after reading this explanation you're not left wondering what all the fuss is about, as it really is quite simple. I'll use the following two images, btn_default_pressed_holo.9 & status_bar_close_off.9 to illustrate the basic point of the borders...
Those borders control which part of the image is stretched (Left & Top) and where on the image content will be displayed (Right & Bottom). We'll use Draw 9-patch which is for editing the borders of decompiled .9pngs and lets you see how the image, when stretched, will be displayed on the Phone. Notice that the borders are different sizes, well that's because I only want/need a certain part of the image to stretch to get the desired look on the Phone.
...in the image below the left side shows the stretched image and desired effect should look on the Phone and the right side shows how it would look if I had edited the borders incorrectly or used none at all...
You can get away by not using a border but you would need to edit your image to specific dimensions to accommodate for the stretching, for example it might look fine for a simple OK button but it might not look right if you used the same .9png for a screen press which stretches the whole length of the screen, so you're better off just using them to begin with, plus you'll more than likely get errors down the line while decompiling or recompiling your apk.
The use of the border is made even more clear when you add text to an image. For my status_bar_close_off.9 I've filled all four sides of the image (except for an empty pixel in all four corners as that is the max amount of border you can use) as I need it to stretch the image just how I've edited it...
...but if I used the same sized left side border as the btn_default_pressed_holo.9 then this is how it would look on the Phone...
...which is obviously not the look I'm trying to achieve, as the left side border is causing the image to stretch incorrectly.
So that's basically it, depending on the image you're editing you can usually leave the borders alone but if you create one from scratch then you might need to play about with them if your images doesn't look quite right when used on the Phone. Here's a few links that will assist you further, especially the batch editing...
- How to edit .9.pngs
- [TUTO] Create your 9.png !
- [UTILITY][TOOL] 9patchPngSuite [Windows&Linux]
- How to draw NinePatch images with Photoshop - .9.PNG
- [TOOL][Multi-Platform][.9.png][ALL DPIs] Android Resizer Tool
- [Tutorial][Commentary] How To Batch Edit .9PNG/PNG Images
- [tool] 9compiler - batch process for themers android
Notable credits and thanks to @iBotPeaches and @Brut.all for making it possible with - Apktool