Access a virtual host from the Android emulator
Asked Answered
M

1

5

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?

Manipulator answered 28/2, 2011 at 12:3 Comment(1)
why can't you use the build/android folder after it's been generated in Ti once. I often build once in the Ti Developer and then just use xcode to build from the build/iphone folder. I assume the same would be true with android?Legg
S
11

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!

Sisyphus answered 9/8, 2011 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.