Reference: http://cstsinghua.github.io/2016/06/13/Android%20studio%E5%8A%A8%E6%80%81%E8%B0%83%E8%AF%95%E6%8C%87%E5%8D%97/#anchor
首先,請先下載apktool工具並熟悉其命令的使用,可參見其官網說明https://ibotpeaches.github.io/Apktool/install/。根據你的操作系統選擇對應的版本,其實最終都是jar包,可以直接下載其jar版本,然后使用的時候采用命令java -jar apktool_x.x.x.jar [cmd ...]
-
Decode apk in debug mode(利用apktool反編譯目標apk,命令格式如下,其中out表示輸出目錄,可以根據需要替換成你自己指定的輸出目錄):
apktool d -d -o out app.apk
或者
java -jar apktool_x.x.x.jar d -d -o out app.apk
-
修改反編譯得到的文件,比如替換圖片,增刪代碼等等操作(根據實際需要進行),另外就是設置調試標記(如沒有特殊要求,除了設置調試標記外,可以不修改其他任何文件),即在輸出的out文件夾中,用文本編輯工具打開AndroidManifest.xml,在application節點中設置屬性android:debuggable=”true”。比如:
<application android:debuggable="true" android:icon="@drawable/icon" android:label="@string/app_name" android:name="com.boyaa.godsdk.core.MutiUnipayApplication">
-
Build new apk in debug mode(用apktool重新編譯打包為debug.apk,注意:回編生成的debug.apk是沒有進行簽名的,也就無法直接安裝在手機上,因此需要進行步驟4):
apktool b -d out -o debug.apk
或者
java -jar apktool_x.x.x.jar b -d out -o debug.apk
-
Sign, install and run new apk(對debug.apk簽名(建議采用安卓逆向助手 ),生成debug_sign.apk,然后安裝debug_sign.apk至手機並啟動運行該apk).
當然也可以直接采用JDK里面的簽名工具通過命令行手動進行簽名,比如:"jarsigner" -keystore "C:\Users\CoulsonChen\Desktop\kop.keystore" -storepass "kop1122334" -keypass "kop1122334" "D:\t\125.apk" "kop" -sigalg SHA1withRSA -digestalg SHA1
- Follow sub-instructions below depending on IDE(參見下面Android studio的后續子步驟,同時可參考Eclipse 動態調試指南的7-10步).
IntelliJ (Android Studio) instructions(針對Android studio的后續操作步驟)
注意:在完成上述的一般性步驟之后,再進行這里的子步驟。
-
In IntelliJ add new Java Module Project selecting the “out” directory as project location and the “smali” subdirectory as content root dir(在Android studio中,本文Android studio的版本是2.3.3,選擇菜單”File”–>”New”–>”import project”,選擇apktool反編譯時的out目錄作為項目位置引入,引入之后,將out下的smali目錄設置為內容根目錄).具體請參見下面截圖示意圖:
a.導入項目,注意:導入時選擇默認設置即可,不斷點擊next直到導入結束。如果在中途出現錯誤,注意下Android SDK的選擇即可
b.成功導入之后,以”project”視圖展示項目,選中smali目錄,鼠標右鍵點擊,在彈出菜單中按照下圖勾選(即將smali目錄設置為源碼根目錄)
-
Run Monitor (Android SDK /tools folder), find your application on a list and click it. Note port information in last column - it should be something like “86xx / 8700”.先啟動調試的apk,然后打開Android Device Monitor,如下圖所示:
a.打開Android Device Monitor(從Android SDK安裝目錄下的tools子目錄下的monitor.bat(針對windows系統)打開也可)
b.查看目標程序的調試端口(每次啟動目標程序,端口是系統分配,可能會變化,下面的步驟4中的debugger配置的端口必須與這個值一致),如下圖所示:
-
In IntelliJ: Debug -> Edit Configurations. Since this is a new project, you will have to create a Debugger.即在Debug菜單中選擇
Edit Configurations
,添加一個調試器。如下圖所示:a.添加調試配置
-
Create a Remote Debugger, with the settings on “Attach” and setting the Port to 8700 (Or whatever Monitor said). The rest of fields should be ok, click “Ok”.創建Debugger,注意配置的端口必須與上面步驟2中查詢到的調試端口一致。
a.創建debugger -
Start the debugging session. You will see some info in a log and debugging buttons will show up in top panel.從Android studio的工具欄中啟動調試會話,如下圖:
-
Set breakpoint. You must select line with some instruction, you can’t set breakpoint on lines starting with “.”, “:” or “#”.這一步主要設置斷點,以”.”/“:”/“#”開頭的行是無法設置斷點的,因為這里是基於smali調試,如不熟悉其語法,可先在google上搜索以了解smali的語法和相關的原理。選擇要觀察的程序點,在對應的代碼行設置斷點,示例如下:
-
Trigger some action in application. If you run at breakpoint, then thread should stop and you will be able to debug step by step, watch variables, etc.在手機端,操作目標程序,如果恰好你執行的操作的邏輯段設置了斷點,那么線程將會被掛起,從而可以進入調試,查看變量的值,等等。
Netbeans instructions(針對Netbeans的后續操作步驟)
-
In Netbeans add new Java Project with Existing Sources, select “out” directory as project root and “smali” subdirectory as sources dir.
-
Run DDMS, find your application on a list and click it. Note port information in last column - it should be something like “86xx / 8700”.
-
In Netbeans: Debug -> Attach Debugger -> select JPDA and set Port to 8700 (or whatever you saw in previous step). Rest of fields should be ok, click “Ok”.
-
Debugging session should start: you will see some info in a log and debugging buttons will show up in top panel.
-
Set breakpoint. You must select line with some instruction, you can’t set breakpoint on lines starting with “.”, “:” or “#”.
-
Trigger some action in application. If you run at breakpoint, then thread should stop and you will be able to debug step by step, watch variables, etc.
Limitations/Issues(限制和已知問題)
Because IDE doesn’t have full sources, it doesn’t know about class members and such.
Variables watching works because most of data could be read from memory (objects in Java know about their types), but if for example, you watch an object and it has some nulled member, then you won’t see, what type this member is.
apktool 2.1.0之后版本動態調試(適用於Android studio)
apktool 2.1.0版本之后,smali調試的邏輯獨立出來,放置在smalidea項目,請參見官方地址https://github.com/JesusFreke/smali/wiki/smalidea。依照該地址說明的步驟即可實現smali代碼動態調試。