Android hosts file usage

dominos picture dominos · Nov 13, 2010 · Viewed 9.1k times · Source

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.

Answer

Jason LeBrun picture Jason LeBrun · Jan 30, 2011

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