[DEV][Work-In-Progress][C#] Direct Smali to Java converter

Search This thread

Ricky Divjakovski

Recognized Developer / Inactive RC
Feb 4, 2013
5,291
7,687
28
Sydney
Hey man! :)

I've been working on merging your new changes and analyzing them. Thanks a lot! you sure did a lot of work there that was really needed, specially with types and arrays. Nice!. I'll see what can I come up with next :).

I tried to use VS2012's IDE but man it was hell, they don't have too much documentation, and WPF is kind of a new tech for me so I didn't want to spend much time on that, but hey it's good to know. I see, well, if you ever manage to improve your IDE somehow with some kind of Amend<->Edify converter and Amend syntax highlighting it would be great, as I'm kinda needing a recovery script IDE sometimes xD

I just tipped off XDA portal, hopefully they post it and it attracts more contributors!
 

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
Hi darkguy2008 !

I'm really interested in your project and wanted to know : do you think we should add an UI to it ? I'm actually working on that (for personnal use ;) )
If you are interested in what I'm doing reply me :)

Btw really nice project :cyclops:
Hello cadialex! :)

Well, when I created the project I didn't think it would need an IDE, as what I'm attempting to do is what smali does with jar/apk files, just plain converting them and outputting a whole converted folder. But hey, I wouldn't mind to check what are you working on, who knows, I could like it xD.

Thanks for the support!
 

Fenny

Inactive Recognized Developer
Jun 28, 2007
1,386
2,073
F(x)tec Pro1-X
Google Pixel 6 Pro
Hey man! :)

I've been working on merging your new changes and analyzing them. Thanks a lot! you sure did a lot of work there that was really needed, specially with types and arrays. Nice!. I'll see what can I come up with next :).

This is a good example of the sort of thing that should parse properly now.
Notice that each parameter is just sort of crammed in there:
Code:
.method static <init>([BLjava/lang/String;Ljava/lang/String;)V

If anyone is having trouble following my changes it might be important to realize this:
'L' = a java object is about to be listed followed by a semicolon, '[' = an array of whatever the next letter is (this can include 'L') :) so this function definition would be void (byte[], string, string)

Good stuff is happening here. :F Are you idling any of the IRC channels at the moment? It might be easier to chat there.
 
Last edited:
  • Like
Reactions: darkguy2008

yashade2001

Senior Member
Sep 29, 2012
814
1,150
Ankara
Hey man! :)

I've been working on merging your new changes and analyzing them. Thanks a lot! you sure did a lot of work there that was really needed, specially with types and arrays. Nice!. I'll see what can I come up with next :).

I tried to use VS2012's IDE but man it was hell, they don't have too much documentation, and WPF is kind of a new tech for me so I didn't want to spend much time on that, but hey it's good to know. I see, well, if you ever manage to improve your IDE somehow with some kind of AmendEdify converter and Amend syntax highlighting it would be great, as I'm kinda needing a recovery script IDE sometimes xD

The alpha version of editor with syntax highlighting is ready, check out my google plus post.
 
Last edited:

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
This is a good example of the sort of thing that should parse properly now.
Notice that each parameter is just sort of crammed in there:
Code:
.method static <init>([BLjava/lang/String;Ljava/lang/String;)V
If anyone is having trouble following my changes it might be important to realize this:
'L' = a java object is about to be listed followed by a semicolon, '[' = an array of whatever the next letter is (this can include 'L') :) so this function definition would be void (byte[], string, string)

Good stuff is happening here. :F Are you idling any of the IRC channels at the moment? It might be easier to chat there.
Hey :)

Good to know! I kind of realized that looking at each of your commits lol. I tried to do my best at merging all the changes, I hope it's all good :)

By the way, shouldn't that line be:
Code:
.method static <init>([B;Ljava/lang/String;Ljava/lang/String;)V
with the semicolon between B and L ? that makes more sense to me xD

Also, it would be great, but what is the server and/or channels? I usually don't idle, but I can create a channel somewhere where we can exchange ideas a bit faster if you'd like :D.

The alpha version of editor with syntax highlighting is ready, check out my google plus post.
Oh, I rarely use G+, but I shall check your project and report back :D

BTW you should make a gui for this, i can help to you. will fork your repo.
About the GUI, well, I would consider it something secondary, and personally I'm very, very picky when it comes to GUIs, as I use the Microsoft guidelines mixed with some of mine when creating UIs, but hey, fork away and show me what you'll do. However, bear in mind that decompiling smali files requires to read other referenced smali files (either by being children of other smali files or referencing anonymous classes and such) so a JD-GUI GUI (lol) wouldn't be ideal in my opinion. A small GUI though, for choosing a folder and setting some options or displaying a single file or two wouldn't be too bad though. I'm open to suggestions, fork away and show me when you have something :)
 

Fenny

Inactive Recognized Developer
Jun 28, 2007
1,386
2,073
F(x)tec Pro1-X
Google Pixel 6 Pro
Hey :)

Good to know! I kind of realized that looking at each of your commits lol. I tried to do my best at merging all the changes, I hope it's all good :)

By the way, shouldn't that line be:
Code:
.method static <init>([B;Ljava/lang/String;Ljava/lang/String;)V
with the semicolon between B and L ? that makes more sense to me xD

Also, it would be great, but what is the server and/or channels? I usually don't idle, but I can create a channel somewhere where we can exchange ideas a bit faster if you'd like :D.

Well, there is #panicroom on Freenode. Lots of good peeps there. If you want to make one, we probably ought to do it on freenode, most of the Android dev stuff from XDA is on there after all.

And probably it would make more sense for smali to have some sort of separator character to split arguments in function signatures, but It totally doesn't. All it has is the semicolon to indicate the end of a type 'L'

*edit* Uhhhh, did you delete your github account or something? I am getting a 404.
 

Goldie

Inactive Recognized Developer
Oct 16, 2010
9,965
22,760
A small GUI though, for choosing a folder and setting some options or displaying a single file or two wouldn't be too bad though.

For me personally this would be great. I do all my mods in smali and know little to no java. However being able to convert can be really useful and a simple GUI to do this on a file or two would be great :)


Sent from my GT-I9305 using Tapatalk
 

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
For me personally this would be great. I do all my mods in smali and know little to no java. However being able to convert can be really useful and a simple GUI to do this on a file or two would be great :)


Sent from my GT-I9305 using Tapatalk
Alright then, no worries, the app will have a small 'n nice GUI :)

Well, there is #panicroom on Freenode. Lots of good peeps there. If you want to make one, we probably ought to do it on freenode, most of the Android dev stuff from XDA is on there after all.

And probably it would make more sense for smali to have some sort of separator character to split arguments in function signatures, but It totally doesn't. All it has is the semicolon to indicate the end of a type 'L'

*edit* Uhhhh, did you delete your github account or something? I am getting a 404.
Ah I see, I thought XDA had their own IRC servers. Well, no prob, I can make a channel somewhere xD. Also thanks for the explanation :)

About github, sorry... I dunno WTF happened to it, for some reason a github "bot" thinks I'm not human (so what the hell am I then?) and asked me to contact support. I did, but I haven't received any replies yet. This is frustrating...

Edit: It seems they fixed that now. Can you try again please?

Edit2: Alright, channel #smali2java created on Freenode. Have fun! :p
 
Last edited:
  • Like
Reactions: yashade2001

codelover

Senior Member
Oct 11, 2012
416
2,178
The Land Below The Wind
A GUI would be nice but a console package would be the starting point right! As it could be added to tools etc...?

Having the same thought! So once the command line backend is ready, the GUI version can be simply a wrapper that calls the API, or better a seperate project maintained by other users called for Smalli2javaGUI, GUI 4 s2j or anything they like, so you can have better time focusing on the master project. :p
 
  • Like
Reactions: Ricky Divjakovski

cadialex

Senior Member
Jul 9, 2013
95
33
Lausanne
Having the same thought! So once the command line backend is ready, the GUI version can be simply a wrapper that calls the API, or better a seperate project maintained by other users called for Smalli2javaGUI, GUI 4 s2j or anything they like, so you can have better time focusing on the master project.

Actually I'm working on a UI. It is not ready now, and comes in a new folder (simply called "UI" ^^). I think I'll do something like the JD-GUI UI, with a tree on the left and the code on the rigth. Will update you when It'll be ready (it could take a few days/weeks).
 

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
I tested it (in VM (win xp) running on linux) and it works, but only for a very small amount of smali files, I have been able to decompile like 2 small files, but all other files failed.

example (first file is small file that is working on decompilation, second one is bigger file that fails)

Code:
C:\code>Smali2Java.exe MainActivity$1.smali
Decompiling file: MainActivity$1.smali
[
package com.broodplank.kitkatscreenrecorder;

  class MainActivity$1 extends java.lang.Object implements android.view.View$OnC
lickListener {
 final com.broodplank.kitkatscreenrecorder.MainActivity  synthetic this$0;
  MainActivity$1 (com.broodplank.kitkatscreenrecorder.MainActivity p1) {
thisreturn;
}
public  void onClick (android.view.View p1) {
boolean Boolean1 = android.widget.Button.isPressed(p1);

com.broodplank.kitkatscreenrecorder.MainActivity.startrecord(Boolean1);

return;
}
}
]

C:\code>Smali2Java.exe MainActivity.smali
Decompiling file: MainActivity.smali
[

Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given
key was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Smali2Java.SmaliVM.Get(String register)
   at Smali2Java.SmaliVM.Instructions.ParseRegistersAsArgs(Dictionary`2 register
s)
   at Smali2Java.SmaliVM.Instructions.Invoke()
   at Smali2Java.SmaliVM.ProcessInstruction(SmaliMethod m, SmaliLine l)
   at Smali2Java.SmaliMethod.Process()
   at Smali2Java.SmaliClass.LoadMethods()
   at Smali2Java.SmaliEngine.Decompile(String sFilename)
   at Smali2Java.Program.Main(String[] args)

(windows xp error report)

Code:
EventType : clr20r3     P1 : smali2java.exe     P2 : 1.0.0.0     P3 : 52c9ba4b
P4 : mscorlib     P5 : 2.0.0.0     P6 : 4889dc80     P7 : dc     P8 : 5     
P9 : a4dh5wwiwww1yjtmp0c0kv4zwcalu4in



What I highly suggest is to implement support for using the ' *.smali ' (all smali files in current dir) prefix as filename, so you don't have to make a loop yourself that performs that task
 

Fenny

Inactive Recognized Developer
Jun 28, 2007
1,386
2,073
F(x)tec Pro1-X
Google Pixel 6 Pro
I tested it (in VM (win xp) running on linux) and it works, but only for a very small amount of smali files, I have been able to decompile like 2 small files, but all other files failed.

example (first file is small file that is working on decompilation, second one is bigger file that fails)

Code:
C:\code>Smali2Java.exe MainActivity$1.smali
Decompiling file: MainActivity$1.smali
[
package com.broodplank.kitkatscreenrecorder;

  class MainActivity$1 extends java.lang.Object implements android.view.View$OnC
lickListener {
 final com.broodplank.kitkatscreenrecorder.MainActivity  synthetic this$0;
  MainActivity$1 (com.broodplank.kitkatscreenrecorder.MainActivity p1) {
thisreturn;
}
public  void onClick (android.view.View p1) {
boolean Boolean1 = android.widget.Button.isPressed(p1);

com.broodplank.kitkatscreenrecorder.MainActivity.startrecord(Boolean1);

return;
}
}
]

C:\code>Smali2Java.exe MainActivity.smali
Decompiling file: MainActivity.smali
[

Unhandled Exception: System.Collections.Generic.KeyNotFoundException: The given
key was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Smali2Java.SmaliVM.Get(String register)
   at Smali2Java.SmaliVM.Instructions.ParseRegistersAsArgs(Dictionary`2 register
s)
   at Smali2Java.SmaliVM.Instructions.Invoke()
   at Smali2Java.SmaliVM.ProcessInstruction(SmaliMethod m, SmaliLine l)
   at Smali2Java.SmaliMethod.Process()
   at Smali2Java.SmaliClass.LoadMethods()
   at Smali2Java.SmaliEngine.Decompile(String sFilename)
   at Smali2Java.Program.Main(String[] args)

(windows xp error report)

Code:
EventType : clr20r3     P1 : smali2java.exe     P2 : 1.0.0.0     P3 : 52c9ba4b
P4 : mscorlib     P5 : 2.0.0.0     P6 : 4889dc80     P7 : dc     P8 : 5     
P9 : a4dh5wwiwww1yjtmp0c0kv4zwcalu4in



What I highly suggest is to implement support for using the ' *.smali ' (all smali files in current dir) prefix as filename, so you don't have to make a loop yourself that performs that task

Well it would be easy enough to do that, but for now there are a LOT of things that are not implemented in the actual decoder... I.E. all conditional code branching, certain method call types, etc... I think it would be better to have the application do one file at a time until it is at a more functional state. Same with GUI, maybe later when the application is more feature complete.

Can you upload the file that it is crashing on? As for the crash itself I will add try blocks around the ParseRegistersAsArgs function in my repo... I think when we hit a parse failure or a skip where there should be none we should concatenate a comment into the output code so that we have a better idea where it is happening. What do you think, @darkguy2008 ?
 

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
Well it would be easy enough to do that, but for now there are a LOT of things that are not implemented in the actual decoder... I.E. all conditional code branching, certain method call types, etc... I think it would be better to have the application do one file at a time until it is at a more functional state. Same with GUI, maybe later when the application is more feature complete.

Can you upload the file that it is crashing on? As for the crash itself I will add try blocks around the ParseRegistersAsArgs function in my repo... I think when we hit a parse failure or a skip where there should be none we should concatenate a comment into the output code so that we have a better idea where it is happening. What do you think, @darkguy2008 ?
Okay, I was gonna do a big multi-quote but Fenny already said a few things I wanted to:

First and foremost, I repeat, the app isn't ready yet for decompiling smali files out of the blue. It is in a very, VERY alpha stage where we don't even have all of the smali functions enumerated or even coded, to have it work on even a simple smali file. In fact, it needs work, but in terms of coding work.

I have to agree with Fenny though. While a GUI is a nice feature to have (and I'm more of a GUI rather than a CLI person and is one of the reasons why I prefer Windows over Linux), I don't think we should focus our work on a GUI, which, more likely than not, will need to be changed a lot through the whole development. Instead, I'd rather get help in coding the needed stuff to make this work nicely and then I promise we'll work toegether to make a good GUI for it. I have plans for a good GUI ! I just don't feel it is the right time to do it with the app not even being able to decompile correctly a smali file yet. Still, if someone does it, the work is appreciated and well if I like it it'll be included, it just isn't the main focus now.

I also agree with the comment thing. I've been working my head around today on how to display some debug info without complicating the working code much. I'm having a bit of a struggle trying to debug some specific smali lines and reaching to them in the debugger is a bit annoying, lol.

I would also like to see what files did broodplank1337 used. I assume they would come in handy in our tests :)

Keep up the great ideas and contributions! :D
 

Fenny

Inactive Recognized Developer
Jun 28, 2007
1,386
2,073
F(x)tec Pro1-X
Google Pixel 6 Pro
Okay, I was gonna do a big multi-quote but Fenny already said a few things I wanted to:

First and foremost, I repeat, the app isn't ready yet for decompiling smali files out of the blue. It is in a very, VERY alpha stage where we don't even have all of the smali functions enumerated or even coded, to have it work on even a simple smali file. In fact, it needs work, but in terms of coding work.

I have to agree with Fenny though. While a GUI is a nice feature to have (and I'm more of a GUI rather than a CLI person and is one of the reasons why I prefer Windows over Linux), I don't think we should focus our work on a GUI, which, more likely than not, will need to be changed a lot through the whole development. Instead, I'd rather get help in coding the needed stuff to make this work nicely and then I promise we'll work toegether to make a good GUI for it. I have plans for a good GUI ! I just don't feel it is the right time to do it with the app not even being able to decompile correctly a smali file yet. Still, if someone does it, the work is appreciated and well if I like it it'll be included, it just isn't the main focus now.

I also agree with the comment thing. I've been working my head around today on how to display some debug info without complicating the working code much. I'm having a bit of a struggle trying to debug some specific smali lines and reaching to them in the debugger is a bit annoying, lol.

I would also like to see what files did broodplank1337 used. I assume they would come in handy in our tests :)

Keep up the great ideas and contributions! :D

Here is a nice listing of opcodes, btw. Many of them are never seen in 2.0+ though. :D

https://github.com/JesusFreke/smali...est/resources/LexerTest/InstructionTest.smali

*EDIT* @broodplank1337 The changes I mentioned were just merged back into the main repo, pull em and give it a go. >:D

*EDIT2* some more light reading for you. :D @darkguy2008
https://github.com/JesusFreke/smali/blob/master/smali/src/main/antlr3/smaliTreeWalker.g
https://github.com/JesusFreke/smali/blob/master/smali/src/main/antlr3/smaliParser.g

Particularly interesting is the ParseRegister function.
 
Last edited:
  • Like
Reactions: broodplank1337

Diego910

Senior Member
Dec 27, 2012
883
984
This could be awesome for thousands of mtk devices without cm or aosp!

Enviado desde mi bq Aquaris 5 HD mediante Tapatalk
 

aashishb4u

Member
Feb 8, 2014
18
3
help

Thankfull that u took so much efforts ...!!!
But sir can u make video tutorial so that we can get it more accurately..!!
I tried this I convert .dex files to .smali but didn't get that how to convert .small into java
As well after modifieng it I don't get how to compile it again...!!!
Please help
 

Top Liked Posts

  • There are no posts matching your filters.
  • 68
    DIRECT SMALI TO JAVA CONVERTER
    (This is a work-in-progress. Hop in if you can help!)

    Welcome guys!

    This project is born because there isn't any reliable way to convert a bunch of smali files into something you can really import into eclipse and compile away. I've lost a few projects in a recent 1 TB HDD crash and man, it wasn't pretty to see I couldn't even get them back with JD-GUI or something. I've done the same with a few .NET projects of mine, so why can't I on Android? you can, but sometimes the tools (JD-GUI or JAD) go nuts and attempting to fix them just gives you a headache so...

    Being a smali modder for quite some time, I managed to realize that the entire smali code, if analyzed correctly, it can be converted back to java. We mentally do it when we make smali mods, all I'm attempting to do is to automate the process and make it easier for everyone. Also, it would be really useful to be able to recompile a few Jars and APKs in order to fix device bugs (which is also another reason why I'm working on this).

    Some of you would say there is already JD-GUI and JAD, but they're not a real true 1:1 conversion and also most of the times they're not compile-able. Also, There's JEB Android Decompiler but no way I'm gonna pay such a price for something I've been doing mentally and that I can automate with a script made in about a week or a few months. The tool could be great, but the price is too high and my own country's restrictions don't allow me to buy it even if I ever get the money.

    The idea here, is to make an app that can translate Smali to Java, directly. Of course, it requires a bit of analysis. It needs to analyze the class directives, the methods and their prologue (parameters, declaration, etc), class annotations and method instructions. So far, I've managed to develop most of the analyzer with a few basic instructions set up for demo purposes.

    Of course, I know it needs a bit more work: some variables/instructions don't appear, types are incomplete or missing, etc. But that's why I'm here: I can work on this alone, but, first, it wouldn't be fair. Also, I'm human and by default I have bugs, so I might skip something and make the code go bogus and stuff. And finally, I'd like to request the help of the community. I made this project to see if there's enough attention and contribution from the smart people here, and if there is, I'm gonna give it all to make this project do what it says, 100% error-free.

    Note: IT NEEDS .NET FRAMEWORK 3.5. Yeah, and VS 2012. I assume you can make it work with VS2010 or I can make a version soon if there's enough interest, or heck, even Mono as long as it has Linq (I make heavy use of it). The idea is to make a good, open-source parser you can compile even on an arduino with Mono and it will work. But for now, this will have to do. It is my own code and I decided to share it to see if there's interest. If there's enough, I'll do everything I can to provide the tools needed for you guys to help me make this project a reality.

    Also, it seems it works with SharpDevelop, so you can compile it under Linux too! :)

    How to contribute:

    Source code (active Github): https://github.com/darkguy2008/smali2java
    IRC Channel: #smali2java in irc.freenode.net . You can also join us in development chit-chat through web IRC client here.

    Fork away!

    Well, hoping to attract the attention of you hackers 'round here :)
    - DARKGuy

    XDA:DevDB Information
    Smali2Java, a Tool/Utility for the Android General

    Contributors
    darkguy2008

    Version Information
    Status: Testing

    Created 2013-12-31
    Last Updated 2014-01-04
    3
    I forked your repo, and I added support for iput-boolean using constant values. We will probably have to add a case to check where the register was populated by a previous instruction, I.E move-result v2. You will see a pull request for this change in your github.

    *edit*
    Okay, so I have added a whole bunch of stuff, fixed paramater parsing, etc... :D @darkguy2008 Let's rock and roll!

    https://github.com/darkguy2008/smali2java/pull/1/files
    2
    Very interesting, i can't find a word to say! Congrats man and thanks very much!
    :D thanks for the support! I'm glad you're interested! any kind of help is welcome :eek:

    Oh wow! this is great!! thank you very much!! :laugh::victory: these kind of things really give a lot of motivation to keep projects going! :good: love the review, hehe :D