23

I'm using Android Studio v1.0.1 to build an app in macOS Yosemite 10.10. When I try to debug the app, it installs correctly but has the error

Error running [app]: Unable to open debugger port : java.net.ConnectException "Connection refused"

When I run the DDMS, it shows the connected devices and the process. The trouble occurs just when trying to debug in Android Studio.

In other forums, I've found that something could have changed the port of the debugger, but that doesn't solve my issue.

SOLO
  • 868
  • 9
  • 19
Kleiber J. Perez
  • 293
  • 1
  • 2
  • 8

12 Answers12

24

Your debug port is probably busy (in use by another process). You can kill all the process associated with the ADB debug port (8601 or higher) using this:

fuser -k 8601/tcp 

UPDATE:

Under OSX, lsof should do the job in substitution of fuser:

lsof -i :8601
Community
  • 1
  • 1
bonnyz
  • 13,458
  • 5
  • 46
  • 70
  • 1
    trying to use fuser in mac returns that -k is unknow option – Kleiber J. Perez Feb 02 '15 at 18:07
  • @KleiberJ.Perez yay, this is a unix command not sure about how it works under OSX – bonnyz Feb 02 '15 at 18:20
  • 1
    @KleiberJ.Perez I've updated my answer with a precisation regarding `fuser` and OSX – bonnyz Feb 02 '15 at 18:42
  • 1
    Do we run this from any directory? Couldn't get the mac one to work. – Azurespot May 13 '16 at 21:44
  • how can I do it in windows 7? my port number is 5037( at least it says so when I do adb get-state --> *daemon not running.starting it now on port 5037 * – Raulp Jan 10 '17 at 13:39
  • The above command kills android studio, Again we will have to restart android studio , then only it works – Sudhir Belagali Mar 28 '17 at 10:08
  • For windows using bash (win10) you can easily do `adb kill-server; adb start-server` – VonSchnauzer Apr 28 '17 at 11:54
  • 1
    adb kill-server then adb start-server both execute successfully and then running the project returns this message again anyway. Restarting AS after restarting adb and then running the project also returns this message. No solution for me here. – person27 Oct 31 '17 at 06:35
15

I have also met this problem. and I think the my solution may help others, so I post it here.

First, you should know what will cause "connection refused" problem. Usually there are two possible reasons:

  1. This particular server is not started.
  2. The server is started but not accept any connection.

As for your problem, I suggest you to first start your Android Device Monitor(DDMS) from your android studio, and DO NOT CLOSE IT.

Then in the DDMS, you can select the package you want to debug and "Update Threads", and now you can debug this application in your studio.

VicX
  • 721
  • 8
  • 13
  • This definitely helped me to see what was going on. I had to close the DDMS eventually to get the AS debugger to connect using ADB 'connect to process' but opening the DDMS was a great help for seeing which devices where connected and which processes where running on each device so +1. – Litome Dec 15 '15 at 14:23
  • This also worked for me. The accepted answer did not. – user1549672 Mar 08 '16 at 02:17
  • I do not follow this… once one has DDMS running and is able to view an app on a device and select “Update Threads”, then what next? Is there something meaningful that can be found in any of the running threads? – MrColes Oct 20 '16 at 22:18
  • This helped but why... Attaching debbuger after this worked, but building debug still do not work, I invalidated caches, killed adb, exit AS, deleted app from phone. Then open AS, made run app w/o debug, open DDMS, update threads, run debug and after this seems to work. – Tomasz Kryński Jan 12 '18 at 11:37
9

My problem Solved by below steps:

1. Invalidate Caches/Restart Android studio

2. Restart you Emulator.

Done for me.

Dhaval Jivani
  • 9,467
  • 2
  • 48
  • 42
4

Restarted my Android Studio, worked for me.

Anjan Kant
  • 4,090
  • 41
  • 39
3

I had this problem when I was using a real device, I just unplugged the USB cable and then again plugged it in and it worked. In case of emulators, I guess restarting the emulator will work. This works because disconnecting the device/emulator closes all connected processes, and then you start your required process.

Shubham Raitka
  • 1,034
  • 8
  • 15
1

For me none of the above solved and got stuck in this for months until I figured this solution. I had a modified version of my HOST file in my mac machine like pointing the IP 127.0.0.1 to a custom domain like www.mymac.com. Once I reverted that then I am able to debug. Yes!!! hope this helps for some one.

1

It was a /etc/hosts file issue for me as well, I changed the following line (dont know why it was working before ... after upgrade to studio 3.1.3 it stopped working though!)

127.0.1.1    localhost 

to

127.0.0.1    localhost 
Robert
  • 5,278
  • 43
  • 65
  • 115
Bakavani
  • 101
  • 5
1

For Windows user you can kill the port by using the following command ..

Step 1:

Open the cmd (note: you may need to run it as an administrator, but this isn't always necessary), then run the below command:

netstat -ano | findstr :PORT_NUMBER

enter image description here

Step 2:


Syntax:
taskkill /PID PORT_NUMBER /F

cmd:
taskkill /PID 5005 /F

enter image description here

0

You can use netstat utility to see what is listening on what ports and, if you are quick, what tries to connect to what ports. This will help to to ensure that you have your process listening on a debugging port and confirm its number.

Andre
  • 637
  • 6
  • 16
  • 1
    there are a lot of different ways to use `netstat`, what commands would you run to look at this? – MrColes Nov 26 '16 at 20:30
  • @MrColes Try this: Windows -->> netstat -anto | findstr 8616 Linux: netstat -anto | grep 8616 Where 8616 is the port number you are interested in. You can also experiment with -ano flags in order to display all NET protocols. (-t means only TCP) – Sold Out Aug 06 '17 at 16:15
0

I also solved this by making 127.0.0.1 in my Mac hosts file NOT point to a custom domain. One thing to note, I had 2 host files, one in "/" and one "/etc". One in "/etc" was actually used.

0

I my case mackbookpro: 20:21 Can't bind to local 8600 for debugger

I find /ect/hosts file is blank. so,I add the default hosts config to /etc/hosts file, It works fine.

127.0.0.1   localhost
255.255.255.255 broadcasthost
::1             localhost
androidmalin
  • 899
  • 1
  • 8
  • 12
0

I encountered same issue when running application on a physical device.

Just connect your PC and mobile to the same WiFi Network and disable the firewall of your PC. It worked for me.

prasuna_16
  • 11
  • 4