17

I reverse engineered an Android application with APKTool and got .Smali files as source code output. I converted the .Smali files with an application to .Java files. I was able to succesfully edit the .Java files but now I want to convert them back to .Smali so I can recompile the application with the new .Smali files. When I just leave the .Java file there it doesn't recompile it and gives some errors. I couldn't find anything about compiling .Java to .Smali on the internet so I hope you guys could help me.

Thank you in advance,

2 Answers2

25

You can compile the java classes using a normal java compiler, and then use Android's 'dx' utility to convert the compiled .class files to a dex file. And then run baksmali on the dex file to produce the smali files.

For example, let's say you have the following code in a java file named "HelloWorld.java":

public class HelloWorld {
    public static void main(String[] args) {
         System.out.println("Hello World!");
    }
}

To convert it to smali:

javac HelloWorld.java
dx --dex --output=classes.dex HelloWorld.class
baksmali d classes.dex
JesusFreke
  • 18,289
  • 4
  • 56
  • 65
  • 2
    Thanks, helped me as well. I've made a small Windows batch file to accomplish this: https://gist.github.com/houtianze/fc9f158fd86bb0b58c485139ddc32ece – ibic Apr 11 '16 at 07:28
  • 3
    Note, that you have to use Java 7 (not Java 8) for that! – z3ntu Jun 03 '16 at 06:36
  • Shouldn't the last line be `baksmali d classes.dex` instead? Did you change the command-line format? – user202729 Dec 11 '20 at 09:21
  • 1
    @user202729 Thanks, fixed. Yeah, the command format did change between now and when this was first posted. – JesusFreke Dec 13 '20 at 04:00
16

Try java2smali: https://github.com/ollide/intellij-java2smali

It is an application used to convert Java to Smali.

Dermot Blair
  • 1,480
  • 9
  • 10
  • It's an IntelIJ plugin. I need a standalone program. –  Mar 14 '15 at 17:18
  • See: http://stackoverflow.com/questions/5448267/direct-java-to-smali-conversion-possible – Dermot Blair Mar 14 '15 at 17:19
  • Yeah, I found that post as well but it converts the .Java file to a .class file or .dex file but I need a .Smali converter because APKTool does the converting from .Smali > .Dex –  Mar 14 '15 at 17:24
  • 3
    Huh! I wasn't aware of this plugin. nifty – JesusFreke Mar 14 '15 at 17:40
  • 2
    Does it required all package in java format to convert 2-3 classes back to smali? I have only 2-3 classes in java which use some libraries. Libraries are already in .smali form (not .java) and I just need to convert those 2-3 classes to .smali. Is it possible? – Alireza Mohamadi Jun 19 '16 at 12:37