編輯:Android 開發實例
研究 Android 反編譯,很重要的一課是理解「Java 原始碼經過編譯後長什麼樣子」。本文以一個掃雷遊戲的方塊更新方法為例,對照原始碼與反編譯結果,說明 switch 與 if 這兩種控制流程在字節碼層級的真實模樣。
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 這類型別前綴加序號的形式。
原始寫法:
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 的關鍵字是 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
可以從這段反編譯輸出解讀出四項資訊:
tableswitch 1 9 表示 case 值從 1 連續到 9,共九個分支,對應標籤 _L3 到 _L11。default 88 指向的位置對應原始碼中沒有寫出的 default: break;,也就是 _L2。byte0 = 79 對應十進位的 79,也就是 Color.rgb(47, 79, 79) 中的 79)。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),還應把關鍵驗證與加解密放到伺服器端,而不是單靠客戶端的程式碼保護。
仿通訊錄索引:自訂 SiderBar 側邊字母列
有些 App 的通訊錄右側那條字母索引做得相當順手,忍不住自己也做一個。這裡把完整實作分享出來,其中還有可以改進的地方,也歡迎一起討論。 先看成果: 自訂 Vie
動手做一個 Android 音樂播放器:MediaPlayer、ListView 與 SeekBar 的整合
做一個音樂播放器,是熟悉 Android 元件最快的方式:它同時用到 ListView 顯示清單、MediaPlayer 控制播放、SeekBar 呈現並調整進度,還牽涉
用 Mono for Android 實作 Android 4.0 高效導航(Effective Navigation)
Android 4.0 這一代系統在設計規範中明確定義了一套「高效導航」(Effective Navigation)模式,主要包含標籤頁、下拉清單,以及向上(Up)與返回
Android 理財 App 實作(四):新增帳單頁面的版面配置與 Spinner 資料綁定
這一節要把新增帳單的介面整理出來。動手前覺得應該不難,實際做下去才發現:排版面比寫程式還費工。上網找資料也收穫有限,官方範例裡用得最多的還是 ListView,但這個頁面