Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,737,799 Members 49,150 Now Online
XDA Developers Android and Mobile Development Forum

[Advice/Reference/Guide] The Definitive APK Modding & Theming Thread... {06/04/14}

Tip us?
 
XperienceD
Old
(Last edited by XperienceD; 6th April 2014 at 01:00 PM.)
#1  
XperienceD's Avatar
Senior Member - OP
Thanks Meter 2332
Posts: 7,193
Join Date: Jul 2010
Location: Here =)
Info 2 [Advice/Reference/Guide] The Definitive APK Modding & Theming Thread... {06/04/14}

Hi there.

I started this thread to try and simplify the process of modding and themeing Android, there are guides a plenty out there but I wanted to provide something a bit more complete and that covers a bit more than just the decompiling and building an apk to make a specific mod. For example, many guides leave out the zipalign process which you'll read below is quite important, so I've tried to write this as something I would have liked to have read when I first started to learn how to theme which in the long run might just save you a lot of time. You might have sat down to read this because you just want to change a few images, well if that's the case you don't need to bother with decompiling (unless it's a .9png), you can just drag and drop images into an apk, themeing can be that simple but obviously for more complex mods you are going to need to know how to decompile.


Quote:
Minimum Requirements:Theming Requirements:
  • Notepad++
  • Photoshop, GIMP or any other image editing software.
  • Patience.
If you get any java not found issues you need to set up the PATH for it in your Environment Variables, you can find a short Guide on how to HERE.

This is how mine looks...



...but it should be there after you install Java.

Code:
Select Code
C:\Program Files (x86)\Java\jre6\bin;
You may or may not need Android SDK but if you don't want to you can download this Apktool_2.9.03.zip that you can extract to a folder and place anywhere you want on your Computer (I recommend C:\Apktool_2.9.03), then you can run any adb, apktool, zipalign, Baksmali or signing command prompts from it. I've included a Text file with a list of the most common commands you'll need so you can just copy and paste them into the cmd window.
We'll start how I started which I recommend learning first because whilst using a program is great sometimes you can run into issues if your firmware or the program is updated which may cause a conflict, so if and/or when then does happen you'll know how to do things manually.

Quote:
Command Prompt Editing
Getting APKs To Your PC

You have two options here...

1. Use a file manager on your Phone to copy and paste files to your SD, then connect to your PC and use Windows Explorer to copy and paste to your desired location.

2. ADB. Open a command prompt window in the ADB folder (see below) and type/copy the following command...
Code:
Select Code
adb pull /system/app/ Original_apps
...which will pull the whole app folder to your computer and create a new folder called Original_apps, which you can then simply copy apps to the main folder ready to be modified (keep the Original_apps folder as a backup). Then do the same for your framework folder...
Code:
Select Code
adb pull /system/framework/ Original_framework
...want to pull your Phones whole system...
Code:
Select Code
adb pull /system/ Name_of_your_Phone
Further Information:

Installing Resources

For apktool to work you need to install the resources needed by your apks for de and recompiling, and those resources are other apks, the first apk regardless of your Phones manufacturer is going to be your framework-res.apk. The second apk you need is dependent on the manufacturer so you need to check your framework folder for another apk such as twframework.apk or SemcGenericUXpres.apk. Put those two apks in the Apktool_2.9.03 folder to begin with then open a command prompt window. The easiest way for me is holding the shift key then right clicking the mouse and selecting Open command window here, but if you're not able to do that then click your Start button and search for Command Prompt, then open it. You then need to point it to the relevant folder, so type cd then copy and paste the location of the folder (cd C:\Apktool_2.9.03) and hit enter.

The install commands are...

Code:
Select Code
apktool if framework-res.apk
...and for the second...

Code:
Select Code
apktool if SemcGenericUXpres.apk

Decompiling

Then once you've done that, the command to decompile is...

Code:
Select Code
apktool d framework-res.apk
...and so on for each individual apk you want to modify. A new folder will be created that is named after the apk you are modding, then you can edit xmls, smali files and .9png images. You'll notice that there are a lot more folders when you decompile an apk, that's normal as those folders are from the resources.arsc and where you'll find the xml files you may need to edit to make the changes you want.




Recompiling

When you've done editing it's time to recompile, using the command...

Code:
Select Code
apktool b -insert folder name-
...and if you've edited your files correctly then it should compile with no errors and create a folder named dist in the decompiled folder along with a new apk.




Signing


Before using the new apk you will need to sign it, to do this you need two things from the original apk, the AndroidManifest.xml and META-INF folder, simply open the apk then drag and drop them to the dist folder, and you also need to extract the resources.arsc from the newly built apk so drag and drop that into the dist folder too. Then drag and drop all 3 back into the new apk, in WinRar it will give you the option to choose the compression method, and it's vital that you choose Store because if you don't then you WILL BOOT LOOP after restarting your Phone!


Alternatively you can use the following command...

Code:
Select Code
java -jar signapk.jar testkey.x509.pem testkey.pk8 framework-res.apk.apk framework-resS.apk
...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 framework-resS.apk, just remember to rename it back to the original before using it on your Phone.

Zipaligning

Quote:
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.
- SOURCE

The command for zipaligning is...

Code:
Select Code
zipalign -v 4 framework-res.apk ZAframework-res.apk
...notice the "ZA", again that can be anything you want so long as it differs from the original name, I use ZA for an obvious reason, and again don't forget to rename it before using on your Phone.


Getting APKs Back To Your Phone

It's just a reversal of how you got them off your Phone to begin with, use a file explorer and put your apk on your SD Card, then move it to the Phones System folder and change it's permissions to rw-rw-r, then move and replace it with the original. Most apps should continue to work but you will most likely need to reboot for changes to take effect. Or you can use adb to push and install them.

If all the above has worked correctly you should now have your modified apk installed with the changes you've made displayed on your Phone.



Quote:
Signing For The Play Store
Quote:
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...

Quote:
Originally Posted by AndroidGraphix View Post

- Noobs guide to signing an APK with a Private Key

And that pretty much covers command prompt editing, I have a few programs setup for different reasons but this is really my preferred method, feels like you have a bit more control over what's going on.

Use Linux or a Mac? See the following threads for more information on using apktool on other Operating Systems...



Quote:
Program Editing
The following are programs that not only automate the process above but do a whole lot more too, so have a browse and give them a try.


Quote:
Theming & Modifying
This is where the fun begins, once you've got setup to decompile your apks then obviously you're going to want to mod them, so here's a few pointers to clear up what seems like a daunting process to new comers...

To start with you'll probably just want to start with replacing images, so if that's all you want to do for now then here's a quick tip, you DON'T need to decompile or sign your apks!, simply edit and rename your replacment images on you computer, preferably using the same or similar dimensions then drag and drop them back into the right folders in the apk, always using Store as the compression method, but PLEASE NOTE; .9 pngs can be replaced in the same way, but if you want to edit them in Photoshop for example then you need DO need to decompile them.

A few things to note...

Quote:
  • Smali code is real Developer territory, it's Matrix code to me , thankfully there's usually a guide to follow so you can simply copy and paste code without having to fully understand how it works, so don't fret about this.

    .9pngs are simply png images with a 1 pixel border. You can edit anything within that border but anything on or over will give errors when building. Unless you're creating an image from scratch then you can leave the border as it is, it's that simple! Sure there's a bit more to .9png editing but you can find a few links below that go into more detail about .9s and how they work.

    xmls are relatively simple and edited with Notepad++, much simpler to understand than smali and again, there's usually a guide to be found for most mods you may want to make yourself, but if you're feeling brave you can experiment with it when you have a basic understanding of how it works.

    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.


Quote:
Links, Links, Links

Reading Material
U.I Modding
Text...
Statusbar...
Misc...

Quote:
.9png Editing
I hope after reading this explanation you're 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...

Decompiled .9pngs:


Recompiled .9pngs:


Those borders control which part of the image is stretched, and 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...



Quote:
Xposed Framework

Quote:
Originally Posted by rovo89 View Post
In this thread, I'm presenting a framework which gives you the possibility to modify your ROM - without modifying any APK (developers) or flashing (users)!
...
Advantages:
  • No need to modify any APKs. This means:
  • No need to decompile, change things in smali, compile, sign, ...
  • It will work for odexed and deodexed ROMs.
  • Your mod is not bound to a specific version of the ROM. Unless there is a major change in the methods called for a certain functionality, your mod will continue to work even when you upgrade your ROM. Hell, it will probably even work with different ROMs.
  • Multiple mods can be installed at the same time, even if they change the same package. So you can use these battery icons and those quick toggles. Even hooking the same method twice is possible. Of course, this only works properly if the mods are not trying to do incompatible things.
  • It does its magic at runtime. That means you can implement options to let the user decide if they prefer the bikeshed to be painted in red or blue. Or you can have some extra logic for certain ROMs without building different mod versions.
  • It can be disabled easily without having to reflash.
  • It's open source.
- [FRAMEWORK ONLY!] Xposed - ROM modding without modifying APKs


Quote:
CyanogenMod Theme Chooser

Quote:
Originally Posted by Annex1 View Post
CM10.1-THEME_BOOK

Theme book was created by Annex and Ebs512. I've been asked alot of questions from how to get started? what do I need to get started? to What Graphics program do I use? etc. We made a real simple theme guide to help get you started. This is in Layman Terms.. Nothing fancy.

ONLY THING FANCY IS MY FLASH BOOK
YOU MUST HAVE A FLASH PLAYER INSTALLED TO VIEW THIS!!!

- [GUIDE] HOW TO THEME CM10.1 - Animated Book
The Following 63 Users Say Thank You to XperienceD For This Useful Post: [ Click to Expand ]
 
XperienceD
Old
(Last edited by XperienceD; 6th April 2014 at 01:00 PM.)
#2  
XperienceD's Avatar
Senior Member - OP
Thanks Meter 2332
Posts: 7,193
Join Date: Jul 2010
Location: Here =)
Default Useful Suff...

