Access a virtual host from the Android emulator - android

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?

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!

Related

Getting "error: closed" twice on "adb reverse"

I am trying to reverse-forward port through ADB, but it just returns cryptic error of error: closed. Normal forwarding works. Session snippet:
$ adb forward tcp:59778 tcp:59778
$ adb forward --list
015d2109ce0c1a0f tcp:59778 tcp:59778
$ adb forward --remove-all
$ adb forward --list
$ adb reverse --list
error: closed
error: closed
$ adb reverse tcp:59778 tcp:59778
error: closed
error: closed
I am connecting via USB to non-rooted Nexus 7 2012 Android 4.4.4 from Windows 7 Pro x64 on Boot Camp.
adb reverse was introduced in Android 5.0
Since adb reverse is not supported in Android versions lower than 5.0, you need to use an alternative method, for example connecting via Wi-Fi instead. If you are using React Native, Facebook has added official documentation to connect to the development server via Wi-Fi. Quoting the instructions for MacOS, but they also have them for Linux and Windows:
Method 2: Connect via Wi-Fi
You can also connect to the development server over Wi-Fi. You'll first need to install the app on your device using a USB cable, but once that has been done you can debug wirelessly by following these instructions. You'll need your development machine's current IP address before proceeding.
You can find the IP address in System Preferences → Network.
Make sure your laptop and your phone are on the same Wi-Fi network.
Open your React Native app on your device.
You'll see a red screen with an error. This is OK. The following steps will fix that.
Open the in-app Developer menu.
Go to Dev Settings → Debug server host for device.
Type in your machine's IP address and the port of the local dev server (e.g. 10.0.1.1:8081).
Go back to the Developer menu and select Reload JS.
Follow these steps carefully.
Note: All commands need to run inside a project only.
Run this command first:
npm react-native start
Open another window in the same project and run:
curl "http://localhost:8081/index.android.bundle?platform=android" -o "android/app/src/main/assets/index.android.bundle"
This will create index.android.bundle in the assets folder
Run:
npm react-native run-android
Now you can get apk in the build folder which will work fine.
adb reverse requires Android 5.0+. For devices previous to that, you'll need to use a workaround like so.
If you have busybox installed on your Android device (most Genymotion images do), you can emulate adb reverse using this incantation:
adb shell busybox nc -ll -p {guest port} -e busybox nc {host IP} {host port}
In this case, "guest" is the Android OS running in the emulator and "host" is the computer running the emulator.
cause of adb reverse isnt working on android prior 5 you could propably use adb forward with a service listening on android and tunneling other connections through this inbound connection. I am doing this mostly with ssh, but you would need an ssh server on android. you than can connect using ssh -R incommingreverseportonandroid:hostyouwanttoforwardto:portyouwanttoforwardto sshuseronandroid#localhost -p portyouhaveusedforadbforwaqrdtoaccessandroidssshserver
but i dont know how to enable an ssh server on android and maybe there is a better way cause ssh uses encryption which isnt needed over usb and using up cpu.
i am using this way with my server to share a service when i am forced behind a nat...
hope someone will find a way to bring this teoretical way into practical possibility
Just use 10.0.2.2 instead of localhost/127.0.0.1 for your hostname. It will directly try to connect to the port on the host machine (same affect as reverse).

How to use android device as host to pull logs from another android device?

Usually we use windows/linux machines to pull logcat info from an android device.
Is it possible to use android tablet/phone to act as host and get the logs from another android phone connected to it?
This is possible. The host Android device needs to be defined as a USB host. Then, once a connection is established, all of the same commands can be run, assuming that ADB is installed on the host device.
See here for more discussion on USB Host Mode support and compatible cables: https://android.stackexchange.com/questions/36887/how-can-i-determine-if-my-device-has-usb-host-mode-otg-support
It is useful to have a terminal program, such as Terminal IDE to issue commands from, and using busybox to provide additional commands would also be beneficial.
ADB comes as part of the AOSP source, so it a device is based off AOSP, then it will possibly include ADB. ADB source is located in /system/core/adb in the AOSP source tree.
Here is an example of a session where a Nitrogen6X (host) running a custom AOSP is used to connect to a Galaxy S3 cellphone (client). Note the use of Busybox and ADB, both of which are installed on the host. Busybox also happens to be installed on the client, but this is not necessary:
Thus in order to pull logs, just use regular ADB commands (e.g. to copy files), or run logcat, as indicated below:

how could I transfer file from terminal to screen /dev/tty.*(android machine)?

