AVD Emulator Browser won't use hosts file - android

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.

Related

Android: adb pull command not working/stuck

on Linux machine I'm able to push files to my Motorla one zoom XT2010-1 via adb yet when I try to pull, the command is stuck indefinitely. The paths seems to be correct since TAB autofill kicks in, and file is created in destionation directory.
adb pull /sdcard/Download/magisk_patched-25200_QsXC2.img /home/flap/Downloads/

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."

Using System Hosts file for Android Emulators

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

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!

Android emulator doesn't use Windows host file?

m3 http://img245.imageshack.us/img245/461/65254534.jpg
Chrome using the windows' HOST file:
m1 http://img266.imageshack.us/img266/9380/59750633.jpg
Android emulator:
m2 http://img821.imageshack.us/img821/7363/19470766.jpg
edit
"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)."
how can I use static IPs in the emulator?
I got this article, it explains how to edit the hosts file. I don't know if it works yet, bit it might help.
BROKEN (Feb 2015) - http://www.bradcurtis.com/2011/02/13/hosts-file-google-android-emulator/ (Leaving it in case it recovers)
If I'm not wrong, they moved the link here, I'm not sure if it's the same article, I haven't touched this since may 2012.
http://www.bradcurtis.com/hosts-files-and-the-google-android-emulator/ (Mirror Link https://web.archive.org/web/20180615121728/http://www.bradcurtis.com/hosts-files-and-the-google-android-emulator/)
EDIT: Adds a new link that works and talks about the ame issue (Not sure if it's the same article.
FWIW, it's not possible to change the phone's own /etc/hosts file within the emulator - it's on a read-only file system.
Personally, I'd run a local resolving server on your PC, point your Windows O/S at that, and then override the entries properly.
Unbound will do the job, and has a nice feature to allow you to insert "local data" into your DNS results.
It is possible to update the Android phone's own hosts file with:
adb remount
adb push hosts /etc/system/hosts (most tutorials suggest this file)
adb push hosts /system/etc/hosts (some VM systems seem to prefer this file instead!, for me this worked)
if any issues like i kept randomly getting a 'read-only file system' error 90
% of the time I tried running above, then try running 'adb root' before above (didnt work for me through), or start the Android VM manually with your android sdk's emulator.exe:
emulator.exe -avd Nexus_6_API_23 -partition-size 512
(some peeps recommended making partition size manually so can write to the VM easier?, eh seemed to work better but still got random read-only errors still).
An Android VM created with GenyMotion seemed much stabler and never get 'read only file system' issues with their VMs when i tried pushing the hosts file.
If the website you want to look at is hosted on your host machine, then you can point to it via a special IP android VMs recognize as the host machine via the hosts file:
10.0.2.2 test-drupal8.localhost
or if using Bridged Networking instead of NAT (pretend your windows host machine is 172.16.6.50 where your website is hosted and Android VM is 172.16.6.51), then can use windows ip directly:
172.16.6.50 test-drupal8.localhost
Note: make sure Windows Firewall/antivirus/firewalls are off if testing as they were blocking my urls until i turned them off.

Categories

Resources