[GUIDE] Smali coding guide for beginners

Ticklefish

Recognized Themer
Oct 27, 2011
6,743
8,534
263
Hampshire, UK
Thanks sir!

But I have a problem with my framework-res.apk. I can not recompile it, even when I keep it original without editing anything after decompile it.
And I contact with iBotPeaches, who dev the apktool, ask him for test my framework-res.apk. And after he looked over that 20,000 line file and found 0 error within aapt, he said that "it is bad apk", so I can not recompile it to get a public string id's for use in smali.

So i think that I can change the public id of string name with the text in smali file.
What errors were you getting when you tried to recompile?

Sent from my C6603 using XDA Premium 4 mobile app
 

sieuan

Senior Member
Nov 12, 2012
665
476
0
What errors were you getting when you tried to recompile?

Sent from my C6603 using XDA Premium 4 mobile app
I don't think your gonna be able to override the string.xml using smali. Best to fix your compile issue. Framework should ALWAYS compile. What version of android are you using and is it sense or TouchWiz ?

Sent from my Nexus 7 2013 using ClearJellyed Tapatalk 4
Thanks all!

Here is the log when recompile it:

Code:
-------------------------------------------------------------------------- 
|24/05/2013 -- 16:30:32,25| 
-------------------------------------------------------------------------- 
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b05)
Java HotSpot(TM) Client VM (build 23.1-b03, mixed mode, sharing)
Could Not Find C:\Users\nguyen\Downloads\Compressed\ApkManagerWanam\place-apk-here-for-modding\../place-apk-here-for-modding/signedframework-res.apk
I: Loading resource table...
I: Loaded.
I: Decoding AndroidManifest.xml with resources...
I: Regular manifest package...
I: Decoding file-resources...
I: Decoding values */* XMLs...
I: Done.
I: Copying assets and libs...
W: Could not find sources
I: Checking whether resources has changed...
I: Building resources...
Exception in thread "main" brut.androlib.AndrolibException: brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 15, --target-sdk-version, 15, -F, C:\Users\nguyen\AppData\Local\Temp\APKTOOL9042299258849906978.tmp, -x, -S, C:\Users\nguyen\Downloads\Compressed\ApkManagerWanam\other\..\projects\framework-res.apk\res, -M, C:\Users\nguyen\Downloads\Compressed\ApkManagerWanam\other\..\projects\framework-res.apk\AndroidManifest.xml]
	at brut.androlib.Androlib.buildResourcesFull(Androlib.java:357)
	at brut.androlib.Androlib.buildResources(Androlib.java:282)
	at brut.androlib.Androlib.build(Androlib.java:205)
	at brut.androlib.Androlib.build(Androlib.java:176)
	at brut.apktool.Main.cmdBuild(Main.java:218)
	at brut.apktool.Main.main(Main.java:74)
Caused by: brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 15, --target-sdk-version, 15, -F, C:\Users\nguyen\AppData\Local\Temp\APKTOOL9042299258849906978.tmp, -x, -S, C:\Users\nguyen\Downloads\Compressed\ApkManagerWanam\other\..\projects\framework-res.apk\res, -M, C:\Users\nguyen\Downloads\Compressed\ApkManagerWanam\other\..\projects\framework-res.apk\AndroidManifest.xml]
	at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:349)
	at brut.androlib.Androlib.buildResourcesFull(Androlib.java:335)
	... 5 more
Caused by: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 15, --target-sdk-version, 15, -F, C:\Users\nguyen\AppData\Local\Temp\APKTOOL9042299258849906978.tmp, -x, -S, C:\Users\nguyen\Downloads\Compressed\ApkManagerWanam\other\..\projects\framework-res.apk\res, -M, C:\Users\nguyen\Downloads\Compressed\ApkManagerWanam\other\..\projects\framework-res.apk\AndroidManifest.xml]
	at brut.util.OS.exec(OS.java:89)
	at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:347)
	... 6 more
The system cannot find the file specified.
I also tried with apktool 1.5.2, apkmultitool,... and get the same error



And here is my framework, can you test recompile it for me, sir? Thanks!
http://www.mediafire.com/download/9pim59iz4vdpnx2/framework-res_mediatek-res_P700.zip


/// My phone is Lenovo P700, running ICS. And I can recompile or mod anything with custom rom 4.0.4. But with the stock rom (deodexed) 4.0.3, I can not recompile framework-res.apk, even when I keep it original without editing anything after decompile it
 

1brainsurgeon

Senior Member
Apr 7, 2011
3,027
6,250
0
Gig Harbor
How to edit over scrolling color in framework-res.apk? I use Virtuous ten studio to decompile. I couldn't get the smali code for framework-res.apk. Please help. Thanks.
Your oversroll glow should be a png in drawable-xhdpi, assuming you're on that version of android. There's no smali in framework...

Sent from my SPH-L710 using Xparent Purple Tapatalk 2
 

b10011

Member
Feb 8, 2012
27
23
0
Tampere
I edited one .smali file by adding there command (Log.d in smali syntax). The app works (Even that part, no crashes) but how can I read logs on real device?

v0 is value I want out, v7 is unused slot in that method:

invoke-static {v0}, Ljava/lang/String;->valueOf(I)Ljava/lang/String;
move-result-object v7
invoke-static {v7}, Landroid/util/Log;->d(Ljava/lang/String;)I

Thank you for answering!
 

Aciong.Aksaya

Senior Member
Aug 15, 2012
590
188
0
Mandaluyong
Hi I am on GIn2kitkat I just wanna know how to capitlized AM/PM in status bar. Erhany tipped that its a smali edit. I browse the smalis of SystemUI.apk and I think the edit should be done in clock.smali or clock$.smali if I am right but I am not sure what code will I write. Can I get a lil help?

Thanks...
 

marsui

Member
Mar 1, 2012
34
5
0
I read your guide thoroughly, I tried modifying a single character in a smali file from a decompiled APK (basically I changed a true 0x1 to a false 0x0), I got errors when trying to recompile.

So then I said, let me try to just decompile and recompile the APK without making any changes, just to make sure my modification was not the problem - and I cannot even do that without a slew of errors.

- I ran setup.bat as administrator, and pulled all framework files from my phone (HTC one - Sprint Variant)
- I ran script.bat and decompiled the apk
- leaving the script window open, I then tried option 12 to recompile a non-system apk (I tried option 11 too, even though what I am trying to recompile is a third party app and not a system apk)

Here is some of my log file (entire file is attached as it was too large)

Any ideas what I am doing wrong?

Code:
[B]log file is too large to completely paste in here, but here is the beginning[/B]
-------------------------------------------------------------------------- 
|Wed 03/05/2014 -- 12:41:22.05| 
-------------------------------------------------------------------------- 
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
-------------------------------------------------------------------------- 
|Wed 03/05/2014 -- 12:42:26.19| 
-------------------------------------------------------------------------- 
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
I: Baksmaling...
I: Loading resource table...
W: Skipping "android" package group
I: Loaded.
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: C:\Users\gbcaver\apktool\framework\1.apk
I: Loaded.
I: Regular manifest package...
I: Decoding file-resources...
W: Cant find 9patch chunk in file: "drawable-hdpi/dvr_filter_background.9.png". Renaming it to *.png.
I: Decoding values */* XMLs...
I: Done.
I: Copying assets and libs...
I: Checking whether sources has changed...
I: Smaling...
I: Checking whether resources has changed...
I: Building resources...
libpng error: Not a PNG file
ERROR: Failure processing PNG image D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\drawable-hdpi\subtitles.png

