What is Smali Code Android

user4598738 picture user4598738 · Jun 15, 2015 · Viewed 34k times · Source

I am going to learn a little bit about Dalvik VM, dex and Smali.

I have read about smali, but still cannot clearly understand where its place in chain of compilers. And what its purpose.
Here some questions:

  1. As I know, dalvik as other Virtual Machines run bytecode, in case of Android it is dex byte code.
  2. What is smali? Does Android OS or Dalvik Vm work with it directly, or it is just the same dex bytecode but more readable for the human?
  3. Is it something like dissasembler for Windows (like OllyDbg) program executable consist of different machines code (D3 , 5F for example) and there is appropriate assembly command to each machine code, but Dalvik Vm also is software, so smali is readable representation of bytecodes
  4. There is new ART enviroment. Is it still use bytecodes or it executes directly native code?

Thank you in advance.

Answer

Antimony picture Antimony · Jun 15, 2015

When you create an application code, the apk file contains a .dex file, which contains binary Dalvik bytecode. This is the format that the platform actually understands. However, it's not easy to read or modify binary code, so there are tools out there to convert to and from a human readable representation. The most common human readable format is known as Smali. This is essentially the same as the dissembler you mentioned.

For example, say you have Java code that does something like

int x = 42

Assuming this is the first variable, then the dex code for the method will most likely contain the hexadecimal sequence

13 00 2A 00

If you run baksmali on it, you'd get a text file containing the line

const/16 v0, 42

Which is obviously a lot more readable then the binary code. But the platform doesn't know anything about smali, it's just a tool to make it easier to work with the bytecode.

Dalvik and ART both take .dex files containing dalvik bytecode. It's completely transparent to the application developer, the only difference is what happens behind the scenes when the application is installed and run.