25

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?

Community
  • 1
  • 1
MrColes
  • 2,453
  • 1
  • 28
  • 38
  • Try to revoke usb debugging auths. – daniel.keresztes Nov 26 '16 at 22:17
  • Thanks @KERiii—I revoked on the phone & then restarted Android Studio too—it still leads to `Application is waiting for the debugger on port 8100...` and `Sending WAIT chunk` in the debug console, which eventually times out and I get the popup saying `Unable to open debugger port (localhost:8600): java.net.ConnectException “Connection refused”` – MrColes Nov 27 '16 at 01:17
  • @MrColes Are you debugging over wifi? – PN10 Dec 01 '16 at 11:36
  • Great question @PN10 — I’m using USB. I have successfully used the same USB cord, same phone, and ran the same project on 2 other computers (one mac, one windows) – MrColes Dec 01 '16 at 15:11

12 Answers12

27

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!

MrColes
  • 2,453
  • 1
  • 28
  • 38
  • I too have the debugger attach problem and tried most of the solutions. But similar to you ,i too added an ip to hosts file. But i still have the following lines 127.0.0.1 localhost ::1 ip6-localhost Do you have any suggestions for me? – Anu Jun 16 '17 at 06:47
  • 2
    You are not the only one :) Found the same issue by reading this answer in /etc/hosts, had with another ip and dublicate of localhost. Removed that line and got everything working again. – perborin Apr 19 '18 at 21:02
  • You saved my day. I add 3 lines (above) into hosts file then it works. I can see the logs now. This is commands to edit hosts file. Hope I can help someone. https://stackoverflow.com/questions/17432358/android-studio-logcat-nothing-to-show/28517794 – Leo Nguyen Sep 05 '18 at 06:46
12

I just experienced the same issue. It seems, as if Android Studio Version 2.2 has some issues with a non-attaching debugger as you can see for example here and here.

In my case the following helped:

  • Remove the launch profile for your app by going to Run -> Edit Configurations... and removing the profile there. Don't forget to hit the OK button after you have removed the profile. :)
  • Create a new launch profile from scratch by again going to Run -> Edit Configurations..., hitting the + button, selecting an Android App launch profile and selecting your start module. The other default values of the profile were just fine.

Hope this helps you as well.

Nantoka
  • 4,174
  • 1
  • 33
  • 36
  • You're the man! Thank you. I've experienced strange debugging behaviour and these steps solved 'em. I have no idea what exactly causes these issues but however, I'm glad it works now! – sjkm Mar 07 '19 at 10:23
  • This worked for me, but more importantly, I renamed the old config, instead of removing it. When the new replacement worked, I tracked down the .idea/workspace.xml file containing the data, and diff'd. The only difference was the old config had two directories I added to find symbols, part of an earlier effort to solve a different problem. That caused the debugger to disconnect spontaneously. – donjuedo Oct 15 '19 at 16:29
  • This solved [my issue](https://stackoverflow.com/questions/67624734/android-studio-lldb-hangs-when-debugging-on-specific-device/), thanks! – vesperto May 27 '21 at 16:52
6

I am using a real device. Simply deleting the app on the device, and then running from Android Studio solved this issue.

It seems Android Studio had trouble with overwriting the file that was already on the phone.

Balazs F.
  • 380
  • 5
  • 10
4

1.try cleaning project Build->clean project

2.do file->Invalidate/cache restart if above not helps you..

3.try to change the data cable you are using to connect your phone.

if nothing above helps, install Android wifi ADB plugin and try to do debug over wifi.

Rajesh.k
  • 2,327
  • 1
  • 16
  • 19
  • I installed [Android wifi ADB plugin](https://github.com/pedrovgs/AndroidWiFiADB#installation) via Preferences > Plugins in Android Studio. I ended up with the same result as when I run it over USB ("Error running app: Unable to open debugger port (localhost:8600): java.net.ConnectException "Operation timed out") – MrColes Dec 01 '16 at 15:37
  • Here is a screenshot of the output from the debug console: http://i.imgur.com/el5kCF8.png – MrColes Dec 01 '16 at 15:40
3

Support for a true debug build. Developers no longer need to add the android:debuggable attribute to the tag in the manifest — the build tools add the attribute automatically. In Eclipse/ADT, all incremental builds are assumed to be debug builds, so the tools insert android:debuggable="true". When exporting a signed release build, the tools do not add the attribute. In Ant, a ant debug command automatically inserts the android:debuggable="true" attribute, while ant release does not. If android:debuggable="true" is manually set, then ant release will actually do a debug build, rather than a release build.

In my case i struggling this issue with 5 hour and found that the

minifyEnabled true is the main culprit so change minifyEnabled false work for me

My app build.gradle file look like

 buildTypes {
        debug {
            signingConfig signingConfigs.debug
            minifyEnabled false
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

    }
Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
2

I also got same error and this thing worked for me

1. Open Android Studio -> Terminal
2. Change directory to $Android SDK Path$\platform-tools
3. Write command adb get-state and hit enter.

Then you will get this output

* daemon not running. starting it now on port 5037 *
* daemon started successfully *
Shubham
  • 165
  • 2
  • 11
  • Isn’t that supposed to just do the following: “Prints the adb state of an emulator/device instance.”? Also, when I run that, it just outputs the text: “device” ``` ~/L/A/s/platform-tools > adb get-state device ``` – MrColes Dec 05 '16 at 23:26
  • Definitely it prints the emulator/device state but when daemon is not running, it starts it for you as well. So, in many cases when daemon to failed to load by itself and you try to debug/run your app then it will show the same error that you got `Unable to open debugger port : java.net.ConnectException "Connection refused"` – Shubham Dec 06 '16 at 04:48
2

I have the opposite problem with Android Studio 3.3, trying to debug a wi-fi-connected phone.

After selecting the device in Android Studio, I receive the following message:

Error running 'app': Unable to open debugger port (localhost:8600): java.net.ConnectException "Connection refused: connect"

I also have IPV-6 enabled on my network.

The issue is that "localhost" resolves to ::1 on my machine by default, but ADB studiously binds explicitly to 127.0.0.1. I happen to know this because I spent the day trying to make ADB connect to emulators running on a remote machine (not since Android 4.4 unfortunately.

To solve the problem, add the following line to \windows\system32\drivers\etc\hosts:

127.0.0.1 localhost

Robin Davies
  • 7,547
  • 1
  • 35
  • 50
2

This problem was driving me crazy for about 2 weeks. What finally fixed it for me was changing "auto" to "java" in the debugger process selection window like this: enter image description here

Dwebtron
  • 777
  • 13
  • 27
1

Try using below commands on terminal-

  1. adb kill-server

  2. adb start-server

It works well.

  • 1
    This is a vague answer. You should give more information on why this method works and/or add a link to a reference material. – Taslim Oseni Jun 24 '19 at 10:16
0

Simply not using my google pixel 1st gen and using my new Nokia 7 plus fixed this issue :).

RelativeGames
  • 1,593
  • 2
  • 18
  • 27
0

There is a known issue with Android Studio 4.2.1 on Ubuntu 20.04 when debugging native code.

There is an easy way to check if you are concerned, just run the following command line that should not finish with an error:

${android-studio-dir}/bin/lldb/bin/LLDBFrontend
Victor Paléologue
  • 2,025
  • 1
  • 17
  • 27
-2

At first i tried all the above ways, did not help. I have two computers with same settings. But at one worked at other error. It's given me an idea that it can be "hardware" problem. I switched USB to other slot with USB3.0 and used short cable and WOW it began work. I suggest there some delay in USB2.0 (polling), therefore process begin out of synch.