[TUTORIAL][SENSE]How to edit XML/smali files in SystemUI.apk, the clean and easy way

Search This thread

TVTV

Senior Member
Jan 5, 2010
336
140
Bucharest
Hello.

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).

Required software:

- APKtool;
- 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!
 
Last edited:

TVTV

Senior Member
Jan 5, 2010
336
140
Bucharest
Example: how to fix the "subsequent row bug" in MrLuje's port of Quick Settings.

1) Decompile SystemUI.apk;
2) Go to "<yourAPKtoolfolder>/SystemUI/res/layout/" and open "status_bar_preference.xml" with the text editor of your choice;
3) Find the line below:
Code:
<com.android.systemui.statusbar.preference.PreferenceView android:orientation="vertical" android:background="@drawable/status_bar_item_background" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="[B][COLOR="Red"]wrap_content[/COLOR][/B]" [...]
4) Edit the value of "layout_height=" from "wrap_content" to "48.5dip";
5) Recompile the APK, push to "/system/app" and voila, bug gone.

For those who don't know/haven't noticed the bug i'm talking about, if you very quickly touch a setting's row in the Quick Settings panel, the subsequent row becomes enlarged and ruins the layout. My finding is if you force the rows to have a fixed value instead of leaving their value "at the discretion of Android", the bug disappears.

Explanation: "dip" means "density-independent pixel". As the Android Developers page defines it:
A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.

The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
 
Last edited:

wally80

Senior Member
Sep 24, 2008
225
101
Vienna
Hi TVTV,

Nice tutorial! Thanks.
Regarding the example, I'm going to point to your new systemui in the OP of my ROM, if you don't mind.

I am currently using the good old superfast coolbravoS_v10 and the small bug you noticed is also there.

Anyway, I would have never noticed it, if you wouldn't have told in my rom thread! :)
You are a real perfectionist!

Cheers,
Wally
 

TVTV

Senior Member
Jan 5, 2010
336
140
Bucharest
Hello, Wally!

Sure, point away, no problem! :)

About the perfectionism, what can i say? When you work some long years in the business of desktop publishing, you unwillingly develop an OCD-like attention to details. :)
 

raafaell

Senior Member
Feb 3, 2011
384
539
great!

That's why the app I modded wasn't working, thanks sir, ill try it when I get home, I was using the com.framework-res.apk only, thanks again.
 
Last edited:

TVTV

Senior Member
Jan 5, 2010
336
140
Bucharest
Glad to help! :)

When decompiling apps, if you are missing some required framework file/s (some packages have more than one dependency), APKTool will return this error: "Can't find framework resources for package of id: [PACKAGE ID]. You must install proper framework files, see project website for more info". In this case you must locate the package with the required ID and install it as framework.

