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!
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?
Related
I did a question since I'm testing my react-native/express app by using usb debug on android and I'm still getting a network request failure:
How to connect to the express development server using adb reverse?
Since I couldn't find a solution I tried to read the docs and figure out how it's the communication among different parts.
Cases
Commands used are those described in React-Native docs but it could differs. Although I guess there are not too many options:
https://reactnative.dev/docs/running-on-device
https://reactnative.dev/docs/signed-apk-android#testing-the-release-build-of-your-app
Case 1: running only using the dev machine and emulator
Case 2: running using the usb debug
Case 3: wifi (I didn't try yet)
Case 4: uploading to play store by bundleRelease
Elements:
Dev machine:
Local host: 127.0.0.1
Local IP (from windows 10: settings, network & internet, wi-fi; eg 192.168.1.1)
Server
Device
Emulator
ADB
Daemon
Ports usage:
From my dev machine:
Server (eg 5000)
React-native: 8081 (default)
Daemon: 5037
From device: I guess is used only the 8081 port.
Emulator:
10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
10.0.2.15 The emulated device network/ethernet interface
127.0.0.1 The emulated device loopback interface
Connections
React-native emulator mounts over the 8081 port of the dev machine local host. Since it "replaces" the local host adress if you wanna access to another port you have to use the alias 10.0.2.2 (eg server running at 5000 will be: 10.0.2.2:5000).
Till then my app works well setting the communication following the instructions above mentioned. But when I try to use the USB debug I can't connect to the server and I'm getting puzzled about what's going on behind the scene.
I wrasp the idea of the emulator running as an isolated router to which is connected the tested device. I guess Daemon runs as a background process reloading every time that the server file is edited. But I can understand why I can't connect to the server.
Resources
Android
https://developer.android.com/studio/run/emulator-networking.html
Note that the same address assignments are used by all running emulator instances. That means that if you have two instances > running concurrently on your machine, each will have its own router and, behind that, each will have an IP address of 10.0.2.> 15. The instances are isolated by a router and can not see each other on the same network. For information about how to let > emulator instances communicate over TCP/UDP, see Interconnecting Emulator Instances.
Also note that the address 127.0.0.1 on your development machine corresponds to the emulator's own loopback interface. If you > want to access services running on your development machine loopback interface (a.k.a. 127.0.0.1 on your machine), you should > use the special address 10.0.2.2 instead.
Finally, note that the pre-allocated addresses of an emulated device are specific to the Android Emulator and will probably be > very different on real devices (which are also very likely to be NAT-ed, specifically, behind a router/firewall).
http://adbcommand.com/adbshell
https://developer.android.com/studio/command-line/adb
Locates emulator for each connection by a using a pair of ports (console/adb) in the range of 5555 to 5585
When the server starts, it binds to local TCP port 5037 and listens for commands sent from adb clients—all adb clients use > port 5037 to communicate with the adb server.
Adb graph connection of my own (this is in case of several devices connected but if I'm not wrong it'll work similarly to one device connected):
devMachine: client -- server
-- daemon -- || -- daemon -- : running device 1
-- daemon -- || -- daemon -- : running device 2
-- daemon -- || -- daemon -- : running device 3
https://developer.android.com/training/articles/security-config
I'm attempting to test my local dockerised API against a staging version of my app.
I don't have access to the repository to build the App myself so I'm relying on changing the IP address within the emulator to point at my local machine and that's where I'm failing.
I've set it to 10.0.2.2 as per the emulator documentation, and when I ping the staging url from the ADB shell (ping mystagingsite.com) it responds data from 10.0.2.2 . But from within the app, the response is always empty. I've tried 127.0.0.1 which obviously won't work, and I've tried my network IP address.
Is what I'm trying to do possible? If it is, what is the right IP address to put in the /etc/hosts file of the emulator and how do I find that address?
So I have set up charles proxy to monitor traffic that goes out of the android emulator.
Now when I open a website it in the Android browser For eg:- google.com I see google.com in charles proxy.
But when I open an app(say instagram app) in the emulator and if the app makes some network calls , I see ip addresses on charles proxy and not domain names.
Because of which the app gives me an error of something went wrong etc.
Can anyone tell me how to change the dns configs of the android emulator so that it sends hostnames instead of IP addresses?
Or any other solution to this problem.
Thanks
you can configure a proxy from the command line with the -http-proxy option when starting the emulator. In this case, you specify proxy information in in one of these formats:
http://<machineName>:<port>
or
http://<username>:<password>#<machineName>:<port>
The -http-proxy option forces the emulator to use the specified HTTP/HTTPS proxy for all outgoing TCP connections. Redirection for UDP is not currently supported.
Alternatively, you can define the environment variable http_proxy to the value you want to use for . In this case, you do not need to specify a value for in the -http-proxy command — the emulator checks the value of the http_proxy environment variable at startup and uses its value automatically, if defined.
You can use the -debug-proxy option to diagnose proxy connection problems.
You can get further detail about this topic here:
https://developer.android.com/studio/run/emulator-networking.html
There's no way to change the dns configs of the android emulator that can get hostnames instead of IP addresses, the only option to use proxy is explain on other SO threads here.
I am sure the packets are coming from instagram and/or maybe they use some firewall for security purpose.
It is not possible with emulator but you can use GENYMOTION for that. Genymotion is very good tool to use online emulator.
You can set proxy in setting->network.
try that .hope this will help you.
For Linux User
We can't able to use emulator started from android studio
We have to start emulator from terminal using this 2 commands
First Move to android sdk tools dir
$ cd /home/..../Android/Sdk/tools
Second start your emulator using this cmd
./emulator -avd Emulator_name -http-proxy http://Ur ip address:port/
Emulator_name : open AVD Manager second column you will get Emulator names, next you have to replace spaces in between name with underscore
like Nexus 5 API 22 --> Nexus_5_API_22
Eg: $ ./emulator -avd Nexus_5_API_22 -http-proxy http://12.1.0.222:8888/
Inside your emulator - go to settings ->network&internet ->mobileNetwork ->AccessPointNames. and then choose your network for editing it and put the IP and port of the Charles's computer.
Remember to open the emulator with poxy.
emulator with proxy
You can use 'ping' command to get the IP address.
Background Info
I got a Samsung GT-S5830, running Android 2.3.4. The device has been successfully rooted.
I edited the WIFI settings to connect to my home network, so now I can surf the Internet event though there is no simm card. The network configuration is static, as follows:
IP : 192.168.0.7
Mask : 255.255.255.0
Gateway : 192.168.0.1
DNS1 : 192.168.0.1
In my local machine, I've installed the Android SDK tools (ADB).
From that console, I issue the following commands:
1) C:\> adb shell
2) $ su
3) # (now successfully logged as root)
Once I am logged in as root (the symbol # is showing), I can successfully ping the following addresses:
192.168.0.7 (the handheld address)
192.168.0.1 (the gateway)
127.0.0.1 (loopback)
I also can ping intranet sites such as Google or FB
The problem
However, I can't ping any other machine in the intranet, eg 192.168.0.4, which is one of my local servers.
The command netcfg shows the following information:
Did anyone had to face the same problem?
Many thanks for your help.
Well, I couldn't work out what's causing the problem, but I've found a workaround.
Although the device is connected to the intranet, it make the requests from the 'outside'. As you can see in the screenshot above, the network adaptor isn't anything like 'eth0' but 'wlan0'. In other words, it's searching for the public IP of the web server
What I did is to configure the firewall to accept inbound traffic on the port 80 (web). Doing this way, I was able to render my webpages on the device.
I having problem in using my system hosts file details in Android Emulators.
I have my website (www.example.com) deployed in Webserver.
I am trying to access those website in Emulator from my desktop.
My desktop has a hosts file with the entry of
10.xx.xx.xx www.example.com
I am trying to access the www.example.com from the android emulator - browser and it is not working.
I took a reference from the below website, but most of the website says how to use the hosts file if the website is deployed in the same server.
http://sadhanasiblog.blogspot.in/2012/07/local-environment-setup-for-android.html
Please let me know if anyone has answers. Thanks in Advance.
-Senthil
With latest Android studio and tools. You can now use follow this instructions. You only need to run this once per emulator.
Start the emulator with following command. This command is located under your [sdk folder]/tools directory
emulator #[emulator name] -writable-system
Open another command prompt and then switch your directory to [sdk folder]/platform-tools and run following commands
adb root
adb remount
adb shell
echo '10.0.2.2 [localserver dns name]'>>/etc/hosts -- example echo '10.0.2.2 xxxx.com'>>/etc/hosts
One important thing to check is, if you create your HOSTS file on Windows or MacOS machine, that the HOSTS file contains linux line endings ('\n', LF, #10, #0x0A). Android ignores the HOSTS file if it contains Windows' 2-char line endings ('\r\n', CRLF, #13#10, #0x0D0A) or MacOS-style line endings ('\r', CR, #13, #0x0D).
Except for letters/numbers/dots (used for IP and host names) the file should contain only characters 0x20 (space), 0x09 (tab) and 0x0A (new line).
To acces to your localhost the IP is
10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
From Android docs:
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).
At your console use:
emulator -avd <you_avd_name> -dns-server <serverList>
I used when I had to connect to a VirtualBox with a linux distro on the same PC and it worked from the emulator webview http://10.0.2.3/servlet-name
Reference: Emulator Networking