26

I want to debug my android application, I connect the android device to my PC using the USB cable. In my application there is a button to connect with localhost, ip for localhost is 10.0.2.2 and the port is 8080, I have read that when debugging on mobile, the ip 10.0.2.2 is the localhost for android device and not for my PC, so what changes should I make to the ip instead of 10.0.2.2? or do I have to make another change? In this case my android device is sony ericsson xperia arc s.

nikhil
  • 8,925
  • 21
  • 62
  • 102
William Kinaan
  • 28,059
  • 20
  • 85
  • 118

5 Answers5

27

Google has added support in Chrome 29 and higher to use reverse port forwarding to access a website hosted on your local development machine through the USB cable on Chrome for Android. Setup instructions can be found at the following URL:

As of desktop Chrome 30 Reverse Port Forwarding is no longer an experimental feature in Chrome. It can be accessed by typing about:inspect in the address bar of your PC, and by clicking the "Enable port forwarding" check box and clicking the "Configure port forwarding" button located to the top right of the window.

Once that is done, connect your mobile device via USB. Open Chrome on your mobile device to localhost:8000 (or whichever port you have configured on your local server).

The Reverse Port Forwarding functionality will make sure that your Android device now sees your PC's localhost.

J.Hogan
  • 430
  • 5
  • 7
  • Chrome has made this very easy now - no more configuration of your local server, it all gets done automatically. It also gets around the problem of having to configure your firewall to allow external connections, which many of the other techniques require. – Richard Le Mesurier Mar 25 '15 at 16:39
  • 1
    The correct answer should be changed to be this one. – Jose Rui Santos Oct 20 '15 at 09:50
  • 1
    I think this should actually link to https://developers.google.com/web/tools/chrome-devtools/remote-debugging/local-server –  Jun 15 '17 at 14:45
  • This also says nothing about configuring the firewall on the dev machine. –  Jun 15 '17 at 14:48
  • I'm trying this right now and the port connection is timing out when I'm making an API request to the port 8080. – Eric Conklin Oct 17 '22 at 13:19
20

As 10.0.2.2 is your system (pc)'s local host address (from emulator only). Actually android doesn't recognized localhost in url. so 10.0.2.2 is for that meant. Also for android device loopback address is 127.0.0.1.

Your url with 10.0.2.2 is correct. Also you can use Static IP of your system.

Just check for

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

in your application's manifest file.

EDIT:

Here you are using port 8080 so, try with adb command on your host machine.

adb forward tcp:8080 tcp:8080

Also please elaborate on this line "i want to debugger my application on my mobile".

Note:

If you are going to test on real device use your Network IP of system (PC).

user370305
  • 108,599
  • 23
  • 164
  • 151
  • i already set that permission, so y i can't access my localhost from mobile ? – William Kinaan Jul 04 '12 at 11:57
  • if am always afraid of cmd so i will ask : if i did this commad `adb forward tcp:8080 tcp:8080` can i keeping working on emulator? and is xampp will still working, and is there anything i will lose? – William Kinaan Jul 04 '12 at 12:03
  • 1
    When you are communicating with USB port, by adb forward command you can open particular port for communication with your app and your host machine (server-client). You can't lose anything. – user370305 Jul 04 '12 at 12:06
  • Using adb forward - Forwards socket connections from a specified local port to a specified remote port on the emulator/device instance. – user370305 Jul 04 '12 at 12:08
  • now i changed the `10.0.2.2` to `127.0.0.1` , but still doesn't work, i will run the command and till you what happend, thank you for the nice answer – William Kinaan Jul 04 '12 at 12:08
  • Don't change 10.0.2.2.And 127.0.01 is for loopback address of emulator. only use 10.0.2.2 for local host. – user370305 Jul 04 '12 at 12:10
  • 1
    i make that command , and still `10.0.2.2` but the mobile doesn't connect to the server – William Kinaan Jul 04 '12 at 12:14
  • 20
    This answer should not have been accepted as it neither solves nor identifies the actual problem. The fundamental problem is that the 10.0.2.2 address is only available to *emulators* and not to physical devices. – Chris Stratton Apr 30 '13 at 18:25
  • I am using port `8000` so should i use abd ? – Moeez Feb 08 '17 at 05:49
  • my friends, try `adb reverse tcp: tcp:` – SinaMobasheri Jun 08 '23 at 16:54
5

For that you need to make some changes in your xampp server... Assign 1 static IP address to your system and then you need to put your xampp server in online mode. after that you can use that ip address in your android application instead of 10.0.2.2. Works fine for me as i am using my localhost with my android application.

Cool Compiler
  • 857
  • 2
  • 9
  • 20
  • my server is tomcat , and to deal with database `mysql` i am using xampp, if that xampp for database is what you mean by xampp server, tell me please how can i assign static ip to system , and how can i put xampp server in online mode, but i told you that xampp just for database , myserver is java tomcat on eclipse – William Kinaan Jul 04 '12 at 12:20
  • Jus check the ip address of your system. Since you are using tomcat server then no need to put xampp in online mode, but it should be in running mode. you can use your system ip plus your tomcat port number to access your web service file. eg if ur system ip address is 192.111.1.111 and ur tomcat port number is 8080(by default) then u need to use 192.111.1.111:8080 instead of 10.0.2.2... – Cool Compiler Jul 04 '12 at 12:34
3

You can share your Mac's Internet connection over Wi-Fi. Then your Android app can connect to a Servlet running on the Mac with HTTP over Wi-Fi. The steps are:

  1. Run System Preference on Mac
  2. Goto "Sharing" tab
  3. Turn on "Internet Sharing"
  4. Select "Ethernet" in the "Share your connection from" combo
  5. Select "Wi-Fi" in the "To Computers Using" list box
  6. Use "Wi-Fi Options..." button to configure Wi-Fi security. Now your Mac is a Wi-Fi server, and it is sharing its Ethernet Internet connection.
  7. Configure your Android device's Wi-Fi to connect to your Mac (in Settings command)
  8. On your Mac, goto the Network tab in System Preferences, and select Wi-Fi in list to find out the IP address of your Mac on the Wi-Fi network (for me it was 169.254.66.223)
  9. In your Android App you can now connect to the Servlet in your Mac with "http://169.254.66.223:8080/YourServer/YourServlet"
Till Helge
  • 9,253
  • 2
  • 40
  • 56
Adam Gawne-Cain
  • 1,347
  • 14
  • 14
0

I think you have two options

The first one is using 10.0.2.3 when you use your real android device.it works for me.

Your Second opt is creating hotspot from your pc and connect your android device to the hotspot.

Find the ip address using cmd type "ipconfig" replace localhost with the ip address.

Thanks.

yosef girma
  • 181
  • 2
  • 2