Android 分析工具 - FrankNine/franknine.github.io GitHub Wiki
為 Android SDK 的一部分,要使用同時需要 啟用開發人員選項和 USB 偵錯功能
adb devices
當連結一個以上的 Android 裝置或是模擬器時,需要用 -s
參數加上 adb devices
顯示的裝置代號指定指令要給哪台機器,否則會收到 error: more than one device and emulator
錯誤。注意 -s
參數必須要緊接在 adb 後,其他命令參數前(Stack Overflow 問題)
adb kill-server
強制關閉,下個命令會重新啟動電腦上的 adb
adb install -r <APK 路徑>
通常會使用 -r
代表如果已安裝則取代,取代時不會清除 App 已產生的資料,可以用這個參數模擬 APK 升級
常見失敗錯誤:
-
INSTALL_FAILED_INSUFFICIENT_STORAGE
空間不足 -
INSTALL_FAILED_VERSION_DOWNGRADE
要安裝的 APK 版號(Version code)比已安裝的小,有時能加入-d
強制執行 -
INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES
已安裝的 APK 使用的 Keystore 跟要安裝的不同,無法執行
adb logcat
持續印出裝置上的 Log
一般是不會用命令列看 logcat,會用 Monitor 或是 Android Studio 來讀才有過濾功能,不過濾 Log 太多
adb logcat -d
印到當下立即停止,組合寫入檔案可以快速留下紀錄:
adb logcat -d > log.txt
適合接著裝置當錯誤發生時馬上輸入留檔給其他人參考
adb push <PC 路徑> <Android 路徑>
最常用在推 OBB 檔案到手機上,安裝有 OBB 的 APK 命令範例:
adb install -r <APK 路徑>
adb push <OBB 路徑> /mnt/shell/emulated/obb/<Application ID 例如:com.supercell.clashofclans>/main.<Version code>.<Application ID>.obb
如果還有 Patch OBB 則目標路徑的 main
要換成 patch
OBB 路徑有很多種,要確認有點麻煩,可能要記錄手上的機器個是哪個路徑可用,常見的有:
/mnt/shell/emulated/obb/
/mnt/sdcard/Android/obb/
/sdcard/Android/obb/
adb pull <Android 路徑> <PC 路徑>
從 Google Play 安裝的 APK 如果知道安裝路徑也可以拉
adb shell
adb shell <命令>
adb shell input text <字串>
直接從電腦輸入字串,在手機上要輸入帳號密碼時非常好用
字串內如果有空白要用 %s
替代
adb shell input keyevent <事件代號>
最常用的是 4
代表 "KEYCODE_BACK"
Back Button,在 Immersive mode 相當好用
adb shell input keyevent 4
事件清單:
https://stackoverflow.com/a/8483797/1294533
adb shell input tap <X> <X>
模擬觸控螢幕一次,座標原點是螢幕左上
adb shell input swipe <start.x> <start.y> <end.x> <end.y>
adb shell dumpsys package <Application ID> | grep version
在登入 Android shell 的狀態下
run-as <Application ID>
如果該 APK 是 debugable(AndroidManifest 有 android:debuggable="true"
android:debuggable 文件,Unity 開 Development build 會幫你加上 debuggable 但是兩者不是相同的概念,手動編輯建置 Gradle 可以做出 debuggable 的非 Development build)
成功的話會進到 App 的私有目錄,可以存取 App 寫到手機的檔案如存檔、網路下載的 AssetBundle 或是 PlayerPrefs。如果不透過這樣只有有 Root 的機器才能讀取
沒有防護的 APK 可以用 Apktool 拆解,編輯 AndroidManifest 手動加上 android:debuggable
後壓回安裝再 run-as 來存取
adb bugreport
產生詳細的 Native 紀錄包,但因為 Unity 黑箱所以用途有限,追 Native crash 寫 Issue 可能會用到
adb shell am start -n {insert bundle identifier here}/com.unity3d.player.UnityPlayerActivity -e 'unity' '-deepprofiling'
https://docs.unity3d.com/Manual/profiler-profiling-applications.html
Analyze your build with APK Analyzer
Build/Analyze APK...
可以開啟 APK 讀取明文的 XML 像是 AndroidManifest,或是讀取 Java 的 Decompiled code
Google 服務包含 Google Play Game Services 與 Firebase 都會認 APK 簽名,確認簽名的 fingerprint (SHA) 受否相等很重要
apksigner verify --print-certs release.apk
keytool -list -v -keystore my.keystore
Android 打包工具,也有分析包裝的功能
aapt dump badging
aapt dump permissions
參考資料:
http://stackoverflow.com/a/18003462
列出已安裝的 APK Application ID
adb shell pm list packages
假設我們從上一步得知想拉的 APK Appliceatio ID 是 com.supercell.clashofclans
,繼續使用這個 ID 獲取 APK 完整路徑:
adb shell pm path com.supercell.clashofclans
對完整路徑 adb pull:
adb pull /data/app/com.supercell.clashofclans C:\clashofclans.apk
對 APK 以 aapt 取得 Version code
aapt dump badging C:\clashofclans.apk
已 Application ID 與 Version code(假設是 123)推測 OBB 位置拉回:
adb pull /mnt/shell/emulated/obb/clashofclans/main.123.supercell.clashofclans.obb C:\main.123.supercell.clashofclans.obb