Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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.

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 :-).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top