Quote:
Clockwork Mod (CWM)

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.
See below for a CWM.zip Template but first some helpful links...

This is my Theme_CMW_Template.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.

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 Ice Green ResMods theme...


...so just edit it using Notepad++ to your liking.

When adding files to your zip add them using Normal as the Compression Method...


...and that's pretty much it. Remember to remove the text files I've added otherwise they will be flashed to your folders too.



Quote:
Deodexing
The first thing I do when installing the latest firmware is Deodex it, mainly because having a deodexed ROM is easier to modify and you'll find a lot of the modifications that are are in the Themes And Apps sections require you to have a deodexed ROM. I use the dsixda's Android Kitchen to deodex and @pankaj88 has written a great guide here - [GUIDE] How to DEODEX stock ODEXED ROM using dsixda's kitchen - so you can do it yourself, or you can give the following tools a try.
Quote:
Originally Posted by philos64 View Post
The differences between Odex and Deodex Files...

In Android file system, applications come in packages with the extension .apk. These application packages, or APKs contain certain .odex files whose supposed function is to save space The odexed file structure works well as an optimization tool. Since these .odex files contain preliminary load information about each system app, the OS knows what to expect when it’s booting up, and consequently, loads all these apps faster.
On the other hand, it also makes hacking those applications difficult because a part of the coding has already been extracted to another location before execution.

