Android emulator -dns-server usage

2020-03-24 03:19发布

问题:

I am currently somewhat struggling to get the Android emulator to use a custom DNS server, which is running on localhost (127.0.0.1).

As of now, I have tried various approaches trying to convince the emulator to use my DNS server, so far without success. In the end, it always boils down to the following error message:

### WARNING: can't resolve DNS server name 'localhost'
### WARNING: will use system default DNS server

localhost, however, resolves correctly when supplied to nslookup:

$ nslookup localhost
Server:  dnszrh01.xxx
Address:  10.xxx.xxx.xxx

Non-authoritative answer:
Name:    localhost.xxx
Address:  127.0.0.1

Using the IP address instead of the hostname also does not help much, it just changes the error message to:

### WARNING: can't resolve DNS server name '127.0.0.1'
### WARNING: will use system default DNS server

The extra command line I am using to start the emulator reads:

-http-proxy http://proxy.xxx:8080 -dns-server 127.0.0.1 -debug-proxy -verbose

This is happening with Android emulator version 5.0 (build_id ECLAIR-24846) running on Windows.

Any hint how to get this working is much appreciated!

回答1:

A couple things...

1) I've noticed I get this error also when I have no network connectivity at all. ie. No wireless or ethernet connection.

2) Excerpt from Emulator guide: http://developer.android.com/guide/developing/tools/emulator.html#dns

Configuring the Emulator's DNS Settings At startup, the emulator reads the list of DNS servers that your system is currently using. It then stores the IP addresses of up to four servers on this list and sets up aliases to them on the emulated addresses 10.0.2.3, 10.0.2.4, 10.0.2.5 and 10.0.2.6 as needed.

On Linux and OS X, the emulator obtains the DNS server addresses by parsing the file /etc/resolv.conf. On Windows, the emulator obtains the addresses by calling the GetNetworkParams() API. Note that this usually means that the emulator ignores the content of your "hosts" file (/etc/hosts on Linux/OS X, %WINDOWS%/system32/HOSTS on Windows).

When starting the emulator at the command line, you can also use the -dns-server option to manually specify the addresses of DNS servers to use, where is a comma-separated list of server names or IP addresses. You might find this option useful if you encounter DNS resolution problems in the emulated network (for example, an "Unknown Host error" message that appears when using the web browser).

3) Also see this other StackOverflow question: Android emulator doesn't use Windows host file?