I'm developing android set-top box app.
So, I have android box and its adb is REALLY SLOW.
When I have to install .apk file to debug my project, I use adb connect [ip address] via wifi and then build & run with Android Studio because the set-top box doesn't have USB port.
But it has serial port so I can connect its shell by screen /dev/tty.usb-serialblablabla 115200. when I use that command, I can see exactly same screen as one of adb shell.
Its adb shell(via wifi) shows very very slow performance.
For example, if I want to run pm list packages, I should type 'pm list packages' and wait for 2min.and then type enter key.(...sigh)
Whereas,Using screen /dev/tty.* methods respond promptly.
I don't know where this difference is come from.
but it is not matter of wifi. I checked its speed. it is quite normal.
In this circumstance, I click Run button in Android Studio after adb connect.
gradle build takes about 10sec.
upload .apk(10.2mb) takes about 10min (sigh).
Because screen ... way is much faster then adb connect way, I want push my .apk into device via 'screen'. how can I do? OR is there anything I can do to reduce time for putting .apk in android device?
You really should find out why adb transfers take so long. But if you insist on using serial console for uploads instead - you could just use any terminal application with XMODEM support on PC side to send the file and busybox rx -b <filename.apk> command on android side to save it. Then pm install <filename.apk> to install it.

Using System Hosts file for Android Emulators

I having problem in using my system hosts file details in Android Emulators.
I have my website (www.example.com) deployed in Webserver.
I am trying to access those website in Emulator from my desktop.
My desktop has a hosts file with the entry of
10.xx.xx.xx www.example.com
I am trying to access the www.example.com from the android emulator - browser and it is not working.
I took a reference from the below website, but most of the website says how to use the hosts file if the website is deployed in the same server.
http://sadhanasiblog.blogspot.in/2012/07/local-environment-setup-for-android.html
Please let me know if anyone has answers. Thanks in Advance.
-Senthil
With latest Android studio and tools. You can now use follow this instructions. You only need to run this once per emulator.
Start the emulator with following command. This command is located under your [sdk folder]/tools directory
emulator #[emulator name] -writable-system
Open another command prompt and then switch your directory to [sdk folder]/platform-tools and run following commands
adb root
adb remount
adb shell
echo '10.0.2.2 [localserver dns name]'>>/etc/hosts -- example echo '10.0.2.2 xxxx.com'>>/etc/hosts
One important thing to check is, if you create your HOSTS file on Windows or MacOS machine, that the HOSTS file contains linux line endings ('\n', LF, #10, #0x0A). Android ignores the HOSTS file if it contains Windows' 2-char line endings ('\r\n', CRLF, #13#10, #0x0D0A) or MacOS-style line endings ('\r', CR, #13, #0x0D).
Except for letters/numbers/dots (used for IP and host names) the file should contain only characters 0x20 (space), 0x09 (tab) and 0x0A (new line).
To acces to your localhost the IP is
10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)
From Android docs:
At startup, the emulator reads the list of DNS servers that your system is currently using. It then stores the IP addresses of up to four servers on this list and sets up aliases to them on the emulated addresses 10.0.2.3, 10.0.2.4, 10.0.2.5 and 10.0.2.6 as needed.
On Linux and OS X, the emulator obtains the DNS server addresses by parsing the file /etc/resolv.conf. On Windows, the emulator obtains the addresses by calling the GetNetworkParams() API. Note that this usually means that the emulator ignores the content of your "hosts" file (/etc/hosts on Linux/OS X, %WINDOWS%/system32/HOSTS on Windows).
When starting the emulator at the command line, you can also use the -dns-server option to manually specify the addresses of DNS servers to use, where is a comma-separated list of server names or IP addresses. You might find this option useful if you encounter DNS resolution problems in the emulated network (for example, an "Unknown Host error" message that appears when using the web browser).
At your console use:
emulator -avd <you_avd_name> -dns-server <serverList>
I used when I had to connect to a VirtualBox with a linux distro on the same PC and it worked from the emulator webview http://10.0.2.3/servlet-name
Reference: Emulator Networking

Debugging using a virtual machine like VMWare/VirtualBox?

