一、首先介绍几条命令:
将Hello.java文件编译成Hello.class文件
F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>javac -source 1.6 -target 1.6 Hello.java
将Hello.class文件反编译java汇编代码
F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>javap -c -classpath . Hello
将Hello.class文件编译成Hello.dex文件
F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>dx --dex --output Hello.dex Hello.class
将.dex文件反编译成dalvik汇编代码
F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>dexdump.exe -d Hello.dex
二、比如下面的java代码,我要看反编译后的android汇编代码,应如下操作:
public class Hello { public int foo(int a, int b) { return (a+b)*(a-b); } public static void main(String[] argc) { Hello h = new Hello(); System.out.println(h.foo(5, 3)); } }
1.把dx路径的放进环境变量path中,例如我的路径是 F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2
2.将java代码编译成java的字节码文件(class文件)
因为的jdk版本太高,所以要使用命令如下:
F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>javac -source 1.6 -target 1.6 Hello.java
2.将Hello.class文件放到dx的路径下
3.通过dx工具将Hello.class文件(java字节码)转换成android的Hello.dex文件(dalvik字节码)
F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>dx --dex --output Hello.dex Hello.class
4.通过dexdump.exe将Hello.dex文件反编译成dalvik汇编代码
F:\adt-bundle-windows-x86_64\sdk\build-tools\android-4.4.2>dexdump.exe -d Hello.dex
最终生成的dalvik汇编代码如下:
Processing ‘Hello.dex‘... Opened ‘Hello.dex‘, DEX version ‘035‘ Class #0 - Class descriptor : ‘LHello;‘ Access flags : 0x0001 (PUBLIC) Superclass : ‘Ljava/lang/Object;‘ Interfaces - Static fields - Instance fields - Direct methods - #0 : (in LHello;) name : ‘<init>‘ type : ‘()V‘ access : 0x10001 (PUBLIC CONSTRUCTOR) code - registers : 1 ins : 1 outs : 1 insns size : 4 16-bit code units 00014c: |[00014c] Hello.<init>:()V 00015c: 7010 0400 0000 |0000: invoke-direct {v0}, Ljava/ lang/Object;.<init>:()V // [email protected] 000162: 0e00 |0003: return-void catches : (none) positions : 0x0000 line=1 locals : 0x0000 - 0x0004 reg=0 this LHello; #1 : (in LHello;) 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] Hello.main:([Ljava/lang /String;)V 000174: 2200 0100 |0000: new-instance v0, LHello; / / [email protected] 000178: 7010 0000 0000 |0002: invoke-direct {v0}, LHello ;.<init>:()V // [email protected] 00017e: 6201 0000 |0005: sget-object v1, Ljava/lang /System;.out:Ljava/io/PrintStream; // [email protected] 000182: 1252 |0007: const/4 v2, #int 5 // #5 000184: 1233 |0008: const/4 v3, #int 3 // #3 000186: 6e30 0100 2003 |0009: invoke-virtual {v0, v2, v3 }, LHello;.foo:(II)I // [email protected] 00018c: 0a00 |000c: move-result v0 00018e: 6e20 0300 0100 |000d: invoke-virtual {v1, v0}, L java/io/PrintStream;.println:(I)V // [email protected] 000194: 0e00 |0010: return-void catches : (none) positions : 0x0000 line=6 0x0005 line=7 0x0010 line=8 locals : Virtual methods - #0 : (in LHello;) name : ‘foo‘ type : ‘(II)I‘ access : 0x0001 (PUBLIC) code - registers : 5 ins : 3 outs : 0 insns size : 6 16-bit code units 000198: |[000198] Hello.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 LHello; source_file_idx : 1 (Hello.java)
使用dx命令在cmd环境下执行的正确方法,我用的版本android4.4.2,jdk1.8