您如何编写adb jdwp + adb forward脚本?

您如何编写adb jdwp + adb forward脚本?

问题描述:

我正在尝试使用命令行和脚本工具调试Android应用程序(我已经受够了Android Studio占用我的CPU)。

I am trying to debug an Android application using command line and script tools (I've had enough of Android Studio eating my CPU).

要连接到我的应用程序,我执行 adb jdwp 来获取pid,然后来获取pid能够连接到该应用。但是我想更多地编写脚本。

To connect to my app, I do adb jdwp to get the pid and then adb forward tcp:8700 jdwp:<pid> to be able to connect to the app. But I'd like to script this more.

adb jdwp 有两个限制:1.它没有返回,所以我必须用ctrl-c 2杀死 adb 。它仅列出pid。如果还有更多pid,就没有简单的方法可以查看哪个是我的应用。

adb jdwp have two limitations: 1. It doesn't return, so I have to kill adb with ctrl-c, 2. it only lists pids. If there are more pids, there's no easy way to see which is my app.

是否可以获取 adb jdwp 的行为:使其退出并返回pid和applicationID?

Is there a way to get adb jdwp to behave: Get it to exit and to return applicationId along with the pid?

我设法解决了这个问题使用以下脚本(如果您的应用是在最后一个过程中启动的,它将起作用)。作为未来的增强功能,它可以与 gradle 任务运行器集成,以进一步简化流程。

I managed to solve this issue with the following script (which will work provided your app is launched as a last process). As a future enhancement it could be integrated with gradle task runner to simplify the process even more.

#!/bin/bash

DEBUG_PORT=7777
SOURCE_PATH=app/src/main/java

FILE=/var/tmp/andebug-$(date +%s)
adb jdwp > "$FILE" &
sleep 1
kill -9 $!
JDWP_ID=$(tail -1 "$FILE")
rm "$FILE"
adb forward tcp:$DEBUG_PORT jdwp:$JDWP_ID
jdb -sourcepath $SOURCE_PATH -attach localhost:$DEBUG_PORT