How to convert Java code into Smali code

Source: Internet
Author: User

1. Overview

Smali is the Dalvik virtual machine instruction language in the Android system, and there are many tools that can turn Smali code into Java code during the APK reverse process. However, in the process of learning Smali grammar, sometimes need to carry out Java code and Smali Code control, if you can convert Java code into Smali code, learning is not very convenient. So online search, very disappointed, are all kinds of turn Smali for Java tools. Later thought, Java becomes Smali is not the process of writing Android program->dex->smali, but Java code compiled is a class file, how to become a dex file it? The answer was later found in a book, recorded as follows.

2, Java code to Smali code

The following three steps are required to turn Java code into Smali code

1. Compile Java code as class file

Javac Smalitest.java

This is relatively straightforward and generates Smalitest.class files

2. Turn class files into Dex files

We know that the Java code in the APK package is finally generating the Class.dex file, and converting the class to a Dex file requires a tool DX provided by the Android SDK, which is located in the android-sdk\build-tools\23.0.1\ LIB, after locating the package, execute the following command

Java-jar Dx.jar--dex--output=smalitest.dex smalitest. class

3. Convert Dex to Smali file

At this time, another tool Baksmali is used, the tool is located in Android-sdk\platform-tools\, and the following command is executed after the package is found

Java-jar Baksmali.jar Smalitest.dex

OK, complete, this will generate an out directory, in the Out directory Smalitest.smali is the Smali code we want to see

3. Sample Program

The Java code is as follows:

 Public class smalitest {       publicstaticvoid  main (string[] args) {           System. out. println ("hello Smali");       }   }

The Smali code is as follows

.classlsmalitest;. Super Ljava/lang/Object;. SOURCE"Smalitest.java"# Direct Methods.method Constructor<init>() v. Registers1. Prologue. Line3Invoke-direct {p0}, ljava/lang/object;-><init>() Vreturn-void. End Method.method Public StaticMain ([ljava/lang/String;) v. Registers3. parameter. Prologue . Line5Sget-ObjectV0, ljava/lang/system;-> out: ljava/io/PrintStream; Const-stringV1,"Hello Smali"Invoke-Virtual{v0, v1}, Ljava/io/printstream;->println (ljava/lang/String;) v. Line6    return-void. End Method
4. Problems encountered

My Android environment is built under Windows, the JDK version is 1.8, and the following error occurred while performing the second step under Windows

Unexpected top-Level EXCEPTION:java.lang.RuntimeException:Exception parsing classes at Com.android.dx.command.dexer.Main.proc Essclass (Main.java:752) at Com.android.dx.command.dexer.Main.processFileBytes (Main.java:718) at com.android.dx.command.dexer.main.access$ -(Main.java: -) at Com.android.dx.command.dexer.main$filebytesconsumer.processfilebytes (Main.java:1645) at Com.android.dx.cf.direct.ClassPathOpener.processOne (Classpathopener.java: the) at Com.android.dx.cf.direct.ClassPathOpener.process (Classpathopener.java:144) at Com.android.dx.command.dexer.Main.processOne (Main.java:672) at Com.android.dx.command.dexer.Main.processAllFiles (Main.java:574) at Com.android.dx.command.dexer.Main.runMonoDex (Main.java:311) at Com.android.dx.command.dexer.Main.run (Main.java:277) at Com.android.dx.command.dexer.Main.main (Main.java:245) at Com.android.dx.command.Main.main (Main.java:106) caused By:com.android.dx.cf.iface.ParseException:badclassfile Magic (Cafebabe) or version (0034.0000) at Com.android.dx.cf.direct.DirectClassFile.parse0 (Directclassfile.java:472) at Com.android.dx.cf.direct.DirectClassFile.parse (Directclassfile.java:4 .) at Com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary (Directclassfile.java:388) at Com.android.dx.cf.direct.DirectClassFile.getMagic (Directclassfile.java:251) at Com.android.dx.command.dexer.Main.parseClass (Main.java:764) at com.android.dx.command.dexer.main.access$ the(Main.java: -) at Com.android.dx.command.dexer.main$classparsertask.call (Main.java:1684) at Com.android.dx.command.dexer.Main.processClass (Main.java:749)        ...  One More1Error Aborting

The cause is unclear, probably the JDK version is wrong, then the Baksmali.jar and Dx.jar two packages copied to Ubuntu, perform the above three steps, run successfully, Ubuntu installed on the JDK version is 1.7

How to convert Java code into Smali code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.