I am developing an Android application but fed-up of performance of My emulator
I do have a Android PC version installed in both VM-ware and Virtual Box
Can I use it as emulator? If so, how? How can I connect ADB to a virtual machine running PC Android?
I saw in some forum to use this but my VMs android having no specific IP.
How to i connect it??
Solution:
I would highly recommend to use Android x86 coz it many many times faster than Android emulator with Android x86 4.2 you can install and use any application with this and use "Google play" synch with your account as you do it with tablet
Working with latest Android X86 4.2 Jelly Bean and Virtual Box
I have found Different ways to connect with Internet and adb
Step: 1 Selection of Adapters
CASE 1: Only Internet {NAT Adapter}
The easiest solution is just use NAT adapter that will directly connect you to internet if host is connected to internet but you won't get the adb connection with this setup
Here you will get Public ip so you can't connect to Host computer
Case 2: Only adb {Host Only Adapter}
The easiest solution is just use Host Only Adapter
Note: The default Host Only adapter may not work due to DHCP server settings either create new HostOnlyAdapter or run DHCP server for existing Adapter()
Case 3: For both adb and Internet {Bridge Adapter}
You will have to take care in this case.
If you are using LAN for internet connection you shall use Bridge Adapter with your Ethernet card it will give you local ip and Virtual Machine will connect to Internet using host machine
Alternatively if you are with Wifi just do the same by selecting the Wifi adapter
For other type of connection you shall go with the same way
Step: 2 Connection with adb
to check the ip Address just press Alt+F1 {for console Window} [To switch back to Graphics view press Alt+F7 ]
you will see the console window type netcfg
it will show the ip address
Now move on to you host run command prompt move to adb directory
type
adb connect {your ip address}
Example
adb connect 192.168.1.51
Note: if adb is not running or responding you can do following
adb kill-server
adb start-server
you can check devices connected to adb
adb devices
As per your edited question, if you want to connect it to ADB you need to check what the IP of the VM is: Assuming you use VMWare's player;
Hit ALT-F1 in the VM and use the ifconfig command to know the IP address given to your network device (usually eth0). You can then hit ALT-F7 to go back to the Android UI.
Then, in your host PC, execute the adb connect [ANDROID_X86_IP] to connect the SDK debugger to your Android x86 VM; for example: adb connect 192.168.1.100:5555.
You should then see the list of devices connected and then it will be enabled to use for debugging.
ADB is typically located on your computer in a subfolder to your user folder in: ~/Android/Sdk/platform-tools. It is recommended to add it to your path so you can access it using the terminal wherever. I personally use this in ~/.bash_profile:
#add Android platform-tools directory
PATH=~/android-sdks/platform-tools:$PATH
export PATH
In case ADB fails the first time, you can try adb kill-server ; adb start-server to reset ADB.
If you want to read further, check out the Android-x86 website. It also has a lot of disc images available for download.
This is not a direct answer to your question, but did you see tricks to increase performance of emulator (read Why is the Android emulator so slow? How can we speed up the Android emulator?)
a) Use Intel Atom x86 instead of ARM
b) Use hw.gpu.enabled
I would say this makes emulator quite comparable in performance to a real device.
Update 1
Generally speaking, the idea is to configure Android PC to use tcp/ip for adb connection. And you may need to play around with network settings on VMWare or VirtualBox.
Useful links (which try to accomplish what you want
No network connection - Android-x86 on VMWare Fusion
http://lkubaski.wordpress.com/2012/08/15/running-android-on-vmware-player-with-networking-enabled/
http://www.transdroid.org/2011/01/26/techpost-debugging-against-a-virtual-machine-android/
install android x86 on vmware
run android terminal write command "ifconfig eth0"
windows user make Environment C:\Users\user- name\AppData\Local\Android\sdk\platform-tools
"adb connect IP address of the VM":5555
now run app on vmware
To save some time:
http://aztcs.org/meeting_notes/winhardsig/Android-vm/Android-VMwarePlayer-Win.pdf
Good link to get started in VMWARE
Where in the case you installed in the following directory /opt/android-sdk-linux
Set path variables
export ANDROID_HOME=/opt/android-sdk-linux
export PATH=${PATH}:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools
After performing path if you see the application from typing "android" in the console your path setting is good.
create a hello world application (get the sdk installed correctly here if you have questions)
Run the hello world app
Hit ALT-F1 in the VM and use the netcfg command to know the IP address given to your eth0 device. You can then hit ALT-F7 to go back to the Android UI. Then, in your host PC, execute the adb connect [ANDROID_X86_IP] to connect the SDK debugger to your Android x86 VM; for example: adb connect 192.168.1.100:5555. If problems bounce the service adb kill-server ; adb start-server
at console change directory to /android-sdks/platform-tools
then type
./adb connect 192.168.1.100
(note the 192.168.1.100 is what is required it defaults to port 5555)
I had to use the ./adb prefix and then I was configuring the VMWARE instance.
I feel allot less greasy avoiding XAML :-)

Categories

Resources