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

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.


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

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

Screenshot highlighting the attach debugger icon

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