class文件与dex文件比较

新建smali.java

public class smali{
    public int foo(int a,int b){
        return (a+b)*(a-b);
    }
    public static void main(String[] argc){

        smali s=new smali();
        System.out.println(s.foo(3,5));
    }
}

编译.java文件,生成:javac smali.java
编译.class文件,生成dex文件:dx --dex --output=smali.dex smali.class //dx工具在sdk目录下(android-sdk\build-tools\26.0.2)


反编译阶段
反编译.class文件:javap -c smali.class
输出结果:

Compiled from "smali.java"
public class smali {
  public smali();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public int foo(int, int);
    Code:
       0: iload_1
       1: iload_2
       2: iadd
       3: iload_1
       4: iload_2
       5: isub
       6: imul
       7: ireturn

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class smali
       3: dup
       4: invokespecial #3                  // Method "":()V
       7: astore_1
       8: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
      11: aload_1
      12: iconst_3
      13: iconst_5
      14: invokevirtual #5                  // Method foo:(II)I
      17: invokevirtual #6                  // Method java/io/PrintStream.println:(I)V
      20: return
}

反编译.dex文件:dexdump -d smali.dex(dexdump命令也在sdk目录)
输出信息:

Processing 'smali.dex'...
Opened 'smali.dex', DEX version '035'
Class #0            -
  Class descriptor  : 'Lsmali;'
  Access flags      : 0x0001 (PUBLIC)
  Superclass        : 'Ljava/lang/Object;'
  Interfaces        -
  Static fields     -
  Instance fields   -
  Direct methods    -
    #0              : (in Lsmali;)
      name          : ''
      type          : '()V'
      access        : 0x10001 (PUBLIC CONSTRUCTOR)
      code          -
      registers     : 1
      ins           : 1
      outs          : 1
      insns size    : 4 16-bit code units
00014c:                                        |[00014c] smali.:()V
00015c: 7010 0100 0000                         |0000: invoke-direct {v0}, Ljava/lang/Object;.:()V // method@0001
000162: 0e00                                   |0003: return-void
      catches       : (none)
      positions     :
        0x0000 line=1
      locals        :
        0x0000 - 0x0004 reg=0 this Lsmali;

    #1              : (in Lsmali;)
      name          : 'main'
      type          : '([Ljava/lang/String;)V'
      access        : 0x0009 (PUBLIC STATIC)
      code          -
      registers     : 5
      ins           : 1
      outs          : 3
      insns size    : 17 16-bit code units
000164:                                        |[000164] smali.main:([Ljava/lang/String;)V
000174: 2200 0400                              |0000: new-instance v0, Lsmali; // type@0004
000178: 7010 0200 0000                         |0002: invoke-direct {v0}, Lsmali;.:()V // method@0002
00017e: 6201 0000                              |0005: sget-object v1, Ljava/lang/System;.out:Ljava/io/PrintStream; // field@0000
000182: 1232                                   |0007: const/4 v2, #int 3 // #3
000184: 1253                                   |0008: const/4 v3, #int 5 // #5
000186: 6e30 0300 2003                         |0009: invoke-virtual {v0, v2, v3}, Lsmali;.foo:(II)I // method@0003
00018c: 0a00                                   |000c: move-result v0
00018e: 6e20 0000 0100                         |000d: invoke-virtual {v1, v0}, Ljava/io/PrintStream;.println:(I)V // method@0000
000194: 0e00                                   |0010: return-void
      catches       : (none)
      positions     :
        0x0000 line=7
        0x0005 line=8
        0x0010 line=9
      locals        :

  Virtual methods   -
    #0              : (in Lsmali;)
      name          : 'foo'
      type          : '(II)I'
      access        : 0x0001 (PUBLIC)
      code          -
      registers     : 5
      ins           : 3
      outs          : 0
      insns size    : 6 16-bit code units
000198:                                        |[000198] smali.foo:(II)I
0001a8: 9000 0304                              |0000: add-int v0, v3, v4
0001ac: 9101 0304                              |0002: sub-int v1, v3, v4
0001b0: b210                                   |0004: mul-int/2addr v0, v1
0001b2: 0f00                                   |0005: return v0
      catches       : (none)
      positions     :
        0x0000 line=3
      locals        :
        0x0000 - 0x0006 reg=2 this Lsmali;

  source_file_idx   : 15 (smali.java)

你可能感兴趣的:(class文件与dex文件比较)