[B]Followed by tons of these kinds of error, of items not being defined:[/B]

D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\values\public.xml:266: error: Public symbol drawable/autowifi_back_button declared here is not defined.
D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\values\public.xml:4: error: Public symbol drawable/autowifi_back_button_states declared here is not defined.
D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\values\public.xml:267: error: Public symbol drawable/autowifi_backward declared here is not defined.

[B]Lastly an exception[/B]

Exception in thread "main" brut.androlib.AndrolibException: brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 8, -F, C:\Users\gbcaver\AppData\Local\Temp\APKTOOL7836915728672309959.tmp, -0, arsc, -I, C:\Users\gbcaver\apktool\framework\1.apk, -S, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res, -M, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\AndroidManifest.xml]
	at brut.androlib.Androlib.buildResourcesFull(Androlib.java:353)
	at brut.androlib.Androlib.buildResources(Androlib.java:281)
	at brut.androlib.Androlib.build(Androlib.java:209)
	at brut.androlib.Androlib.build(Androlib.java:177)
	at brut.apktool.Main.cmdBuild(Main.java:237)
	at brut.apktool.Main.main(Main.java:79)
Caused by: brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 8, -F, C:\Users\gbcaver\AppData\Local\Temp\APKTOOL7836915728672309959.tmp, -0, arsc, -I, C:\Users\gbcaver\apktool\framework\1.apk, -S, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res, -M, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\AndroidManifest.xml]
	at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:406)
	at brut.androlib.Androlib.buildResourcesFull(Androlib.java:334)
	... 5 more
