前面幾篇關(guān)于Gradle插件的開(kāi)發(fā)。在開(kāi)發(fā)過(guò)程中,遇到問(wèn)題就需要找出問(wèn)題出在哪里。這個(gè)問(wèn)題是自定義插件中還是其他地方猾警?通常報(bào)錯(cuò)會(huì)給提示:
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
然后添加--info
或--debug
選項(xiàng)或許可以解決。有時(shí)我們需要深入源代碼找出問(wèn)題發(fā)生時(shí)的情況隆敢,又或者想要跟蹤代碼的執(zhí)行過(guò)程以了解原理等等发皿。這種方式仍然稱為“斷點(diǎn)調(diào)試”。
工具
IntelliJ IDEA拂蝎、Android Studio或者類似環(huán)境
斷點(diǎn)調(diào)試
點(diǎn)擊Edit Configurations
debug-gradle-1.png
點(diǎn)開(kāi)左邊的“+”號(hào)穴墅,點(diǎn)擊“Remote”
debug-gradle-2.png
debug-gradle-3.png
- 修改名稱(不改默認(rèn)Unnamed)
-
Use module classpath
這一項(xiàng)如果要調(diào)試頂級(jí)build.gradle保持默認(rèn)就可以了,如果是gradle插件工程中的代碼就選插件所在的模塊温自。 - 默認(rèn)遠(yuǎn)程調(diào)試的服務(wù)器是
localhost:5005
- 點(diǎn)擊"OK"
選中遠(yuǎn)程調(diào)試任務(wù)如圖
debug-gradle-4.png
在需要調(diào)試的地方打上斷點(diǎn)玄货,然后命令行執(zhí)行調(diào)試任務(wù)
./gradlew tasks -Dorg.gradle.debug=true --no-daemon
上面執(zhí)行tasks
任務(wù)并打開(kāi)Gradle的“調(diào)試模式”。運(yùn)行時(shí)你會(huì)發(fā)現(xiàn)Gradle停止在遠(yuǎn)程配置調(diào)試器連接之前悼泌。
> Starting Daemon
點(diǎn)擊調(diào)試按鈕就可以開(kāi)始連接遠(yuǎn)程調(diào)試了松捉。
概括
- IntelliJ(Android Studio)
Edit Configurations
點(diǎn)擊+
找到Remote
點(diǎn)擊創(chuàng)建遠(yuǎn)程配置 - 填寫信息. Name自定義, 默認(rèn)遠(yuǎn)程調(diào)試
localhost:5005
-
Search sources using module's classpath
選擇需要調(diào)試的插件模塊 - 命令行執(zhí)行任務(wù)調(diào)試:
./gradlew tasks -Dorg.gradle.debug=true --no-daemon
,等待連接調(diào)試 - 源代碼斷點(diǎn), 選擇剛創(chuàng)建的調(diào)試配置, 點(diǎn)擊
Debug Xxx(Shift+F9)
遠(yuǎn)程調(diào)試同樣適用于注解處理器(Annotation Processor)
問(wèn)題
Remote Configuration debugger **can not be attached.
- 這是因?yàn)槎丝?默認(rèn)5005)已經(jīng)被另一個(gè)正在運(yùn)行的Gradle守護(hù)進(jìn)程占用了,簡(jiǎn)單的停止即可
./gradlew --stop