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

Search This thread

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
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
 

Attachments

  • ScreenShot022.jpg
    ScreenShot022.jpg
    164.6 KB · Views: 8,698

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
Thanks for your effort. May I request a small readme or setup guide as I'm generally a Linux user trying this out on my Windows machine?

Sent from my SAMSUNG-SGH-I727 using Tapatalk
Hello!

Oh, sure, here's a small resume but if you need more help don't hesitate to ask :)

First you need to install Visual Studio 2010 or Visual Studio 2012, I don't know if the express editions work, but they should. Then, if you use VS2010, you need to install the 4.5 .NET Targeting Pack for VS2010 which you can find on the MS website. If you install VS2012, it comes with it preinstalled.

Then, I assume you can double-click the .csproj (project file) and it'll open inside a blank solution that will ask you to save before you can compile it (press F5 to build & run). Also, make sure to edit Program.cs and modify it where it says to edit and put the absolute path of a smali file of your choice (for example, C:\users\you\desktop\someclass.smali). There's no need to put double-backslashes due to the "@" before the string literal.

Also, I think it could also work with .NET 3.5 or 4.0, but I didn't test it as I was hesitant to make something that I could publish on here. Over the next few days I'll keep working on it and keep you guys updated :)

This is great, really interested in this, and is the project open source?

Thanks for your interest! I'm glad :) and yes! you can download the whole source code and binary from the Download section here in the black top bar over the first post. That's the idea ;) open-source and easy to understand! :D
 

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
Alright, I finally learned how to use GitHub (it wasn't that hard... yet, lol), so here's a link for you all to try (it's in the main post, too): https://github.com/darkguy2008/smali2java

Also, I modified the code a bit... removed the whole switch/case stuff for each instruction and changed it to functions. Also, SmaliVM is now the main class where the translation to Java happens. I've also made a "buffer system" because, in smali, you need to process a lot of lines to create one Java line, so every smali line adds up stuff to the buffer (if needed) and it "flushes" on each .line directive. So far, so good.

Fork away and I'm eager to see your pull requests! :D
 
Last edited:

Ricky Divjakovski

Recognized Developer / Inactive RC
Feb 4, 2013
5,289
7,687
28
Sydney
Alright, I finally learned how to use GitHub (it wasn't that hard... yet, lol), so here's a link for you all to try (it's in the main post, too): https://github.com/darkguy2008/smali2java

Also, I modified the code a bit... removed the whole switch/case stuff for each instruction and changed it to functions. Also, SmaliVM is now the main class where the translation to Java happens. I've also made a "buffer system" because, in smali, you need to process a lot of lines to create one Java line, so every smali line adds up stuff to the buffer (if needed) and it "flushes" on each .line directive. So far, so good.

Fork away and I'm eager to see your pull requests! :D

404 not found?
 

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
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
 

codelover

Senior Member
Oct 11, 2012
416
2,178
The Land Below The Wind
Excellent work man! Confirmed working with SharpDevelop + .NET 4.0 too, 3.5 is not possible since some namespaces used are not available prior to 4.0.

Btw there seems to be a bug in SmaliLine.cs, line 135 where the value 0 supposed to be 1, otherwise keep getting index out of bound error.
Code:
           if (sWords.Length < [B][COLOR="Red"]1[/COLOR][/B])
Also i did some modification to the main function, so there is no need to recompile everytime, now i can simply run in in command prompt or with batch file. :D
Code:
	if (args.Length == 0)
	{
		Console.WriteLine("ERROR: No Input File!");
		Environment.Exit(1);
	}

	String sFile = args[0];
Really hope to see this project get more usable soon. Good Luck!
 
Last edited:
  • Like
Reactions: darkguy2008

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
I had so much trouble with small files before. I don't really work with them much now. Wish that this was there to help me. Keep it up! ;)
Me too haha, smali files are a headache... they're kinda easy to understand, but man aren't they harder to read? ;) thanks for the support!
Excellent work man! Confirmed working with SharpDevelop + .NET 4.0 too, 3.5 is not possible since some namespaces used are not available prior to 4.0.

Btw i did some modification to the main function, so there is no need to recompile everytime, now i can simply run in in command prompt or with batch file. :D
Hey man, thanks a lot! :D It's awesome to know it works cross-platform now :) don't worry, I just pushed a few commits and lowered the framework version, I assume you can test with 3.5 now.

Also, thanks for the code, I just pushed it to the main repo. I actually refactored a few things, hopefully it's easier to understand and handle now. Also, it now works in a buffer-like way so with some work, it will be able to handle lots of smali instructions in a single Java line as it should be :D
 

digitalhigh

Inactive Recognized Developer
Dec 28, 2010
3,626
11,934
Milwaukee, WI
www.facebook.com
This is awesome! I've been working with MIUI for quite some time now, and their utter lack of source code is both frustrating and difficult to work with. Porting eventually works, but for my current ROM, theirs is based on 4.2.2, where the US version went from 4.1.2 to 4.3. So, it works, but it's still not 100%. I'm going to get set up for this and see what I can do with actual source for some of the screwed frameworks.

As I haven't even downloaded anything yet...how crazy difficult would it be to do a whole .jar or .apk...or ROM using this?
 

darkguy2008

Senior Member
Apr 30, 2011
672
240
Heredia
very interested with this project.. hope it will be a simple to use.. :D
Thanks for the support! that's one of the main objectives: it should be ;)

This is awesome! I've been working with MIUI for quite some time now, and their utter lack of source code is both frustrating and difficult to work with. Porting eventually works, but for my current ROM, theirs is based on 4.2.2, where the US version went from 4.1.2 to 4.3. So, it works, but it's still not 100%. I'm going to get set up for this and see what I can do with actual source for some of the screwed frameworks.

As I haven't even downloaded anything yet...how crazy difficult would it be to do a whole .jar or .apk...or ROM using this?
Yeah man I feel your pain, I also have the same issue with my ZTE device. Of course the main idea is to be able to get back code from your own APKs-JARs but if it works for fixing bugs or allowing more ports on other devices, awesome.

For now the app isn't even usable, it's a bunch of code which tries to translate maybe two or three smali instructions to java but it's not ready for even a small APK. I assume that the fire test should be trying to convert a whole jar file or two, but it could take maybe a few months for it to do that, depending on how much work the project gets from the community.
 

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