For instance, on a non-rooted device you’ll find
system/app/Phone.apk ===> as well as ===> system/app/Phone.odex
- [GUIDE] - The differences between Odex and Deodex Files



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.


If you feel something more should be added to this thread please feel free to suggest it.



Credits & Thanks:

@_calum_
@Diamondback
@ebs512
@Rizal Lovins
@Ticklefish
@wanam
@rovo89
@Annex1
@ebs12

And not forgetting @iBotPeaches and @Brut.all for making it possible with - [UTIL][Feb 02 2013] Apktool v1.5.2 - a tool for reverse engineering apk files
The Following 18 Users Say Thank You to XperienceD For This Useful Post: [ Click to Expand ]
 
merin_83
Old
(Last edited by merin_83; 30th December 2012 at 11:50 PM.)
#3  
Member
Thanks Meter 2
Posts: 59
Join Date: Oct 2010
Hi Guys, I tried to do a small tweak to my ROM like just a color change to signal bars, as mentioned in the XDA university, but once the apk is pushed to system/app and restarted, it went to bootloop. I just changed the color of a image file using GIMP (as mentioned), then dint work, so just tried decompiling SystemUI.apk and then recompiling then folder without any changes and push to system folder, even that dint work, then i saw this thread, i tried the replacing the resources.arsc from old apk (opened using winrar, and dragged to my folder), the opened my new recompiled SystemUI.apk with winrar and added this resources.arsc file which i extracted with compression option as Store, then pushed it bcak to /system/app folder , every time i pushed i connected the phone in recovery mode. But still in bootloop, the moment i replace the apk with original one, it works, can some one pls point out what am I doing wrong ?

Hardware - I9100 - galaxy S2
ROM - JellyBAM 3.3
Kernel - Siyah 5.1+

thanks,
merin
 
trevd
Old
#4  
Recognized Developer
Thanks Meter 1154
Posts: 863
Join Date: Jul 2011
Location: Hull

 
DONATE TO ME
Quote:
Originally Posted by merin_83 View Post
Hi Guys, I tried to do a small tweak to my ROM like just a color change to signal bars, as mentioned in the XDA university, but once the apk is pushed to system/app and restarted, it went to bootloop. I just changed the color of a image file using GIMP (as mentioned), then dint work, so just tried decompiling SystemUI.apk and then recompiling then folder without any changes and push to system folder, even that dint work, then i saw this thread, i tried the replacing the resources.arsc from old apk (opened using winrar, and dragged to my folder), the opened my new recompiled SystemUI.apk with winrar and added this resources.arsc file which i extracted with compression option as Store, then pushed it bcak to /system/app folder , every time i pushed i connected the phone in recovery mode. But still in bootloop, the moment i replace the apk with original one, it works, can some one pls point out what am I doing wrong ?

