Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 反編譯實戰(一):從掃雷源碼看 Switch 與 if 語句的字節碼形態

編輯:Android 開發實例

研究 Android 反編譯,很重要的一課是理解「Java 原始碼經過編譯後長什麼樣子」。本文以一個掃雷遊戲的方塊更新方法為例,對照原始碼與反編譯結果,說明 switch 與 if 這兩種控制流程在字節碼層級的真實模樣。

一、原始碼:一個典型的 updateNumber 方法

public void updateNumber(int text) {
    if (text != 0) {
        this.setText(Integer.toString(text));

        switch (text) {
        case 1:
            this.setTextColor(Color.BLUE);
            break;
        case 2:
            this.setTextColor(Color.rgb(0, 100, 0));
            break;
        case 3:
            this.setTextColor(Color.RED);
            break;
        case 4:
            this.setTextColor(Color.rgb(85, 26, 139));
            break;
        case 5:
            this.setTextColor(Color.rgb(139, 28, 98));
            break;
        case 6:
            this.setTextColor(Color.rgb(238, 173, 14));
            break;
        case 7:
            this.setTextColor(Color.rgb(47, 79, 79));
            break;
        case 8:
            this.setTextColor(Color.rgb(71, 71, 71));
            break;
        case 9:
            this.setTextColor(Color.rgb(205, 205, 0));
            break;
        }
    }
}

二、編譯後的樣子

public void updateNumber(int i)
{
    char c;
    char c1;
    byte byte0;
    byte byte1;
    int j;

    c     = '\315';
    c1    = '\213';
    byte0 = 79;
    byte1 = 71;
    j     = 0;

    if (i == 0) goto _L2; else goto _L1

_L1:
    String s = Integer.toString(i);
    setText(s);

    JVM INSTR tableswitch 1 9: default 88
          1 89
          2 98
          3 118
          4 127
          5 146
          6 165
          7 187
          8 207
          9 227;
      goto _L2 _L3 _L4 _L5 _L6 _L7 _L8 _L9 _L10 _L11

_L2:
    return;

_L3:
    setTextColor(0xff0000ff);
    continue;  /* Loop/switch isn't completed */

_L4:
    int k = Color.rgb(j, 100, j);
    setTextColor(k);
    continue;

_L5:
    setTextColor(0xffff0000);
    continue;

_L6:
    int l = Color.rgb(85, 26, c1);
    setTextColor(l);
    continue;

_L7:
    int i1 = Color.rgb(c1, 28, 98);
    setTextColor(i1);
    continue;

_L8:
    int j1 = Color.rgb(238, 173, 14);
    setTextColor(j1);
    continue;

_L9:
    int k1 = Color.rgb(47, byte0, byte0);
    setTextColor(k1);
    continue;

_L10:
    int l1 = Color.rgb(byte1, byte1, byte1);
    setTextColor(l1);
    continue;

_L11:
    int i2 = Color.rgb(c, c, j);
    setTextColor(i2);

    if (true) goto _L2; else goto _L12

_L12:
}

從對照中可以清楚看到:Java 原始碼在編譯後會產生大量中間變量,而且變數名稱也會被改寫成 c1、byte0、i1 這類型別前綴加序號的形式。

三、if 條件語句的編譯規律

原始寫法:

public void updateNumber(int text) {
    if (text != 0) {
        this.setText(Integer.toString(text));
        ...
    }
}

編譯後:

public void updateNumber(int text) {
    if (text == 0)
        goto _L2;
    else
        goto _L1

_L2:
    return;

_L1:
    String s = Integer.toString(i);
    setText(s);
    ...
}

規律很明確:編譯器會把條件取反,並用 goto 跳到「不執行」的分支。原本「不等於 0 就執行」,變成「等於 0 就跳走」,語意等價但控制流反轉。

四、Switch 語句的編譯標誌

辨識 switch 的關鍵字是 JVM INSTR tableswitch:

JVM INSTR tableswitch 1 9: default 88
//      1  89
//      2  98
//      3  118
//      4  127
//      5  146
//      6  165
//      7  187
//      8  207
//      9  227;
      goto _L2 _L3 _L4 _L5 _L6 _L7 _L8 _L9 _L10 _L11

可以從這段反編譯輸出解讀出四項資訊:

  1. 分支數量:tableswitch 1 9 表示 case 值從 1 連續到 9,共九個分支,對應標籤 _L3 到 _L11。
  2. default 分支:default 88 指向的位置對應原始碼中沒有寫出的 default: break;,也就是 _L2。
  3. 常數會被提取成變數:原始碼中的枚舉值與重複使用的數字會被編譯器轉成十六進位,並把常用到的值宣告為區域變數(例如 byte0 = 79 對應十進位的 79,也就是 Color.rgb(47, 79, 79) 中的 79)。
  4. break 的判定:每個分支標記後的 continue 代表該處是 break;;而最後一個分支以 if (true) goto _L2; 作結,這是編譯器對「switch 區塊結束」的固定寫法。

五、再驗證一次:跳躍分支的情形

如果 switch 中只寫了 case 1、case 2 與 case 9,中斷連續性:

public void updateNumber(int text) {
    if (text != 0) {
        this.setText(Integer.toString(text));
        switch (text) {
        case 1:
            this.setTextColor(Color.BLUE);
            break;
        case 2:
            this.setTextColor(Color.rgb(0, 100, 0));
            break;
        case 9:
            this.setTextColor(Color.rgb(205, 205, 0));
            break;
        }
    }
}

編譯後:

JVM INSTR tableswitch 1 9: default 88
//      1  89
//      2  98
//      9  227;
      goto _L2 _L3 _L4

即使原始碼只寫了 1、2、9 三個 case,tableswitch 仍會標示範圍為 1 到 9;而 goto 後面只列出三個標籤,說明未被明確撰寫的中間值會一併導向 default。這正是 tableswitch 用「連續整數查表」取代逐一比較的效能優勢所在。

六、這代表什麼?

理解編譯後的形態有兩個實際價值:一是閱讀他人 APK 時,能快速從 tableswitch、標籤跳轉與變數命名反推原始邏輯;二是評估程式碼保護時會發現,即使原始碼被編譯成 class 或 dex,控制流程與常數仍大量保留線索。因此若程式含敏感邏輯,除了混淆(ProGuard/R8),還應把關鍵驗證與加解密放到伺服器端,而不是單靠客戶端的程式碼保護。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved