justarandomuser12345
justarandomuser12345

Reputation: 253

How to set the Android emulator DNS server from Android Studio

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?

Upvotes: 20

Views: 27407

Answers (3)

Serge
Serge

Reputation: 479

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.

Upvotes: 0

m_katsifarakis
m_katsifarakis

Reputation: 1907

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" $@

Upvotes: 10

jcady
jcady

Reputation: 3968

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

Upvotes: 11

Related Questions