Unable to open debugger port : java.net.SocketException "Socket closed"

Android Studio

Android Studio Problem Overview


I get this error when trying to debug an Android app on Android Studio (v.0.4.4):

Error running <appname> [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.

Android Studio Solutions


Solution 1 - Android Studio

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.

Solution 2 - Android Studio

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.

Screenshot highlighting the attach debugger icon

Solution 3 - Android Studio

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.

Solution 4 - Android Studio

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.

Solution 5 - Android Studio

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.)

enter image description here

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.

Solution 6 - Android Studio

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".

Solution 7 - Android Studio

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

Solution 8 - Android Studio

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.

Solution 9 - Android Studio

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.

enter image description here

Solution 10 - Android Studio

Make sure your Android Phone is well connected.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser146043View Question on Stackoverflow
Solution 1 - Android Studiouser3318951View Answer on Stackoverflow
Solution 2 - Android StudioscottyabView Answer on Stackoverflow
Solution 3 - Android StudioSteven SzczukaView Answer on Stackoverflow
Solution 4 - Android StudioRogelio TriviñoView Answer on Stackoverflow
Solution 5 - Android StudioqixView Answer on Stackoverflow
Solution 6 - Android StudiocmarcelkView Answer on Stackoverflow
Solution 7 - Android StudiofriedwmView Answer on Stackoverflow
Solution 8 - Android Studiouser3387386View Answer on Stackoverflow
Solution 9 - Android StudiorothloupView Answer on Stackoverflow
Solution 10 - Android StudioKai WangView Answer on Stackoverflow