Sunday, July 22, 2012

Local environment setup for Android Emulator

Here are the steps I followed to set up Driod emulator on my local to launch the web application which was running on my local:
1. Download the android SDK for windows from http://developer.android.com/sdk/index.html and install the same.
2. Create an Andriod Virtual Device with AVD manager http://developer.android.com/tools/devices/managing-avds.html
 To learn about different screen sizes, please check
3. Launching local web application (running on localhost) page on AVD:
localhost or 127.0.0.1 will not launch the application running on desktop on the emulator as the loopback address 127.0.0.1 refers to the Andriod emulator itself.  10.0.2.2 is the IP to be used on Driod Emulator to connect to local computer. However direct reference to 10.0.2.2 also does not work on emulator browser for a web application. Hence the host file on the Android emulator image has to be edited to access local desktop web applications and here are the steps:
  • start the Android Virtual Device (AVD)(Start-> All Programs -> Andriod SDK Tools -> AVD Manager), wait for the virtual device to come up  and execute the following commands from local command prompt.
  • cd <ANDRIOD_SDK_HOME>\tools where ANDRIOD_SDK_HOME is the SDK installation folder
  • emulator -avd myAvdNameHere -partition-size 128
  • cd ../platform-tools
  • adb remount
  • adb pull /system/etc/hosts c:\temp
  • edit the hosts file (@C:\temp) adding an entry pointing to local host computer i.e. 10.0.2.2:                   
                         10.0.2.2      mytesthost
  • Save the edited hosts file to your Android emulator: adb push c:\temp\hosts /system/etc
  • The application can now be browsed by typing the URL in the Android browser: http:// mytesthost:<port_number_local_web_app>/<app_context_path>/<jsp_or_html_file>
  • The list of steps mentioned above has to be executed every time the emulator is launched. So it would be a good idea to save the above list of commands on a windows batch file for reuse:
                    1. Set up an environment variable - ANDRIOD_SDK_HOME to point to android sdk
                        home
                   2. Copy the below commands to a notepad and save it as a windows batch file at
                       a convenient location:              
                                    C:
                                    cd %ANDRIOD_SDK_HOME%/tools
                                   emulator -avd myAvdNameHere -partition-size 128
                                   cd ../platform-tools
                                   adb remount
                                   adb pull /system/etc/hosts c:\temp
                                   echo 10.0.2.2      mylocaltest >> c:\temp\hosts
                                   adb push c:\temp\hosts /system/etc

  • If the emulator is not connecting to local server after step #2, please check if the ADB (Andriod Debug Bridge) is able to connect to the emulator        
                             cd %ANDRIOD_SDK_HOME%/platform-tools;adb devices
        If the output text contains one of the below snippet,
                    emulator-5554 offline - the device is offline and is coming up. Please wait a while and try executing the above commands again.
                    emulator-5554 online - the device is online                   
         Here 5554 is the port number at which the emulator is running. If the device is not listed by the above command, the device is not accessible and hence relaunch the emulator.
  • To check the logs,   
               cd %ANDRIOD_SDK_HOME%/platform-tools; adb -s emulator-<port_number of the emulator> logcat > log.txt
  • To set the log level
   %ANDRIOD_SDK_HOME%/platform-tools; adb shell setprop log.tag.MyAppTag <LOG_LEVEL>
          and the available log levels are ASSET, DEBUG, ERROR, INFO, VERBOSE and WARN

1 comments:

Vishal said...

This is helpful!!

Post a Comment