Hardware - I9100 - galaxy S2
ROM - JellyBAM 3.3
Kernel - Siyah 5.1+

thanks,
merin
Hi There

Try using 7zip to update the apk instead of winrar. if that doesn't work, post your logcats,

Thanks
Don't forget to click Thanks If this post has helped. It gives me a little warm feeling inside

I WILL NOT RESPOND TO PM'S REQUESTING TECHNICAL SUPPORT


Development Work: Git Hub (https://github.com/trevd) | Huawei Dongle Support : Archos G9 , Asus Nexus 7 | adb extended version ( adbx ) [Always WIP] | The No Script, No Cygwin Standalone Boot Image Utility [WIP]
Other Work : Custom framework-res.apk resources.arsc using vendor overlays | [GUIDE][HACK]Cross Compiling for OSX on Linux with AOSP

A bug report without logs is just trivia!

Code:
adb shell dmesg ; adb logcat -b system -b radio -b events -b main
The Following User Says Thank You to trevd For This Useful Post: [ Click to Expand ]
 
merin_83
Old
#5  
Member
Thanks Meter 2
Posts: 59
Join Date: Oct 2010
Quote:
Originally Posted by trevd View Post
Hi There

Try using 7zip to update the apk instead of winrar. if that doesn't work, post your logcats,

Thanks
I will try that when i get back home today, and is there any option i need to set when using 7zip like store mode ?

thanks,
merin
 
merin_83
Old
(Last edited by merin_83; 31st December 2012 at 10:48 PM.)
#6  
Member
Thanks Meter 2
Posts: 59
Join Date: Oct 2010
hi, I tried with 7zip, 1st i just opened the recompiled zip with 7zip and dragged and dropped the resources file from original apk to this archive, and pushed to phone , still boot looping, and then tried to right click on the resource file from original apk, and selected add to 7zip archive, and changed the archive format to .zip/compression mode to Store and saved, then pushed to phone, still boot looping. So here i have attached the logcat, hope that is correct how i have done it, after rebooting the phone, i typed in consle logcat > log.txt and it was waiting for device, once in boot loop i did ctrl + c, then it exited. Please let me know if you need more info, do i need to attach my original SystemUI.apk and framework-res.apk ??

Happy New Year !!!


thanks,
merin
Attached Files
File Type: txt boot_log_i9100.txt - [Click for QR Code] (508.1 KB, 18 views)
 
cocodinc
Old
#7  
cocodinc's Avatar
Junior Member
Thanks Meter 5
Posts: 14
Join Date: Sep 2012
Location: Pontianak
Nice tuts I will try at home :thumbup:

Sent from my ST18i using xda premium
 
XperienceD
Old
#8  
XperienceD's Avatar
Senior Member - OP
Thanks Meter 2332
Posts: 7,193
Join Date: Jul 2010
Location: Here =)
Quote:
Originally Posted by merin_83 View Post
then i saw this thread, i tried the replacing the resources.arsc from old apk (opened using winrar, and dragged to my folder), the opened my new recompiled SystemUI.apk with winrar and added this resources.arsc file which i extracted with compression option as Store, then pushed it bcak to /system/app folder , every time i pushed i connected the phone in recovery mode. But still in bootloop, the moment i replace the apk with original one, it works, can some one pls point out what am I doing wrong ?
Hi.

Are you copying the AndroidManifest.xml and META-INF folder from the original to the new apk too?
 
merin_83
Old
(Last edited by merin_83; 2nd January 2013 at 05:17 AM.)
#9  
Member
Thanks Meter 2
Posts: 59
Join Date: Oct 2010
Actually i did the exact thing you said yesterday, when i found in the youtube, and it worked thank you!, but dint copy the resource file (but had some fc's when opening camera 1st time), may thats the reason,,, so i should copy AndroidManifest.xml and META-INF and resource file ?, and is this a standard procedure when we do a recompile every time ? or it varies with situation ?

thanks,
merin
 
sagar4all007
Old
#10  
Senior Member
Thanks Meter 10
Posts: 127
Join Date: Dec 2012
Location: mumbai
Hey hi guys....can any 1 tell me how to set boot image which i want hmmmmm

Sent from my HTC Desire HD using xda app-developers app

Tags
apktool, decompile, guide, recompile, theming
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes