May 17, 2017

How to create and launch an Android emulator from Android Studio

Today, let's walk through how to create and launch an Android emulator with Android Studio.


What does an Android Emulator Consist Of?


"The Android Emulator runs a full Android system stack, down to the kernel level, that includes a set of preinstalled apps (such as the dialer) that you can access from your apps. You can choose which version of the Android system you want to run in the emulator when creating AVDs.

"The Android system images available through the AVD Manager contain code for the Android Linux kernel, the native libraries, the VM, and the various Android packages (such as the Android framework and preinstalled apps)". - Developer.Android.com, Run Apps on the Android Emulator



Do You Have Android Studio Downloaded?


First things first! Do you have Android Studio downloaded and installed?





Yes, we could just download the standalone Android Command Line Tools, with:

... but as I was doing that before, installing the command line tools as a standalone, I kept on getting warnings messages from the Android Developer people, that installing them without Android Studio is not supported by them, and I was on my own by doing it.

Note, we can still used these command line tools via the Mac Terminal command line. If you open a Mac Terminal and type in to go change into your home directory, where in your Library in your Home directory, you can go to the Android SDK Tools directory:

 cd ~/Library/Android/sdk/tools   

... You can see the tools there if you do: ls


Aren't The Android OS Versions Named After Candy?


Ever since April 2009, when version 1.5 of the Android mobile operating system was released, the operating systems have been code-named some type of dessert.

Android supports, at the time of this blog post, Android 6.0 (Marshmallow, API Level 23, Released October 5, 2015), Android 7.0 (Nougat, API Level 24, Released August 22, 2016), and Android 7.1.1 (Nougat, API Level 25, October 4, 2016).

But what devices should we set up? For that, we can go to the Android Dashboards at https://developer.android.com/about/dashboards/index.html

VersionCodenameAPIDistribution
2.3.3 -
2.3.7
Gingerbread100.9%
4.0.3 -
4.0.4
Ice Cream Sandwich150.9%
4.1.xJelly Bean163.5%
4.2.x175.1%
4.3181.5%
4.4KitKat1920.0%
5.0Lollipop219.0%
5.12223.0%
6.0Marshmallow2331.2%
7.0Nougat244.5%
7.1250.4%
Data collected during a 7-day period ending on April 3, 2017.
Any versions with less than 0.1% distribution are not shown.

... And it looks like with screen sizes, most people seem to be using a Normal screen, hdpi or xhdpi. If we were setting up an automation framework, it would be good to have an emulator running an older Android operating system.


Have You Set Android Home?


Type in the Mac Terminal:

 echo $ANDROID_HOME  

... Is it blank? We need to set up ANDROID_HOME for your bash profile.

Note: Want a quick way to Change Directory to your Home Directory via the Command Line?

 cd ~  

a) Open up a Mac Terminal
b) Go into the text editor called "nano", opening up your .bash_profile

 nano ~/.bash_profile  

c) Add the following:

 export ANDROID_HOME=~/Library/Android/sdk<br />  
 export PATH=$PATH:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools<br />  


... That will set the variable, "$ANDROID_HOME" as the Library directory off of your home directory, in the Android/sdk subfolder. Once that variable is set, we can use it to also point to the /tools and the /platform-tools subdirectory.


d) Save and Close nano by entering:

CNTRL + X (Note: ^ is a shortcut for CNTRL button).
Y to answer "Yes" to saving the changes.
[ENTER] to save changes.

e) Check that it worked:

 source ~/.bash_profile   
 echo $ANDROID_HOME  


Create an Emulator


  • Launch Android Studio
  • Go to Tools -> Android -> Android Device Manager



The Android Virtual Device Manager will pop up displaying devices, if any. 
  
Select the "+ Create Virtual Device..." button. 



Here you can select the Hardware you want to emulate, setting up your Virtual Device Configuration. 

Let's try for:
  • Phone -> Pixel (The Google Pixel Phone) 
... And select Next. 


What operating system do we want to pick? Let's try the latest, which is Android 7.1.1 at the time of this writing. Select the code name Nougat, API Level 25.

Hit NEXT.













What name to give it? Let's change Pixel API 25 to have underscores, Pixel_API_25. 

If you want, select Advanced Settings. You can make it use many different networks, from Full, HSDPA, UMTS, EDGE. GRPS, HSCSD, and GSM. You can give it more (or less) RAM, storage space on the SD card, etc. 


Select FINISH.

To start up the device, just select the Green Arrow to the right of it.

Wait a few minutes for the phone to boot up.





A quick way to install an app on it? Drag-and-drop an .apk on it.

Does ADB Recognize the Emulator?

Make sure that adb integration is enabled through Android Studio -> Tools > Android > Enable ADB Integration


Go to the Mac Terminal and run: adb devices

List of devices attached
emulator-5554   device


Need More Help?


Review:




Happy Testing!


-T.J. Maher
Twitter | LinkedIn | GitHub

// Sr. QA Engineer, Software Engineer in Test, Software Tester since 1996.
// Contributing Writer for TechBeacon.
// "Looking to move away from manual QA? Follow Adventures in Automation on Facebook!"

1 comment:

Krishna Verma said...

Thanks sir this guide.

Can I build an iOS emulator for Android device with Android studio?

I am a freelance android developer and just started an app development agency. I am looking for a way to build an emulator which can simulate iOS apps.