How to find out package IDs? Well, i do it by trial and error. I have noticed that when you install a package as framework, APKTool installs it as a number (e.g. if you install com.htc.resources.apk, APKTool will say "I: Framework installed to: C:\Users\*\apktool\framework\2.apk; if you install SystemUI.apk, it will install it as 127.apk etc.). That number represents the package's ID. I just keep installing various packages from /system until i find the one of the required ID. If anybody knows of an easier way, please do tell. :)
 

thebarsteward

Senior Member
Apr 7, 2010
161
31
Birmingham
Great tutorial, thanks. "apktool if framework-res.apk" runs OK, however when I run "apktool if com.htc.resources.apk" I get the following errors:

HTML:
C:\APK Tools>apktool if com.htc.resources.apk
W: Config flags size > 36. Exceeding bytes: 0x1000000.
W: Invalid config flags detected: dimen-hdpi-ERR0
W: Config flags size > 36. Exceeding bytes: 0x2000000.
W: Invalid config flags detected: dimen-hdpi-ERR1
W: Config flags size > 36. Exceeding bytes: 0x4000000.
W: Invalid config flags detected: dimen-hdpi-ERR2
W: Config flags size > 36. Exceeding bytes: 0x5000000.
W: Invalid config flags detected: dimen-hdpi-ERR3
W: Config flags size > 36. Exceeding bytes: 0x1000000.
W: Invalid config flags detected: dimen-zh-rCN-hdpi-ERR4
W: Config flags size > 36. Exceeding bytes: 0x2000000.
W: Invalid config flags detected: dimen-zh-rCN-hdpi-ERR5
W: Config flags size > 36. Exceeding bytes: 0x4000000.
W: Invalid config flags detected: dimen-zh-rCN-hdpi-ERR6
W: Config flags size > 36. Exceeding bytes: 0x5000000.
W: Invalid config flags detected: dimen-zh-rCN-hdpi-ERR7
W: Config flags size > 36. Exceeding bytes: 0x1000000.
W: Invalid config flags detected: dimen-zh-rTW-hdpi-ERR8
W: Config flags size > 36. Exceeding bytes: 0x2000000.
W: Invalid config flags detected: dimen-zh-rTW-hdpi-ERR9
W: Config flags size > 36. Exceeding bytes: 0x4000000.
W: Invalid config flags detected: dimen-zh-rTW-hdpi-ERR10
W: Config flags size > 36. Exceeding bytes: 0x5000000.
W: Invalid config flags detected: dimen-zh-rTW-hdpi-ERR11
W: Config flags size > 36. Exceeding bytes: 0x1000000.
W: Invalid config flags detected: integer-hdpi-ERR12
W: Config flags size > 36. Exceeding bytes: 0x2000000.
W: Invalid config flags detected: integer-hdpi-ERR13
W: Config flags size > 36. Exceeding bytes: 0x4000000.
W: Invalid config flags detected: integer-hdpi-ERR14
W: Config flags size > 36. Exceeding bytes: 0x5000000.
W: Invalid config flags detected: integer-hdpi-ERR15
I: Framework installed to: C:\Users\xxxxxxx\apktool\framework\2.apk

I then run "apktool d systemUI.apk" and I get the following errors

HTML:
I: Baksmaling...
testI: Loading resource table...
I: Loaded.
I: Loading resource table from file: C:\Users\xxxxxxx\apktool\framework\1.apk
I: Loaded.
I: Decoding file-resources...
I: Loading resource table from file: C:\Users\xxxxxxx\apktool\framework\2.apk
W: Config flags size > 36. Exceeding bytes: 0x1000000.
W: Invalid config flags detected: dimen-hdpi-ERR0
W: Config flags size > 36. Exceeding bytes: 0x2000000.
W: Invalid config flags detected: dimen-hdpi-ERR1
W: Config flags size > 36. Exceeding bytes: 0x4000000.
W: Invalid config flags detected: dimen-hdpi-ERR2
W: Config flags size > 36. Exceeding bytes: 0x5000000.
W: Invalid config flags detected: dimen-hdpi-ERR3
W: Config flags size > 36. Exceeding bytes: 0x1000000.
W: Invalid config flags detected: dimen-zh-rCN-hdpi-ERR4
W: Config flags size > 36. Exceeding bytes: 0x2000000.
W: Invalid config flags detected: dimen-zh-rCN-hdpi-ERR5
W: Config flags size > 36. Exceeding bytes: 0x4000000.
W: Invalid config flags detected: dimen-zh-rCN-hdpi-ERR6
W: Config flags size > 36. Exceeding bytes: 0x5000000.
W: Invalid config flags detected: dimen-zh-rCN-hdpi-ERR7
W: Config flags size > 36. Exceeding bytes: 0x1000000.
W: Invalid config flags detected: dimen-zh-rTW-hdpi-ERR8
W: Config flags size > 36. Exceeding bytes: 0x2000000.
W: Invalid config flags detected: dimen-zh-rTW-hdpi-ERR9
W: Config flags size > 36. Exceeding bytes: 0x4000000.
W: Invalid config flags detected: dimen-zh-rTW-hdpi-ERR10
W: Config flags size > 36. Exceeding bytes: 0x5000000.
W: Invalid config flags detected: dimen-zh-rTW-hdpi-ERR11
W: Config flags size > 36. Exceeding bytes: 0x1000000.
W: Invalid config flags detected: integer-hdpi-ERR12
W: Config flags size > 36. Exceeding bytes: 0x2000000.
W: Invalid config flags detected: integer-hdpi-ERR13
W: Config flags size > 36. Exceeding bytes: 0x4000000.
W: Invalid config flags detected: integer-hdpi-ERR14
W: Config flags size > 36. Exceeding bytes: 0x5000000.
W: Invalid config flags detected: integer-hdpi-ERR15
I: Loaded.
I: Decoding values*/* XMLs...
I: Done.
I: Copying assets and libs...

but everything seems to be OK in "<yourAPKtoolfolder>/SystemUI" and I can edit the resulting xml files OK. :confused:

Im running JRE 1.7.0.20
 

TVTV

Senior Member
Jan 5, 2010
336
140
Bucharest
Hmm. I can see that you're using an ICS ROM w/Sense 3.6. APKTool has not yet been updated to work with the ICS SDK, that's why you're getting those error messages. Thus it shouldn't be used for editing ICS-related packages. Hang tight, the guys behind APKTool will certainly update it soon.

L.E. - There seems to be a workaround for editing SystemUI on ICS, but there are no guarantees as to the outcome. Take a look at this thread and read the OP and NilsP's posts.
 
Last edited:
  • Like
Reactions: thebarsteward

shukyc19

New member
Feb 10, 2012
1
0
Greate tutorial

Hi,

Very useful tutorial, any chance you know how can I
Change systemui.apk to add more notification tuggles?

---------- Post added at 11:00 AM ---------- Previous post was at 10:55 AM ----------

Hi,

Very useful tutorial, any chance you know how can I Change systemui.apk to add more notification tuggles?
 

TVTV

Senior Member
Jan 5, 2010
336
140
Bucharest
Haven't tried it myself, but orkillakilla has put together a great tutorial on this subject. You can find it here. Beware though, it's quite an enterprise! :)
 

bigshotrob22

Senior Member
Oct 3, 2010
1,306
712
Tucson
I get this error when compiling

C:\apktool>apktool b SystemUI
I: Checking whether sources has changed...
I: Smaling...
I: Checking whether resources has changed...
I: Building resources...
invalid resource directory name: C:\apktool\SystemUI\res/drawable-sw600dp-xhdpi
invalid resource directory name: C:\apktool\SystemUI\res/layout-sw600dp
invalid resource directory name: C:\apktool\SystemUI\res/values-sw600dp
invalid resource directory name: C:\apktool\SystemUI\res/values-sw600dp-port
invalid resource directory name: C:\apktool\SystemUI\res/values-sw720dp
invalid resource directory name: C:\apktool\SystemUI\res/values-sw720dp-port
Exception in thread "main" brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, -F, C:\Users\robertra\AppData\Local\Temp\APK
TOOL4611948545260478965.tmp, -I, C:\Users\robertra\apktool\framework\1.apk, -S, C:\apktool\SystemUI\res, -M, C:\apktool\SystemUI\AndroidManifest.xml]
at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:193)
at brut.androlib.Androlib.buildResourcesFull(Androlib.java:301)
at brut.androlib.Androlib.buildResources(Androlib.java:248)
at brut.androlib.Androlib.build(Androlib.java:171)
at brut.androlib.Androlib.build(Androlib.java:154)
at brut.apktool.Main.cmdBuild(Main.java:182)
at brut.apktool.Main.main(Main.java:67)
Caused by: brut.common.BrutException: could not exec command: [aapt, p, -F, C:\Users\robertra\AppData\Local\Temp\APKTOOL4611948545260478965.tmp, -I, C:\Users\robertr
a\apktool\framework\1.apk, -S, C:\apktool\SystemUI\res, -M, C:\apktool\SystemUI\AndroidManifest.xml]
at brut.util.OS.exec(OS.java:83)
at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:191)
... 6 more
 

Dhiraj

Senior Member
Dec 28, 2010
2,596
341
I am new in development section. I found system apps are dependend on system-res.apk . If I like to port a system apps from one Rom to some other Rom , what steps I have to follow ....if possible pl. guide me briefly.
 

ezfilez

Member
Apr 15, 2010
42
5
What ROM are you using, please?

I get the same error ... I've tried everything ... same thing ... i can decompile ok

Thanks

---------- Post added at 02:24 AM ---------- Previous post was at 02:23 AM ----------

I get this error when compiling

C:\apktool>apktool b SystemUI
I: Checking whether sources has changed...
I: Smaling...
I: Checking whether resources has changed...
I: Building resources...
invalid resource directory name: C:\apktool\SystemUI\res/drawable-sw600dp-xhdpi
invalid resource directory name: C:\apktool\SystemUI\res/layout-sw600dp
invalid resource directory name: C:\apktool\SystemUI\res/values-sw600dp
invalid resource directory name: C:\apktool\SystemUI\res/values-sw600dp-port
invalid resource directory name: C:\apktool\SystemUI\res/values-sw720dp
invalid resource directory name: C:\apktool\SystemUI\res/values-sw720dp-port
Exception in thread "main" brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, -F, C:\Users\robertra\AppData\Local\Temp\APK
TOOL4611948545260478965.tmp, -I, C:\Users\robertra\apktool\framework\1.apk, -S, C:\apktool\SystemUI\res, -M, C:\apktool\SystemUI\AndroidManifest.xml]
at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:193)
at brut.androlib.Androlib.buildResourcesFull(Androlib.java:301)
at brut.androlib.Androlib.buildResources(Androlib.java:248)
at brut.androlib.Androlib.build(Androlib.java:171)
at brut.androlib.Androlib.build(Androlib.java:154)
at brut.apktool.Main.cmdBuild(Main.java:182)
at brut.apktool.Main.main(Main.java:67)
Caused by: brut.common.BrutException: could not exec command: [aapt, p, -F, C:\Users\robertra\AppData\Local\Temp\APKTOOL4611948545260478965.tmp, -I, C:\Users\robertr
a\apktool\framework\1.apk, -S, C:\apktool\SystemUI\res, -M, C:\apktool\SystemUI\AndroidManifest.xml]
at brut.util.OS.exec(OS.java:83)
at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:191)
... 6 more


I get the same error ... I've tried everything ... same thing ... i can decompile ok

Thanks
 

TVTV

Senior Member
Jan 5, 2010
336
140
Bucharest
If you're trying to decompile/recompile an ICS ROM, that may be your problem. What ROM are you using, please?
 

TVTV

Senior Member
Jan 5, 2010
336
140
Bucharest
Hmm, you ROM appears to be 2.3.6-based, so there shouldn't be any problems in decompiling its APKs. I'm not knowledgeable enough to tell you exactly if the fact that it's deODEXed represents the problem, but i believe that might be it.
 
Last edited:

winsettr

Senior Member
Oct 26, 2010
373
66
I had been trying to rearrange the softkeys in CM7 for Nook Tablet, this method finally worked. Thanks!

Sent from my Nexus S using Tapatalk
 

Top Liked Posts

  • There are no posts matching your filters.
  • 29
    Hello.

    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).

    Required software:

    - APKtool;
    - 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!
    3
    Example: how to fix the "subsequent row bug" in MrLuje's port of Quick Settings.

    1) Decompile SystemUI.apk;
    2) Go to "<yourAPKtoolfolder>/SystemUI/res/layout/" and open "status_bar_preference.xml" with the text editor of your choice;
    3) Find the line below:
    Code:
    <com.android.systemui.statusbar.preference.PreferenceView android:orientation="vertical" android:background="@drawable/status_bar_item_background" android:focusable="true" android:clickable="true" android:layout_width="fill_parent" android:layout_height="[B][COLOR="Red"]wrap_content[/COLOR][/B]" [...]
    4) Edit the value of "layout_height=" from "wrap_content" to "48.5dip";
    5) Recompile the APK, push to "/system/app" and voila, bug gone.

    For those who don't know/haven't noticed the bug i'm talking about, if you very quickly touch a setting's row in the Quick Settings panel, the subsequent row becomes enlarged and ruins the layout. My finding is if you force the rows to have a fixed value instead of leaving their value "at the discretion of Android", the bug disappears.

    Explanation: "dip" means "density-independent pixel". As the Android Developers page defines it:
    A virtual pixel unit that you should use when defining UI layout, to express layout dimensions or position in a density-independent way.

    The density-independent pixel is equivalent to one physical pixel on a 160 dpi screen, which is the baseline density assumed by the system for a "medium" density screen. At runtime, the system transparently handles any scaling of the dp units, as necessary, based on the actual density of the screen in use. The conversion of dp units to screen pixels is simple: px = dp * (dpi / 160). For example, on a 240 dpi screen, 1 dp equals 1.5 physical pixels. You should always use dp units when defining your application's UI, to ensure proper display of your UI on screens with different densities.
    1
    Hmm. I can see that you're using an ICS ROM w/Sense 3.6. APKTool has not yet been updated to work with the ICS SDK, that's why you're getting those error messages. Thus it shouldn't be used for editing ICS-related packages. Hang tight, the guys behind APKTool will certainly update it soon.

    L.E. - There seems to be a workaround for editing SystemUI on ICS, but there are no guarantees as to the outcome. Take a look at this thread and read the OP and NilsP's posts.