52

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.

Sofía
  • 784
  • 10
  • 24

10 Answers10

45

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
    You know what, I figured this out last night, after diffing workspace.xml (quite a large file) and making changes until it worked! Every time I do a release build (click on Gradle, then double click on assembleRelease) it changes my run/debug config to run the assembleRelease option. What sense does this make? How do you do debug an assembleRelease task? Is this a bug in Android Studio? –  Feb 17 '14 at 11:43
  • This does not help me. Whatever I choose, I receive the same error. – parvus Apr 02 '15 at 12:31
  • 1
    Where can I find this button? SAorry I'm very new to Android Studio :( – Maciek Czarnik Jun 03 '15 at 11:09
31

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

scottyab
  • 23,621
  • 16
  • 94
  • 105
  • Also a solution, but too contrived. Selecting the Android app with the drop-down menu (the one labeled [InstallDebug] in your screenshot) will do it with ease. – Sven Jun 16 '15 at 10:00
  • This is an intermittent problem for me. The socket is closed some of the time, and other times it works without a problem. Attaching to the debug after is a workaround, but why in the world is the debug port blocked in the first place? – chubbsondubs Mar 28 '16 at 15:06
25

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.

Steven Szczuka
  • 251
  • 3
  • 2
  • 3
    @Triztian Someone else telling me which answer to accept! No. It refers to a screen shot "above" (where "above" doesn't make any sense on a site which lets users sort the answers by 3 different criteria) and is just another variation on the accepted answer (app configuration) posted over 7 months earlier. –  Jan 23 '15 at 09:53
  • This is also the answer that helped me best. Screenshot above is in the answer by qix Oct 9, 14. Selecting the right configuration in Android Studio is done by "Select Run/Debug Configuration", which is accessed by left-clicking on the Drop-down menu in the menu bar to the left of the run button (the green right-pointing triangle). The Android app, whatever its name, must be selected. The others are Gradle build targets which cannot be debugged, hence the error. – Sven Jun 16 '15 at 09:58
  • This answer is just how to lean how to use Android Studio properly then if it's simply tell you to select the "app" run configuration before debugging it. It's not an answer to the actual problem. – chubbsondubs Mar 28 '16 at 15:10
4

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.

Rogelio Triviño
  • 5,961
  • 3
  • 22
  • 17
3

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.

qix
  • 7,228
  • 1
  • 55
  • 65
  • Great tip to add `debuggable true` to the build.gradle file! still can't debug straight from debugger but now I can attach to my process. Thanks! – Litome Jan 18 '16 at 14:20
2

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

cmarcelk
  • 306
  • 1
  • 4
2

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

Murtaza Khursheed Hussain
  • 15,176
  • 7
  • 58
  • 83
friedwm
  • 21
  • 1
2

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.

1

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

rothloup
  • 1,220
  • 11
  • 29
-2

Make sure your Android Phone is well connected.

Kai Wang
  • 3,303
  • 1
  • 31
  • 27