Android hosts file usage
I have th开发者_如何学Goe 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.
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
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:
- Using Busybox, copy the hosts file to an editable location with
cp /etc/hosts /mnt/sdcard/hosts.new
; - 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, eg192.168.2.81 siva siva.myinventeddomain.org.au
, but other formats should in theory work too; - In BusyBox again,
su
to root; /system
is ro by default, so I had to make it rw withmount -o remount,rw /system
;- To save typing later,
cd /etc
(whereupon the shell prompt showed/system/etc
rather than/etc
, which makes me suspect symlink shenanigans); - Back up the default hosts file (which contained only
127.0.0.1 localhost
) withmv hosts hosts.old
; - Install new hosts file with
mv /mnt/sdcard/hosts.new hosts
; - Execute
sync
(merely because I am paranoid - this shouldn't be necessary); - Remount
/system
fs ro withmount -o remount,ro /system
; - Exit BusyBox;
- 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...
- Start a DNS server somewhere (you can load DD-WRT, have a fancy router, or run a daemon on your PC).
- OPTIONAL: Configure your local router (DHCP server) with the location of the running DNS server (make it first in the DNS server list).
- IF SKIPPING #2: Configure your phone for static IP (Wi-Fi Settings->Menu button->advanced). Set the DNS1 to be your server.
- 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 :-).
精彩评论