Android Studio和 adb 的一些常用技巧


AS和ADB的隨身手冊

工欲善其事,必先利其器。

最近因為換了Mac,很多地方有些不太適應,剛好最近有想寫一篇記錄一些小工具技巧的文章,順便就把Mac中AS常用的快捷鍵也一並對應記錄起來吧。

以下為Android開發中,比較常用的小技巧,一時間想不起太多,所以先這樣,以后有時間持續更新。

IntelliJ Idea常用的快捷鍵

key xwin mac os x 10.5 + desc
SmartType Ctrl + Shift + 空格 ⌃ + ⇧ + 空格 提示與補全
Show Intention Actions Alt + Enter ⌥ + ↩︎ 顯示修改建議
Undo/Redo Ctrl + ZCtrl+Shift+Z ⌘ + Z⌘ + ⇧ + Z 撤銷/反撤銷
Generate... Alt + Insert ⌘ + N⌃ + ↩︎ 自動生成代碼
Override Methods... Ctrl + O ⌃ + O 自動復寫方法
Comment Ctrl + Shift + / ⌘ + ⇧ + / 注釋與取消
Rename... Shift + F6 ⇧ + F6 重命名相關引用
Optimize Imports Ctrl + Alt + O ⌃ + ⌥ + O 整理導包
Reformat Code Ctrl + Alt + L ⌥ + ⌘ + L 格式化代碼
Duplicate Entire Lines Ctrl + D ⌘ + D 復制當前行粘到下一行​
Delete Line Ctrl + Y ⌘ + Back 刪除當前行
Find Usages Alt + F7 ⌥ + F7 查看調用的地方
Quick Doc Ctrl + Q F1 查看注釋文檔
File Structure Ctrl + F12 ⌘ + F12 查看文件代碼結構
Class... Ctrl + N ⌘ + O 找類,再按含Non-Project
File... Ctrl + Shift + N ⌘ + ⇧ + O 找文件,再按含Non-Project
Find in Path... Ctrl + Shift + F ⌘ + ⇧ + F 全局搜索
Jump To Source F4 F4⌘ + Down 跳轉到源碼
Select Prev/Next Tab Alt + Left|Right ⌃ + Left|Right 歷史軌跡跳轉
Folding Ctrl + -|Ctrl + = ⌘ + -|⌘ + = 代碼折疊

注:
⌘:Command
⌥:Option
⌃:Control
⇧:Shift
⌫:Backspace
↩︎:Enter
Up|Right|Down|Left:方向鍵
關於代碼折疊
AS關於折疊有三種:只折疊當前塊(⌘ + -),往下遞歸折疊(⌘ + ⌥ + -),全部折疊(⌘ + ⇧ + -)。其中由於默認打開了閉包折疊(Editor - General - CodeFolding:Collapase By Default 默認勾了 "Closures"),所以總有部分折疊不起來,筆者是有強迫症的,而且我覺得吧,我都要折疊了你還給我強制留一部分顯示???有點不可思議的交互設計~,所以如果你跟我一樣,就把這個勾去掉即可。

各種依賴方式比較

Old New Desc
compile api
implementation
其中 apicompile 一致,impl無法傳遞依賴
provided compileOnly 只參與編譯,不參與打包
apk runtimeOnly 只參與打包,不參與編譯
debugCompile debugImplementation debug包有效
releaseCompile releaseImplementation release包有效
testCompile testImplementation test包有效

常用分析 Analyze:

優化相關:
Inspect Code / Run Inspection by Name...:這個是代碼分析,或者說代碼掃描。
Infer Nullity:配合@Nullable/@NotNull規避空指針。
Code Cleanup:這個是代碼自動優化。

分析認識:
Analyze Dependences...:這些基本都是分析依賴的,從宏觀認識新項目。
Analyze Data Flow From/To Here:對某個小模塊進行數據流分析,從調用層次認識項目。

定位問題:
Analyze Stack Trace:如果你有一份堆棧信息,這個可以幫你快速跳轉。

頁面元素分析 Layout Inspector

Tools -> Layout Inspector
截取一個畫面后,可以根據畫面元素ID找到交互監聽,快速定位業務線。

用adb 查看 activity

adb shell dumpsys activity | grep "mFoc"
adb shell dumpsys activity | grep -i run

用adb操作包(安裝-查看-啟動-停止-卸載)

## 安裝
adb install <file>
## 卸載
adb uninstall <package>
## 清空
adb shell pm clear <package>
## 查看包
adb shell pm list packages [-args]
    -f: 輸出包和關聯的apk文件
    -i: 輸出安裝來源
    -e "com.google": 過濾
    -s:系統包
    -3:第三方包
    -d/e: 輸出禁用和啟用的包

## 根據包名啟動:
adb shell monkey -p <package> -c android.intent.category.LAUNCHER 1

## 根據包名+類名啟動:
adb shell am start [-a android.intent.action.MAIN -c android.intent.category.LAUNCHER] -n <package>/<package.class>

## 關閉應用
adb shell am force-stop <package>

用adb進行輸入(adb shell input)

# 文本輸入:
text <string> (Default: touchscreen)
# 鍵盤按鍵:
keyevent [--longpress] <key code number or name> ... (Default: keyboard)
# 觸屏點擊:
tap <x> <y> (Default: touchscreen)
# 滑動:
swipe <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
# 拖放操作:
draganddrop <x1> <y1> <x2> <y2> [duration(ms)] (Default: touchscreen)
    press (Default: trackball)
    roll <dx> <dy> (Default: trackball)

關於KEYCODE可參考:https://blog.csdn.net/feizhixuan46789/article/details/16801429

使用ADB WIFI

要求兩個設備必須在同一個局域網(同一IP段)。
第一次連接需要USB

# 查看adb進程相關信息
ps -e|grep "adb"

# 以 TCP Mode 重新啟動ADB
adb tcpip 5555

# 查看手機IP地址
adb shell ifconfig wlan0

# 連接與斷開,其中5555端口是默認端口,disconnect時可以省略
adb connect  <手機IP>:5555
adb disconnect  <手機IP>[:5555]

附ADB手冊:
http://adbshell.com/commands


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM