Using System Hosts file for Android Emulators - android

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

Related

can't access localhost via android device using Easyphp

I'm using Easyphp to host my local website on windows and It works flawlessly. Now I'm trying to launch the website from my android phone. But It doesn't seem to work by just typing :
http://pclocalip:8080
or
http://pclocalip
As other web hosts like XAMPP and WAMP allow it by just doing that.
If you are using default android emulator
The first step is to start the AVD with an increased partition size otherwise you may get an out of memory error when you try to save the modified hosts file
emulator -avd MyAVD -partition-size 128
You then have to remount the system partition so that it is writeable
adb remount
Then copy the hosts file from the emulated device to the host machine
adb pull /etc/hosts
Edit the hosts file so that it includes mappings for all relevant virtual host names
127.0.0.1 localhost
10.0.2.2 pclocalip
Then copy the updated file back to the emulated device
adb push hosts /etc/hosts
You should then be able to visit http://pclocalip in the emulator’s browser and see the correct site
PS: Note that 10.0.2.2 here is the localhost IP address of your machine that run the AVD
There is a module especially for that : www.easyphp.org/documentation/devserver/mobile-for-devserver.php
"With the module "Mobile", you can test your code on your mobile phone / tablet and see if your website / application is mobile friendly."

Android emulator browser can't access to localhost subdomains set on hosts file

In my current Android Emulator with API 26 I'm trying to access with it's browser to a localhost subdomain set in the android emulator hosts file with no success.
I successfully managed to root my emulator and modify it's /etc/hosts file (pushing a local file and with busybox mini vi implementation). Some of the successful tests that I made in the browser and with the ping command from adb shell are as follows (where 192.168.0.10 is my current assigned host ip):
192.168.0.10 source
192.168.0.10 source.api
192.168.0.10 source.api.local
10.0.2.2 source
10.0.2.2 source.api
10.0.2.2 source.api.local
but with localhosts subdomains mappings I can only ping the domain from adb shell with success. The browser displays an ERR_CONNECTION_REFUSED error in the next host mappings:
10.0.2.2 something.localhost
10.0.2.2 something.something.localhost
Any ideas on why this is happening?
EDIT:
The current problem only happens in my macOS 10.14, I couldn't reproduce the problem in Ubuntu 18.04.
The problem is with the Multicast DNS (mDNS) implementation of macOS, when using the top-level domain .local
Bonjour... "will automatically resolve the private IP addresses of link-local Macintosh computers running MacOS and mobile devices running iOS if .local is appended to their hostnames."
RFC 6762 Appendix G recomends the following private namespaces instead of .local|.localhost to avoid conflicts.
.intranet.
.internal.
.private.
.corp.
.home.
.lan.

Ionic Android Accessing Local Sites Set By /etc/hosts

On my local laptop I have a bunch of sites running using docker and made accessible in my /etc/hosts like so:
> 127.0.0.1 www.example.local
> 127.0.0.1 api.example.local
> 127.0.0.1 connect.exampe.local
Using Ionic I am emulating android: ionic cordova emulate android
Now when I got the browser on the android and I type it in www.example.local, I cannot access the site. But it works fine from my local. I assumed they would be using the name network settings. What configuration am I missing?
I would imagine this device is acting in similar fashion as a virtual machine. In which case, the network is bridged but different. 127.0.0.1 on this device likely points to the device itself. You will need to find (or create) the ip of your computer that your device is familiar with
If you are running on a mac, you can use the following commands to create an alias to the loopback adapter which the device should be able to see.
(NOTE: the dns you setup in /etc/hosts may not work)
ifconfig lo0 alias 172.16.123.1 # Allows emulator to use 172.16.123.1 for localhost
ifconfig lo0 -alias 172.16.123.1 # removes alias
Source of this info:
https://stackoverflow.com/a/624660/1991100

AVD Emulator Browser won't use hosts file

