Reputation: 889
I am developing an Android app that connects to a Rails web server. I need to test the app against the development version of the server on my computer. In order to work, the server needs incoming requests to be to the named domain.
Requests are addressed to localsite.com:3000
.
For browser testing, the hosts file on my computer handles that:
127.0.0.1 localsite.com
For testing from Android emulators, the hosts file in the emulated device handles that:
10.0.2.2 localsite.com #Localhost from an Android emulator
Now I want to test it from a physical device (a Nexus6 that has developer tools enabled, but is not rooted. My preference is to keep it that way). I can connect to my server from the phone by using the IP and port (192.168.1.190:3000
), but then nothing on the server works, because it expects localsite.com:3000
.
Here are my options, as I understand them:
Get the hosts file from the device:
adb pull /system/etc/hosts ~/device_hosts/nexus6
Edit hosts file:
192.168.1.190 localsite.com #IP of workstation on local network
Try to push hosts file back to device:
adb push ~/device_hosts/nexus6/hosts /system/etc
This fails, with the error failed to copy: Read-only file system
.
adb remount
also fails, as does adb root
: adbd cannot run as root in production builds
.
Question Part 1: is there any way to edit the hosts file of the device without permanently rooting it?
While Googling around, I have seen references to an alternative to the hosts file solution, which is to "run a DNS server on the Android phone". It is far from clear to me how this could work to perform the same job as the hosts file.
I've installed the app DNS Server
on the phone, and am running a DNS server with a "Forward" rule, as follows:
Server name: mysite
Port: 59953
Forward: mysite.com to 198.168.1.190
The result is absolutely nothing. Requests to mysite.com or mysite.com:3000 from the device do not reach the local server.
Question Part 2: Can I set up the DNS Server
app on my Android phone so that it can make requests to mysite.com:3000
running on a machine on the local network?
Bonus question: Are there any other approaches to testing on a local server from a physical, unrooted Android device that I have not yet considered?
Upvotes: 3
Views: 6644
Reputation: 889
The solution that I've worked out for my Mac desktop/Android mobile/Rails server combo:
Chrome://inspect
on the desktop to set up port forwarding for the mobileThis process is adapted from this resource: https://developer.chrome.com/devtools/docs/remote-debugging#port-forwarding
Assumptions before you're at this point:
chrome://inspect
I installed Squidman proxy server: http://squidman.net/squidman/index.html, running on port 8080.
On the tab "Template", comment out this line to allow access to web apps running on the proxy host (e.g, your localhost):
# protect web apps running on the proxy host from external users
#http_access deny to_localhost
Start 'Squidman'. For troubleshooting, open the "Tools" menu to see the access logs.
On Chrome on the desktop, open up chrome://inspect/#devices
. Select your device. Click "Port Forwarding". Enter a line to direct port 9000 to your proxy server:
9000 localhost:8080
On the device, open up the Wifi connection Advanced Settings. Set it up as follows & save.
Proxy: Manual
Proxy hostname: localhost
Proxy port: 9000
My Android device now connects correctly to localsite.com:3000
(as set up in the hosts file on the desktop)- this now works for me from the device browser, and from apps installed using adb install
.
Upvotes: 5