20

Essentially my issue is that when I run emulator -verbose -avd Nexus_5X_API_19 in the command line the emulator starts up with the argument -dns-server = "w,x,y,z" where w,x,y,z are 4 ip addresses for DNS servers. When I run ipconfig /all I only see x,y,z listed as my valid DNS servers in Windows. Because of this odd first DNS server, I am unable to access the internet within the emulator. When I run the emulator with emulator -verbose -avd Nexus_5X_API_19 -dns-server "x,y,z" everything works fine.

But now I want to be able to run my app from within Android Studio 2.2.3 with the corrected DNS servers. So does anyone know how to specify the emulator command line arguments within Android Studio (similar to this answer for the older Eclipse based version: https://stackoverflow.com/a/4736518/1088659), or how to set the default DNS for the emulator to start with?

Community
  • 1
  • 1

3 Answers3

11

Unfortunately, as of 3.0.1, this isn't possible. They removed adding additional arguments for emulators launched from Android Studio. Until they add it back in, starting the emulator from the command line (as you showed) is the only option.

You can track this issue here: https://issuetracker.google.com/issues/37071385

jcady
  • 3,850
  • 2
  • 21
  • 21
  • 1
    Although an old issue this one is still not resolved and I was hit too. In my case it was an ipv6 DNS server which was added when I connected with a new network. OSX added two DNS servers in `/var/run/resolv.conf`: an ipv4 and an ipv6 entry. When I removed the ipv6 entry I was able to start the AVD from Android Studio so that the emulator could connect to the internet again. – Jürgen Jatzkowski Aug 28 '18 at 19:21
8

If you are on MacOS or Linux, you could rename the Android Emulator executable to something else (say emulator-binary) and create a script with the actual emulator name (emulator) in its place, that calls the executable with the -dns-server parameter.

Here are the steps required:

  1. Find the path where the Android SDK is located in your system. This answer will help you find it.

  2. cd <your-SDK-path>/emulator.

  3. Rename the original executable: mv emulator emulator-binary.

  4. Finally, create an emulator shell script named emulator with the following contents:

<your-SDK-path>/emulator-binary -dns-server "8.8.8.8,8.8.4.4" $@

m_katsifarakis
  • 1,777
  • 1
  • 21
  • 27
  • Giving it the 8.8.8.8 address is not always a good idea. In a work environment you'll often have servers with access restricted to the local network. See my answer for getting the DNS directly from settings. – Serge Mar 19 '21 at 09:41
0

Unfortunately, I ran into the same issue. I wrote a simple script that pulls the DNS address from settings automatically. Script was written for MacOS, but should work for Linux just the same. Note: You may need to change the path to the Android Emulator script.

#!/bin/bash
set -m
dns=$(grep "nameserver" /etc/resolv.conf | grep "\." | awk '{split($0,a," "); print a[2]}')
echo "Running emulator with DNS address: $dns"
~/Library/Android/sdk/emulator/emulator -avd $1 -dns-server $dns -no-snapshot-load &>/dev/null &
disown

You just need to give it your emulator name (note: spaces become underscores), e.g.:

runemu Pixel_4_API_30

where "runemu" is what I named the script on my machine.

Serge
  • 359
  • 2
  • 13