How to edit XML/smali files in SystemUI.apk, the clean and easy way
For the last two days, i've been working on a fix for the "subsequent row resize bug" in MrLuje's port of Quick Settings. As every journey starts with the first step, i needed to properly decompile/recompile SystemUI.apk before being able to read/edit any XML/smali files it contains. Upon searching the Internet and through trial and error, i've managed to find a simple and clean way to do it (as any other way i've tried was leading to force closes).
- an enhanced text editor to edit the XML/smali files with (i use EditPlus);
- *Root Explorer or any other root explorer out there (i use ES File Explorer);
- any archive utility that can open .ZIP files (7zip, WinRAR etc.);
- Windows PC.
*Note: yes, you have to have root and S-OFF!
Step 1 (installing the tools):
- Download and install the latest Java Runtime Environment (APKtool needs at least ver. 1.6);
- Download "apktool-install-windows-r04-brut1.tar.bz2" and "apktool1.X.X.tar.bz2" and unzip them into a folder of your choosing;
Step 2 (preparing to decompile):
- In order to properly decompile/compile SystemUI.apk, you need to install the framework with APKtool;
- Open your root explorer of choice and navigate to /system/framework;
- Copy both "framework-res.apk" and "com.htc.resources.apk" to the SD card, then move them to your PC either via cable or via a software of your choosing (i use AirDroid);
- Put both "framework-res.apk" and "com.htc.resources.apk" into the folder where you've extracted APKtool;
- Open a command prompt (Windows button + R then type "cmd") and navigate to the folder where you have APKtool and the framework APKs;
- Type "apktool if framework-res.apk" then "apktool if com.htc.resources.apk". Your files will be processed and the required bits will be "installed" in "C:/Users/<yourusername>/apktool/framework";
- You can now delete both APKs from the folder.
Step 3 (proper decompilation):
- Copy your SystemUI.apk from "/system/app" to the folder where you've installed APKtool or copy the SystemUI.apk you wish to mod to the folder where you've extracted APKtool;
- Open a command prompt, navigate to the APKtool folder, type "apktool d SystemUI.apk" and wait 'till APKtool has finished. You should get no errors whatsoever and your APK should now be decompiled into "readable format" in "<yourAPKtoolfolder>/SystemUI". Edit the files you wish to edit with the text editor of your choosing. Beware, if you don't know what you're doing when messing with the XML/smali files, you're either gonna be stuck at the Quietly Brilliant logo or your Notification Bar will force close!
Step 4 (proper compilation):
- After making the desired changes in the decompiled APK, it's now time to compile it;
- Open a command prompt, navigate to the APKtool folder then type "apktool b SystemUI". Your APK will now be compiled to "<yourAPKtoolfolder>/SystemUI/dist" (again, you should get no errors whatsoever). DON'T USE IT YET!
- Being a system app, SystemUI.apk must first be signed with the proper keys else odds are it won't work. To do that, you need to rename the SystemUI.apk you're editing (the original one) into SystemUI.zip, open it with an archive manager then extract META-INF and AndroidManifest.xml to the "<yourAPKtoolfolder>/SystemUI/build/apk" folder, then run the "apktool b SystemUI" command again. Your APK will now be built with the proper keys included, and is now fully functional. You may "pick it up" from the "<yourAPKtoolfolder>/SystemUI/dist" folder;
- You can now flash the resulting (modded) APK to your device either via ADB or via a way of your choosing (.ZIP from recovery etc.).
Have fun and remember to always do a nandroid backup before messing with system files!