Android – Unable to attach debugger in Android Studio – localhost:8600 java.net.ConnectException

androidandroid-studiodebugging

I cannot attach the debugger in Android Studio while running debug on a phone.

I’m currently using a Samsung S4, running Android 4.4.4 (but have tried an S5, S6, S7, and Moto E phone on various Android versions). It times out when I try to run it with either of the following errors:

I/System.out: Sending WAIT chunk
W/ActivityThread: Application is waiting for the debugger on port 8100…

Or

failed to open debugger port localhost:8600 java.net.ConnectException "connection refused"

and the App says:

Waiting For Debugger
Application (process ) is waiting for the debugger to attach.
Force Close

Android Studio version:

Android Studio 2.2.2
Build #AI-145.3360264, built on October 18, 2016
JRE: 1.8.0_112-release-b05 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

Mac version:

10.11.6 (15G1004)

I can successfully connect and run the application, I just can’t get the debugger to attach when I do Run > Debug App > select the connected phone > OK.

I have been able to run the app in debug on other computers (both Windows and Mac) with the same phones.

Various approaches I have tried from hours of debugging and online searching:

  1. Invalidate Caches/Restart Android studio

  2. Check for anything else running on ports 8100, 8600, 8601, etc., nothing comes back when I run the following from bash:

    $ # while Android studio is trying to attach the debugger
    $ lsof -i :8100
    $ lsof -i :8600
    COMMAND  PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
    

    studio 6282 peter 125u IPv4 0x973c7e31bda641ab 0t0 TCP 192.168.1.16:64022->ip-166-62-27-181.ip.secureserver.net:asterix (SYN_SENT)
    $ lsof -i :8601
    $
    $ # after Android studio fails to attach the debugger
    $ lsof -i :8100
    $ lsof -i :8600
    $ lsof -i :8601
    $

  3. Run Tools > Android > Android Device Monitor – it shows my device, but I can’t run Android Device Monitor while I’m trying to debug the app, it says, “Monitor will be closed to enable ADB integration. Continue?” I haven’t found anything useful here.

  4. Searching for and killing adb processes

    ps aux | egrep '(adb|java)'
    ... then doing `kill <pid>` or `kill -9 <pid>` if necessary for the found ones
    
  5. Restart Android studio

  6. Restart my computer

  7. Update Java JDK from the Oracle website—however, I’m not sure why Android Studio still says, “JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o”

  8. Completely uninstall and reinstall android studio – e.g., https://stackoverflow.com/a/18458893/376489 – weirdly, I think this worked for one run of the debugger and then not again…

  9. Tried Run > Attach debugger to Android Process – this didn’t help

  10. Tried running:

    ~/Library/Android/sdk/platform-tools/adb kill-server &&
     ~/Library/Android/sdk/platform-tools/adb start-server
    
  11. Tried unchecking Run > Edit Configurations > Run/Debug Configurations > Skip installation if APK has not changed

  12. Check System Preferences > Security & Privacy > Firewall – this is turned off

  13. Update 1: tried revoking permissions on the phone as well as restarting it

  14. Update 1: muted all breakpoints Android Studio

Are there any other ideas for what might be going wrong or anything meaningful in the details I added above?

Best Answer

Quite embarrassing, but it looks like some time ago I had entered an ip address into my /etc/hosts file to test something as localhost and never removed it. I found a comment on another post saying to check that the following is in your /etc/hosts:

127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost

Upon commenting out that spurious line, the debugger is now working. Given I have a dozen or so other IPs in that file (so I can more easily test remote hosts with a friendly name), I must have not noticed this one at the bottom. I think a more foolproof approach to verifying if this is the problem or not is to ping localhost and verify that it resolves to the ip address of 127.0.0.1, e.g.,

$ ping localhost
PING localhost (127.0.0.1): 56 data bytes

Great comments and suggestions from other folks here too, thanks!