Friday, December 27, 2013

From an emulator to a real device

In Testing and debugging GAE Endpoints in Android Studio we used an Android emulator to test a debug GAE Endpoints running on local dev server.  Here we describe the steps required to move the client from an emulator to a real Android device.

Bind local dev server to all interfaces

By default, local dev server is listening to incoming connections only on the loopback network device. We need to change the server's configuration so it can accept incoming connections on all network interfaces.
  1. Open Project-AppEngine/pom.xml
  2. Locate <groupId>com.google.appengine</groupId> plugin
  3. Under <configuration/> tag add <address>0.0.0.0</address>
  4. Optional, add <port>8080</port> (or other value)
  5. Restart local dev server.  It is better to do this in two steps:
    • appengine:devserver_stop
    • appengine:devserver
Open a web browser on the same computer where the local dev server is running.  Both URIs should connect to the local dev server:
  • http://localhost:8080
  • http://ip-address:8080
where ip-address is the IP address of the computer running local dev server.  You should see the content of Project-AppEngine/src/main/webapp/index.html page.

Connect Android device to WiFi

Both an Android device that will be running instrumentation tests and a computer running local dev server should be connected to the same network.  After connecting an Android device to WiFi, open a web browser on the device. Type http://ip-address:8080, where ip-address is the IP address of the computer running local dev server.  If you don't see the content of Project-AppEngine/src/main/webapp/index.html page, go to the next step, otherwise skip it.

Configure firewall

The most likely reason you do not see the content of Project-AppEngine/src/main/webapp/index.html page from an Android device (or another computer) is because your computer's firewall is blocking incoming connections to the local dev server.  You need to whitelist Java so that the clients from the local network can connect to the dev server.  The steps below show how to do this on OS X 10.9.1.  Other OSes should have similar configuration options.
  1. Open System Preferences
  2. Open Security & Privacy
  3. Click Firewall tab
  4. Unlock to enable Firewall Options
  5. Go to Firewall Options
  6. Make sure that "Allow incoming connections" is enabled for Java
  7. Just in case, restart the local dev server
    • appengine:devserver_stop
    • appengine:devserver
Open a web browser on an Android device connected to local WiFi.  Type http://ip-address:8080, where ip-address is the IP address of the computer running local dev server.  After whitelisting Java, you should see the content of Project-AppEngine/src/main/webapp/index.html page.

Modify dev server URI

In Testing and debugging GAE Endpoints in Android Studio we used TestEndpointTest to run instrumentation tests. Modify TestEndpointTest.DEV_SERVER:
private static final String DEV_SERVER = "http://ip-address:8080/_ah/api/";
where ip-address is the IP address of the computer running local dev server.  You can add ip-address to the list of manually assigned IP addresses by your DHCP server.  Or, if you run a local DNS server, you can replace ip-address with the host name.

Run instrumentation tests

Connect your device to your computer through a USB cable and run an instrumentation tests as described in Testing and debugging GAE Endpoints in Android Studio.   If everything is configured correctly, the tests should run and you should be able to debug GAE endpoints in Android Studio.  Running instrumentation tests on an Android emulator should also work with this configuration.

No comments:

Post a Comment