dex 反编译(附脚本)

nix.long 于 2017-10-20

文章目录

    • 0. Demo
    • 1.javac
    • 2.生成Dex
    • 3. Samli
    • 4. 涉及工具
    • 5. Bat脚本

1.[注意]不要使用JDK1.8及以上
2.[流程].java -> .class -> .dex --> .smali

0. Demo

public class Main{
    void main(String args[]){
        int i = 0;
        i = 5;
        return;
    }
}

1.javac

编译 .class

javac Main.java

2.生成Dex

使用Android dx.jar包将.class转换为.dex, dx.jar一般位于 android-sdk\build-tools* 下面

java -jar dx.jar --dex --output=Main.dex Main.class

3. Samli

使用smali包工具将dex文件转换为smali:
java -jar baksmali.jar Main.dex

此时会生成一个out目录,在out目录下的Main.smali就是我们要看到的smali代码了

.class public LMain;
.super Ljava/lang/Object;
.source "Main.java"
# direct methods
.method public constructor <init>()V
    .registers 1
    .prologue
    .line 2
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V
    return-void
.end method
# virtual methods
.method main([Ljava/lang/String;)V
    .registers 2
    .prologue
    .line 5
    .line 7
    return-void
.end method

4. 涉及工具

  • apktool:
    是apk格式文件与smali文件的转换

  • dex2jar:
    是dex格式文件与jar文件的转换

  • smali/baksmali:
    是dex格式文件与smali文件的转换

5. Bat脚本

java2smali.bat

REM =====================================================================
REM Author:     nix.long
REM Date:        2017.10.17
REM Function:     compile x.java to dalvik smali code    
REM =====================================================================

@echo off
if "%1"=="" (
    goto help
)
set input=%1
set baksmali=baksmali-2.1.3.jar
IF %input% == help (
    goto help
) ELSE IF %input% == clean (
    IF EXIST *.class (
        del /f /s /q *.class
    )
    
    IF EXIST *.dex (
        del /f /s /q *.dex
    )
    
    IF EXIST *.smali (
        del /f /s /q *.smali
    )
) ELSE (
    IF EXIST %input%.class (
        del /f /s /q %input%.class
    )
    
    IF EXIST %input%.dex (
        del /f /s /q %input%.dex
    )
    
    IF EXIST %input%/%input%.smali (
        del /f /s /q %input%\%input%.smali
    )
    
    echo 'to make:'  %input%.java
    echo 'to gen class:'  %input%.class
    javac %input%.java
    echo 'to gen dex:'  %input%.dex
    java -jar dx.jar --dex --output=%input%.dex %input%.class
    java -jar %baksmali% -o %input% %input%.dex
    echo 'gen smali ok...'
)
goto end
:help
    echo -----------------------------------------------------------------
    echo     help [dalvik smali]:
    echo        java2smali.bat help     - show help
    echo        java2smali.bat clean    - clean mid-files
    echo        java2smali.bat X        - gen smali file for X.java
    echo -----------------------------------------------------------------
:end

你可能感兴趣的:(JVM,Java)