I'm pretty frustrated with this one. I have a dev web server running a service feed for my Android app. I can browse to it from any number of machines.
On the AVD (and I've tried different versions all the same) I remount rw and pull, edit then push back the host file to system/etc/hosts. In the DDMS I see the date updated and if I pull it back off the changes are there.
Further if I go to a command prompt then run adb shell and ping the testhost name I get a reply from the correct ip. So why?????? will the avd browser not load the page. Oh yes the page. Plain HTML that says YES I am working.
In the end if I don't browse to the html page that's fine but since I need my app to query my service (hosted same site) for development and testing purposes I was thinking this would be a good test for connectivity before trying to have my app make a request to the service.
Stumped.
.....
Solved!!!
My windows background has me editing a hosts file with IP then tab then host name. Well I followed some instructions that had me edit the hosts from the adb shell. That worked...huh??? so I pulled the file and opened in Edit+ only to find I was shooting muyself in the foot. Only *ONE space between the ip and host name. It is working great now.
My windows background has me editing a hosts file with IP then tab then host name. Well I followed some instructions that had me edit the hosts from the adb shell. That worked...huh??? so I pulled the file and opened in Edit+ only to find I was shooting muyself in the foot. Only *ONE space between the ip and host name. It is working great now. If in doubt I would highly recommend updating your host file without pulling the file to the desktop. This is the command from the shell to do so.
./adb -s [DeviceID] shell
echo '###.###.###.### [HostName/URl]' >> /etc/hosts
exit
NOTE The above command is not every step needed just the command to actually edit the file from the shell. You still need to have write perms etc.
In windows host file its allowed to have multiple spaces (tabs) between IP address and hostname, but in android host file it requires single space between IP and HostName.
I tried with a host file with single entry in which IP and Hostname are separated by single space character using notepad++. But still problem persists, ie I was unable to open the site in emulator.
You can find steps to push host file of your local machine to android emulator ->
http://borkweb.com/story/setting-etchosts-on-an-android-emulator-using-a-mac
But still emulator is unable to use this host file which got resolved as ->
I tried this (Worked):
1. pushed a empty host file in emulator
2. Edited the same host file using adb shell echo command
#adb shell
#echo '172.30.1.227 fat.choice.plus' >> system/etc/hosts
#echo '\n172.30.1.227 static-fat.loyrewards.com' >> system/etc/hosts
use your IP address and hostname in above commands
To come out of adb shell use shortcut Ctrl + D
You can add multiple entries via echo using \n character for newline.
Emulator is now able to use this modified host file and site is loaded successfully. :-)
In my particular case, localhost subdomains give this problem on the android emulator browser (even though I can ping the domain), I was using the next mapping on my macOS 10.14:
10.0.2.2 something.localhost
Getting rid of the localhost fixed the issue for me. I don't know why this happens on my Mac, I couldn't reproduce this behavior on Ubuntu 18.04
The easiest way and the simple answer is:
It's the end of line(EOL) character for each line in the hosts file.
The EOL should be that of linux(LF).
The number of spaces or tabs doesn't matter.
For converting EOL character you can follow the following steps in notepad++:
Edit-> EOL Conversion-> Linux(LF).
To verify:
View -> Show symbol -> show end of line.
Once you have verified the above in your hosts file, simply push it to emulator.
Use the following commands to ensure your hosts file is replaced on emulator:
adb push ./hosts /etc/hosts
adb push ./hosts /system/etc/hosts
adb push ./hosts /etc/system/hosts
Note: You need to have your emulator running in write mode.

Access a virtual host from the Android emulator

I'm developing a simple app that connects to a webservice configured as a virtualhost in Apache, so its URL is myapp.localhost
In my development machine (Mac OS X) I can access it, and also from the iPhone emulator.
But I'm having problems from the Android emulator, it just cannot reach that URL, because it's not using my /etc/hosts file to resolve the "myapp.localhost" domain name.
Since I'm using Appcelerator Titanium (appcelerator.com), I cannot use "adb" to inject a custom hosts file to the Android emulator.
Is there a DNS server or something similar I can install in my Mac OS X system to translate that virtual host for the Android emulator?
I suppose you eventually found how to solve the problem. Still, I will give a possible answer for those who seek around, struggling with the same problem we had once.
Since android devices are emulated and not simulated (as opposed to iOS devices), they indeed have their own kernels, configuration files... and their own /system/etc/hosts. But you can actually use adb with Titanium. You just need to be careful. Since Titanium use a custom-generated virtual device, based on tiapp.xml, you will have to run your application at least once before the virtual device shows up in the device list.
If you have more than on virtual device you first need to get the name of the device generated by Titanium (typically emulator-XXXX).
adb devices
Then you can mount it and use adb push and pull commands to get the /system/etc/hosts out of the emulator's system, edit it and push it back. -s emulator-XXXX specifies on which device these commands are meant to be executed. If there is only one device, you can skip this option. See adb doc for more informations.
adb -s emulator-XXXX remount
adb -s emulator-XXXX pull /system/etc/hosts /whatever/directory/
You can now edit /whatever/directory/hosts with you favorite editor and add the necessary host. If you need to access the localhost interface of your development machine, 10.0.2.2 is a link to the computer's loopback interface. Simply add 10.0.2.2 myapp.localhost to the host and push it back to the virtual device.
adb -s emulator-XXXX push /whatever/directory/hosts /system/etc/hosts
Relaunch the simulator with Titanium and it should do the trick!

Categories

Resources