Caused by: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 8, -F, C:\Users\gbcaver\AppData\Local\Temp\APKTOOL7836915728672309959.tmp, -0, arsc, -I, C:\Users\gbcaver\apktool\framework\1.apk, -S, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res, -M, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\AndroidManifest.xml]
	at brut.util.OS.exec(OS.java:89)
	at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:404)
	... 6 more
 

Attachments

djdarkknight96

Recognized Themer
Nov 2, 2009
16,838
46,956
263
Springfield
www.designrifts.com
I read your guide thoroughly, I tried modifying a single character in a smali file from a decompiled APK (basically I changed a true 0x1 to a false 0x0), I got errors when trying to recompile.

So then I said, let me try to just decompile and recompile the APK without making any changes, just to make sure my modification was not the problem - and I cannot even do that without a slew of errors.

- I ran setup.bat as administrator, and pulled all framework files from my phone (HTC one - Sprint Variant)
- I ran script.bat and decompiled the apk
- leaving the script window open, I then tried option 12 to recompile a non-system apk (I tried option 11 too, even though what I am trying to recompile is a third party app and not a system apk)

Here is some of my log file (entire file is attached as it was too large)

Any ideas what I am doing wrong?

Code:
[B]log file is too large to completely paste in here, but here is the beginning[/B]
-------------------------------------------------------------------------- 
|Wed 03/05/2014 -- 12:41:22.05| 
-------------------------------------------------------------------------- 
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
-------------------------------------------------------------------------- 
|Wed 03/05/2014 -- 12:42:26.19| 
-------------------------------------------------------------------------- 
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
I: Baksmaling...
I: Loading resource table...
W: Skipping "android" package group
I: Loaded.
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: C:\Users\gbcaver\apktool\framework\1.apk
I: Loaded.
I: Regular manifest package...
I: Decoding file-resources...
W: Cant find 9patch chunk in file: "drawable-hdpi/dvr_filter_background.9.png". Renaming it to *.png.
I: Decoding values */* XMLs...
I: Done.
I: Copying assets and libs...
I: Checking whether sources has changed...
I: Smaling...
I: Checking whether resources has changed...
I: Building resources...
libpng error: Not a PNG file
ERROR: Failure processing PNG image D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\drawable-hdpi\subtitles.png

[B]Followed by tons of these kinds of error, of items not being defined:[/B]

D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\values\public.xml:266: error: Public symbol drawable/autowifi_back_button declared here is not defined.
D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\values\public.xml:4: error: Public symbol drawable/autowifi_back_button_states declared here is not defined.
D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\values\public.xml:267: error: Public symbol drawable/autowifi_backward declared here is not defined.

[B]Lastly an exception[/B]

Exception in thread "main" brut.androlib.AndrolibException: brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 8, -F, C:\Users\gbcaver\AppData\Local\Temp\APKTOOL7836915728672309959.tmp, -0, arsc, -I, C:\Users\gbcaver\apktool\framework\1.apk, -S, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res, -M, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\AndroidManifest.xml]
at brut.androlib.Androlib.buildResourcesFull(Androlib.java:353)
at brut.androlib.Androlib.buildResources(Androlib.java:281)
at brut.androlib.Androlib.build(Androlib.java:209)
at brut.androlib.Androlib.build(Androlib.java:177)
at brut.apktool.Main.cmdBuild(Main.java:237)
at brut.apktool.Main.main(Main.java:79)
Caused by: brut.androlib.AndrolibException: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 8, -F, C:\Users\gbcaver\AppData\Local\Temp\APKTOOL7836915728672309959.tmp, -0, arsc, -I, C:\Users\gbcaver\apktool\framework\1.apk, -S, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res, -M, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\AndroidManifest.xml]
at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:406)
at brut.androlib.Androlib.buildResourcesFull(Androlib.java:334)
... 5 more
Caused by: brut.common.BrutException: could not exec command: [aapt, p, --min-sdk-version, 8, -F, C:\Users\gbcaver\AppData\Local\Temp\APKTOOL7836915728672309959.tmp, -0, arsc, -I, C:\Users\gbcaver\apktool\framework\1.apk, -S, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res, -M, D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\AndroidManifest.xml]
at brut.util.OS.exec(OS.java:89)
at brut.androlib.res.AndrolibResources.aaptPackage(AndrolibResources.java:404)
... 6 more

This is your issue:

ERROR: Failure processing PNG image D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\drawable-hdpi\subtitles.png

Fix the .9.png from the bad decompile.


Sent from my Nexus 5 with the KlearKat tapatalk app! ;)
 

marsui

Member
Mar 1, 2012
34
5
0
This is your issue:

ERROR: Failure processing PNG image D:\rsms_dev\Android\Development\APK-Multi-Tool-Stable-RELEASE\projects\com.optimum.unity.mobile-1.apk\res\drawable-hdpi\subtitles.png

Fix the .9.png from the bad decompile.


Sent from my Nexus 5 with the KlearKat tapatalk app! ;)
OK, I actually didn't realize that error was from the decompile and not the recompile. I didn't realize how flaky the decompile process is, but I guess it was kind of naive of me to think it would work without issues.

So I did some searching on this whole 9patch thing, and I am utterly confused....are you saying I have to open that png file in some image editor and do something with it? I can't believe the problem is an image and not code lol, this is my first time decompiling so I might just not understand what it means to fix a png file.
 

djdarkknight96

Recognized Themer
Nov 2, 2009
16,838
46,956
263
Springfield
www.designrifts.com
OK, I actually didn't realize that error was from the decompile and not the recompile. I didn't realize how flaky the decompile process is, but I guess it was kind of naive of me to think it would work without issues.

So I did some searching on this whole 9patch thing, and I am utterly confused....are you saying I have to open that png file in some image editor and do something with it? I can't believe the problem is an image and not code lol, this is my first time decompiling so I might just not understand what it means to fix a png file.

Apktool decodes .9 images with patch lines that allow the image to stretch and guide where text and icons are located within that image. On decode that errors because it was not properly coded. To fix it you can use the drawpatch editor in the android Sdk or use an image decompiler like Xultimate to just decode that image and see if a copy of it from the original apk decodes right. Then replace the bad one with that one. I've used both methods to fix this error.

Sent from my N7 using a KlearKat tapatalk
 
  • Like
Reactions: DuB and marsui

marsui

Member
Mar 1, 2012
34
5
0
Apktool decodes .9 images with patch lines that allow the image to stretch and guide where text and icons are located within that image. On decode that errors because it was not properly coded. To fix it you can use the drawpatch editor in the android Sdk or use an image decompiler like Xultimate to just decode that image and see if a copy of it from the original apk decodes right. Then replace the bad one with that one. I've used both methods to fix this error.

Sent from my N7 using a KlearKat tapatalk
Thanks that's very useful, will try both methods
 

marsui

Member
Mar 1, 2012
34
5
0
So I've successfully decompiled the apk I want to mod (the optimum app) I found the smali code I wanted to modify, modified it, and it seems to work. (I'm trying to remove the restriction that the optimum app will not allow you to have an HDMI cable [mhl] connected)

So the app starts up, and its mirroring great (usually it would have error'd out saying that the app does not support HDMI output). I'm all excited until I go to actually stream one of the channels, and the app just completely reboots =(

So II decided just to decompile, recompile and sign without my code modifications, and it does the same thing. I have no idea what could possible be causing this to occur specifically but its obviously not decompiling perfectly. Is this THE goto decompiler for APKs? Do APKs generally decompile/recompile without issues?

I'm thinking I just have to pour over the code and fix it, but its probably going to be quite difficult without really being able to debug what is going on.
 

djdarkknight96

Recognized Themer
Nov 2, 2009
16,838
46,956
263
Springfield
www.designrifts.com
So I've successfully decompiled the apk I want to mod (the optimum app) I found the smali code I wanted to modify, modified it, and it seems to work. (I'm trying to remove the restriction that the optimum app will not allow you to have an HDMI cable [mhl] connected)

So the app starts up, and its mirroring great (usually it would have error'd out saying that the app does not support HDMI output). I'm all excited until I go to actually stream one of the channels, and the app just completely reboots =(

So II decided just to decompile, recompile and sign without my code modifications, and it does the same thing. I have no idea what could possible be causing this to occur specifically but its obviously not decompiling perfectly. Is this THE goto decompiler for APKs? Do APKs generally decompile/recompile without issues?

I'm thinking I just have to pour over the code and fix it, but its probably going to be quite difficult without really being able to debug what is going on.
Try moving just the classes.dex to an untouched apk. Delete the META-INF folder and resign the apk.

If that doesn't work try moving the dex file but leave the signature and move the apk to system/app.

Those tricks work theming Google apps so it might work in your case.

Sent from my Nexus 5 with the KlearKat tapatalk app! ;)
 
  • Like
Reactions: marsui

marsui

Member
Mar 1, 2012
34
5
0
Try moving just the classes.dex to an untouched apk. Delete the META-INF folder and resign the apk.

If that doesn't work try moving the dex file but leave the signature and move the apk to system/app.

Those tricks work theming Google apps so it might work in your case.

Sent from my Nexus 5 with the KlearKat tapatalk app! ;)
That sounds like a good idea, so just to be clear, I'm going to take the Original APK, move my recompiled classes.dex into the original apk (using a tool like winrar)?

Edit: Well I tried both, and the app crashes instantly with both methods =(

Thank you though
 
Last edited:

mikk_11

Senior Member
Mar 6, 2014
108
15
0
I tried to mod my SuystemUI of my Z1c.
When I want to compile it again Smali Manager writes this Error:
Code:
..\project\SystemUI.apk_classout\com\android\systemui\statusbar\phone\PhoneStatusBar.smali[12843,3] Error for input '.parameter': Invalid directive
..\project\SystemUI.apk_classout\com\android\systemui\statusbar\phone\PhoneStatusBar.smali[12843,14] mismatched input '"event"' expecting END_METHOD_DIRECTIVE
What do I wrong?
 
Last edited:

kingdj

Senior Member
Mar 18, 2013
398
220
0
sweet city,pangkalanbun
Damn, well that stinks. :-\

Sent from my Nexus 5 with the KlearKat tapatalk app! ;)
hai mate i have few question hope you can help me.

i try to mod my system ui for that i have to modified my phone statusbar smali but i get confuse when some line i search can.t be found on my phone statusbar smali. here the code im looking for.

method private setpile layer

) iput-object v0, p0, Lcom/android/systemui/statusbar/phone/PhoneStatusBar;->mMediaEjectBroadcastReceiver:Landroid/content/BroadcastReceiver;

.method private showAlwaysAskOrInternetCall(J)V
.method private setIntruderAlertVisibility(Z)V


anyway im on cm11 .maybe its override by new code. but i can.t find it please help which code is override that code on cm11.