How to get the Android Emulator's IP address?
Asked Answered
Z

8

94

I want to get the currently running Android Emulator's IP address through code. How can it be achieved?

Zeph answered 12/11, 2009 at 6:44 Comment(3)
Perhaps related here and here.Cycad
@Cycad Second link is broken. :)Barcroft
@MuhamedHuseinbašić I feel this thread solved the problem in the second thread so decided to delete it to keep things simple.Cycad
P
188

Just to clarify: from within your app, you can simply refer to the Emulator as "localhost" or 127.0.0.1.

Web traffic is routed through your development machine, so the Emulator's External-IP is whatever External-IP has been assigned to that development machine by your internet-provider. The development machine can always be reached from your device at 10.0.2.2.

If you have multiple Emulators launched, where adb does not work, unless you pick one by Emulator's Local-IP (like adb -s 192.168.232.2:5555 shell), then:

  • Just like a real Android device,
  • In Emulator, swipe down from top-most, to open menu,
  • In the menu, press and hold on WiFi,
  • Finally, go to settings of current WiFi-connection,
  • There you should see IP of Emulator process.
Paleoclimatology answered 12/11, 2009 at 14:7 Comment(4)
I want to upvote you 100 times for this. I couldn't figure out how my app could talk to another local server I had running until I read your post. Thanks!Deci
How can you reach your development machine from 10.0.2.2? I tried pinging but nothing, pic here. Ifconfig -app on the right and terminal-emulator on the left trying to ping dev-machine. Cannot see anything in localhost, any simple tests?Cycad
@Cycad You can try JuiceSSH: play.google.com/store/apps/details?id=com.sonelli.juicessh You can reach your host from the mentioned IP or from the real IP of the host. Both variants work for me.Barcroft
I believe the traffic from an emulator is routed over a "virtual" router first (with IP address of 10.0.2.1, not directly. So, there should be one extra connection takes place between this router 10.0.2.1 to the host/dev machine 127.0.0.1/10.0.2.2. What I really usually change the port number I want to connect to.Regenerator
A
43

If you do truly want the IP assigned to your emulator:

adb shell
ifconfig eth0

Which may give you something like:

eth0: ip 10.0.2.15 mask 255.255.255.0 flags [up broadcast running multicast]

Or, if above fails, try:

adb shell
ifconfig wlan0

And look for inet addr: in the output, for example, my Emulator logs inet addr:192.168.232.2

Abuzz answered 18/8, 2012 at 23:24 Comment(3)
Luise said: I tried Derek method. it can ping within adb shell.Linkwork
I get this error after adb shell: ifconfig: eth0: No such deviceany ideas why?Guard
@Guard it might be under a different interface like wlan0Spurgeon
A
27

Like this:

public String getLocalIpAddress() {
    try {
        for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}

Check the docs for more info: NetworkInterface.

Aholla answered 12/11, 2009 at 7:6 Comment(2)
And don't forget 'the android.permission.INTERNET' permissionLeathers
This gives IP6 address, how to get IP4?Constitutionality
J
21

Use this method you will be getting 100% correct ip address for your android emulator

To get the ip address of yoor emulator

Go to adb shell and type this command

adb shell
ifconfig eth0

enter image description here

After running this command I am getting

IP : 10.0.2.15

Mask : 255.255.255.0

Which works for me . I am also working for an networking application.

Jeanettajeanette answered 18/5, 2013 at 11:11 Comment(2)
The emulator no longer has 'eth0', but has 'radio0' and 'wlan0' instead. You may want to edit this answer, because it is no longer 100% correct.Tennessee
I'm on macOS. ifconfig: en0: No such device.Levitan
R
12

If you need to refer to your host computer's localhost, such as when you want the emulator client to contact a server running on the host, use the alias 10.0.2.2 to refer to the host computer's loopback interface. From the emulator's perspective, localhost (127.0.0.1) refers to its own loopback interface.

More details: https://developer.android.com/studio/run/emulator-networking#networkaddresses

Rhu answered 18/2, 2013 at 9:48 Comment(1)
This answer worked for me. Remember to include the computer's port: http://10.0.2.2:3000/api, for example, for a web api.Thanos
G
3
public String getLocalIpAddress() {

    try {
        for (Enumeration < NetworkInterface > en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
            NetworkInterface intf = en.nextElement();
            for (Enumeration < InetAddress > enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                InetAddress inetAddress = enumIpAddr.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress().toString();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e(LOG_TAG, ex.toString());
    }
    return null;
}
Galvan answered 12/1, 2012 at 6:26 Comment(0)
A
0

Within the code of my app I can get the running device IP andress easily like beolow:

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
Apomixis answered 5/5, 2020 at 3:45 Comment(0)
T
0

Normally the IP address of the android emulator is 10.0.0.2 you check it from the Wi-Fi setting in the emulator

Trev answered 25/2 at 7:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.