可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I get this error when trying to debug an Android app on Android Studio (v.0.4.4):
Error running [assembleDebug]: Unable to open debugger port : java.net.SocketException "Socket closed"
The app compiles, and can be pushed to the phone using adb. There's no problem with adb; I can install/monitor logcat etc perfectly. I can load another project and can debug it just fine. I switch back to this one and it's still broken. So that would appear to rule out a problem with the pc (actually a Linux vm running under windows), the Android Studio install etc. I've rebooted this vm and the host pc.
I had this problem last last year and I "fixed it" then by uninstalling Eclipse (I suspected perhaps it was trying to get the same socket).
I've spent some time Googling and attempting suggestions but they don't make any difference.
The problem occurred around the time that I produced a signed, proguarded release build. Until then I'd only been running the debug build. However, I believe all I did to create this release build was to edit build.gradle and configure Android Studio to automatically sign apks. I've since reverted the changes to build.gradle but the problem remains.
I see no relevant errors in any Android Studio log files; it's as if this exception is being captured, reported on screen but not logged anywhere.
Hopefully somebody reading this can suggest a few things here to change that I've overlooked.
回答1:
On Android Studio desktop, click on "Select Run/Debug Configuration" button (one with an android icon), you should be able to select the correct option.
回答2:
I've had this “Socket closed” error for several weeks and it was driving me slightly mad. Today I discovered that although launching via the debug icon still gives me a “Socket closed” if I attach the debugger afterwards it works (in screenshot). Hope it saves someone else time.
回答3:
I was able to fix this issue by selecting the "app" configuration. In the above screen shot the pull down to the left of the run button allows you to select the configuration.Select one without the brackets.
回答4:
To avoid this problem and actually launching from within Android Studio, I had to explicitly switch the build variant I was operating in (via the panel that pops up when selecting 'build variants' on the left edge). Then when actually pressing the debug button up top for my Android Application configuration (selected via drop down menu to the left of the Run/Debug icons), it would properly deploy the build variant mode I was in. (Would be nice if I could set up different Android App configurations to deploy different build types from the drop down menu instead of having to switch variants more manually, but I don't see how right now.)
Also in your buildTypes section in your build.gradle file, you probably want debuggable true
, else your process won't show up in the dialog if you try to manually attach the debugger.
回答5:
I was getting this error on linux, and the problem was that the another previous process have taken the port and hung. So, the solution is a netstat to locate the process blocking the port, then kill this process, or you can restart the machine.
回答6:
I ran into the same behavior, the above solutions didn't work for me, but I have a bit of a corner case: my app extends android.app.Application. What I found works for me is to edit the run/debug configuration ("Edit Configurations..."), and select "Do not launch Activity" instead of "Launch default Activity".
回答7:
I run into this problem just now, simple solution: if you have multiple instances running, make sure your run config specifiy different JMX port. refer to Image
回答8:
I was getting this error because I had the "Test Kind" option in the "Run/Debug Configurations" dialog box set to "All in Package" rather than "Test Class". When I set it to "Test Class", the debugger worked flawlessly.
回答9:
I had a similar problem with a Java program. I restarted the IDE and changed the JMX port number. Then the program ran in debug mode without any problems.
回答10:
Make sure your Android Phone is well connected.