Android hosts file usage
Asked Answered
C

3

10

I have the following problem:

I make a custom hosts file to test some features of my application and then push it to my android emulator. The thing is that these settings do not take effect immediately. I have to wait about 10 minutes before they become active.

So my question is: how to make the new hosts file active instantly? I have many different settings to test and I can't wait 10 minutes every time.

Camorra answered 13/11, 2010 at 22:25 Comment(2)
I haven't tried waiting 10 minutes, but I do see that my changes are not effective immediately (when updated via adb pull /etc/hosts, local edit, adb push hosts /etc/hosts). Looking for similar help on this topic!Anguilliform
@Matt One quirk is that the host entry must end in a LineFeed or Android will ignore it. Not a CR/LF and you can't leave off the LF on the last entry. That confused me for a while. I'm not seeing a 10 minute delay, changes I make seem to take place immediately.Elswick
F
5

Java maintains its own internal DNS cache. The operating system will reflect the new hosts file immediately (verify that with ping on the command line) but you'll need to tell java not to cache anything. Add these lines to your test application:

System.setProperty("networkaddress.cache.ttl" , "0"); System.setProperty("networkaddress.cache.negative.ttl" , "0");

For more information on these properties, see here: http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html

Fireback answered 29/1, 2011 at 23:56 Comment(0)
M
2

I just edited my hosts file on my rooted Samsung Galaxy S, and the changes took effect immediately. Perhaps the problem you're seeing is something to do with ADB?

I did this:

  1. Using Busybox, copy the hosts file to an editable location with cp /etc/hosts /mnt/sdcard/hosts.new;
  2. Edit /mnt/sdcard/hosts.new using the pre-installed text editor, adding the two entries I need. I used IP, then unqualified hostname, then FQDN, eg 192.168.2.81 siva siva.myinventeddomain.org.au, but other formats should in theory work too;
  3. In BusyBox again, su to root;
  4. /system is ro by default, so I had to make it rw with mount -o remount,rw /system;
  5. To save typing later, cd /etc (whereupon the shell prompt showed /system/etc rather than /etc, which makes me suspect symlink shenanigans);
  6. Back up the default hosts file (which contained only 127.0.0.1 localhost) with mv hosts hosts.old;
  7. Install new hosts file with mv /mnt/sdcard/hosts.new hosts;
  8. Execute sync (merely because I am paranoid - this shouldn't be necessary);
  9. Remount /system fs ro with mount -o remount,ro /system;
  10. Exit BusyBox;
  11. Fired up web browser (FireFox) and entered siva in the combined URL/search field thingy (siva being one of the two hosts entries I added).

Prior to these changes, step 11 resulted in a stupid Google search for 'siva' or something; immediately after them, I get my LAN httpd vhost's front page, as I expect.

There was well under 10 minutes elapsed between it working and it not working.

The link to Sun's Java doco may or may not be relevant (probably it isn't). Android doesn't contain a Java VM at all, let alone Sun's one. It runs a different VM called Dalvik - see Wikipedia entry: http://en.wikipedia.org/wiki/Dalvik_%28software%29

The fact that you can program Android phones in a language that looks a lot like Java is beside the point.

Mayfield answered 22/10, 2011 at 1:51 Comment(0)
B
0

I haven't tried using the hosts file; but I have tried using custom DNS...

  1. Start a DNS server somewhere (you can load DD-WRT, have a fancy router, or run a daemon on your PC).
  2. OPTIONAL: Configure your local router (DHCP server) with the location of the running DNS server (make it first in the DNS server list).
  3. IF SKIPPING #2: Configure your phone for static IP (Wi-Fi Settings->Menu button->advanced). Set the DNS1 to be your server.
  4. Have your phone connect to this network over WIFI.

Your done! Now you can manage DNS Names/IP parings along with lease times (e.g. so the phone will update the IP every 30s if you need). Somewhat complicated, but you don't have to load the hosts file on the phone :-).

Breastsummer answered 29/1, 2011 at 23:23 Comment(1)
This won't work. Java is caching the DNS lookups internally, it won't even bother querying the custom DNS server.Fireback

© 2022 - 2024 — McMap. All rights reserved.