Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access a virtual host from the Android emulator

I'm developing a simple app that connects to a webservice configured as a virtualhost in Apache, so its URL is myapp.localhost

In my development machine (Mac OS X) I can access it, and also from the iPhone emulator. But I'm having problems from the Android emulator, it just cannot reach that URL, because it's not using my /etc/hosts file to resolve the "myapp.localhost" domain name.

Since I'm using Appcelerator Titanium (appcelerator.com), I cannot use "adb" to inject a custom hosts file to the Android emulator.

Is there a DNS server or something similar I can install in my Mac OS X system to translate that virtual host for the Android emulator?

like image 323
David Morales Avatar asked Jan 21 '23 09:01

David Morales


1 Answers

I suppose you eventually found how to solve the problem. Still, I will give a possible answer for those who seek around, struggling with the same problem we had once.

Since android devices are emulated and not simulated (as opposed to iOS devices), they indeed have their own kernels, configuration files... and their own /system/etc/hosts. But you can actually use adb with Titanium. You just need to be careful. Since Titanium use a custom-generated virtual device, based on tiapp.xml, you will have to run your application at least once before the virtual device shows up in the device list.

If you have more than on virtual device you first need to get the name of the device generated by Titanium (typically emulator-XXXX).

adb devices

Then you can mount it and use adb push and pull commands to get the /system/etc/hosts out of the emulator's system, edit it and push it back. -s emulator-XXXX specifies on which device these commands are meant to be executed. If there is only one device, you can skip this option. See adb doc for more informations.

adb -s emulator-XXXX remount
adb -s emulator-XXXX pull /system/etc/hosts /whatever/directory/

You can now edit /whatever/directory/hosts with you favorite editor and add the necessary host. If you need to access the localhost interface of your development machine, 10.0.2.2 is a link to the computer's loopback interface. Simply add 10.0.2.2 myapp.localhost to the host and push it back to the virtual device.

adb -s emulator-XXXX push /whatever/directory/hosts /system/etc/hosts

Relaunch the simulator with Titanium and it should do the trick!

like image 170
Frédéric Maquin Avatar answered Jan 31 '23 20:01

Frédéric Maquin