81

I am using Android Studio 2.3(Latest). Till yesterday it was all good and working, today emulator is not connecting to the data network.
I couldn't find any solution working so far. My Mac is running on Mac OS Sierra, connected to WiFi with no proxy.

enter image description here

Zoe stands with Ukraine
  • 25,310
  • 18
  • 114
  • 149
Amit
  • 3,052
  • 1
  • 25
  • 37

11 Answers11

272

In Mac OS go to: System Preferences -> Network -> select Wi-Fi os left panel -> Advanced on right panel -> DNS -> add new DNS server; for example 8.8.8.8 and 8.8.4.4 (Google Public DNS) or 1.1.1.1 and 1.0.0.1 (Cloudflare and APNIC DNS) or another public DNS provider. Then restart the emulator so the changes take effect.

DNS setup

Edited jun/2020

Another option is to pass dns-server params when start Android emulator. According with this solution https://stackoverflow.com/a/51858653/3328566, I changed the emulator executable name and I created a bash script to load the AVD with param -dns-server 8.8.8.8.

In your Android SDK default folder /Users/[MY_USER_ACCOUNT]/Library/Android/sdk/emulator/emulator

  1. Rename the binary emulator to emulator_original
  2. Create a bash script named emulator that contains:

#!/bin/bash /Users/[MY_USER_ACCOUNT]/Library/Android/sdk/emulator/emulator_original -dns-server 8.8.8.8 $@

  1. Change the script permissions with chmod +x emulator

Now, you can start AVD from Android Studio normally

In this case, you don't need to set DNS server in System Preferences. You are setting the DNS server only for the emulator, avoiding other problems

ikaioi
  • 2,894
  • 1
  • 11
  • 8
70

For me the issue appears to stem from the DNS settings my company enforces.

In order to be able to get network access for my emulator I needed to launch the emulator with the same corporate dns-server specified.

I'm on a Mac, so first I checked my network settings to find what my DNS was set to:

System Preferences -> Network -> Wi-Fi -> Advanced -> DNS

Then navigated to the sdk emulator location (for convenience):

cd ~/Library/Android/sdk/emulator

Then listed the available emulators:

./emulator -list-avds

Then ran the desired emulator with dns server override:

./emulator @<emulator_name> -dns-server <dns.server.ip.address>

It would be nice if I could set this DNS to be used by emulators launched through Android Studio, but hopefully these steps help someone else in a similar position.

Jez
  • 1,086
  • 8
  • 10
  • When I run this command line ./emulator -list-avds, I do not load an emulator – dbenitobaldeon Jun 28 '19 at 15:31
  • What are dns settings? – IgorGanapolsky Jun 28 '19 at 20:38
  • @IgorGanapolsky your DNS settings are likely to be what your company computer is using. If in doubt contact your network administrator. – Jez Jul 01 '19 at 08:09
  • @dbenitobaldeon 1) ensure you've downloaded an android emulator through android studio 2) ensure that the path `~/Library/Android/sdk/emulator` does indeed point to a folder containing an `emulator` script - otherwise look up the location of your android studio emulator install directory and use that instead. The path will obviously be different if you're on a windows machine. – Jez Jul 01 '19 at 08:12
  • 1
    In case I misunderstood your initial question @IgorGanapolsky `DNS` means "Domain Name Server", the place your computer looks to find out what IP corresponds to a website name. Eg, `google.com` might resolve to the IP address `216.58.198.174`, which is what your computer will actually use to find the website. Your DNS settings specify what server your computer will use for this service. – Jez Jul 01 '19 at 08:18
  • 1
    @IgorGanapolsky open `System Preferences` select `Network` then `Wi-Fi` then `Advanced` then `DNS`, and you should see a pane containing a sequence of numbers, such as `8.8.8.8`. This is a DNS address. – Jez Jul 01 '19 at 21:48
22

I'm new to Android Studio and just ran into this issue. Network in the sim was working fine and stopped working for some reason. Didn't like any of the solutions above, so I poked around the AVD Manager and found an option to wipe the data on the sim.

  1. quit the sim
  2. open AVD Manager
  3. Actions > open down arrow for more options
  4. select Wipe Data
  5. restart sim

Wipe simulator data in AVD Manager

knarf
  • 583
  • 1
  • 6
  • 15
11

Go to open AVD manager and click wipe data

Click on the wipe data

That's it now the Internet will work. This is how I solved my Issue.

5

It's a bug with IPv6 name resolution, removing any IPv6 nameserver in /etc/resolv.confg fixes the issue, see https://issuetracker.google.com/issues/155686508#comment3

saschpe
  • 801
  • 7
  • 8
  • This is it kind sir!! Adding Google's DNS to MacOS's system preferences didn't work. Wiping AVD data didn't work. This for me is the solution! Thanks!! – Etchelon Sep 02 '21 at 07:59
5

i tried purge all android studio files and reinstall, start with -dns-server, wifi dns to 8.8.8.8, none of them not working for me. i found it only can using ip address in emulator. but this post saved me. https://www.bswen.com/2021/08/others-how-to-enable-android-emulator-internet-access.html.
1\ turn off you macos wifi; 2\ cold boot emulator; 3\ waiting the emulator's wifi connected (limited connection but it's ok) 4\ turn on you macos wifi; it's working now.

Jarvis Zheng
  • 51
  • 1
  • 2
4

Couldn't find any solution by tweaking network settings. So added a new virtual device from Tools -> Android ->AVD Manager by downloading a new system image(Android O, API 26). And it's working now.

If you want to use the same API level then make sure to delete the existing system image and download it again.

Amit
  • 3,052
  • 1
  • 25
  • 37
3

You can go to: System Preferences -> Network -> Wi-Fi -> Advanced -> DNS

So you add a new DNS 8.8.8.8. It might solve your problem.

2

If you have Blue Coat Unified Agent, internet wont work. Kindly uninstall it.

It can be uninstalled by going to below folder- /Library/Application Support/bcua

RaghuD
  • 31
  • 1
-1

There was an update available to my Android Studio, i updated it and it worked!

Anderson Bressane
  • 358
  • 1
  • 5
  • 15
-5

if its an android project, u can change the baseUrl to 10.0.2.2, note this is only applicable from android emulator, will not work on phone

e.g Api endpoint will now look like this: val baseUri : String = "http://10.0.2.2/restapi/"

val loginEndpoint = "${baseUri}login"

kingsley
  • 15
  • 2
  • I wonder in what scenario this might work. Does the url `10.0.2.2` replace `localhost`? In any way, this is not an answer to connect to the internet, but only to your api. – Randy Aug 12 '20 at 08:43