How to run an app using Android Studio
Two of my previous articles – How to Create a New Project in Android Studio and Components of an Android Project explained the process of creating a new Android project and the structure of a typical Android project, respectively. The current article will show you how to run an app using Android Studio. You can run an app either on a physical device (phone or tablet) or the Android emulator.
How to run an app on a physical device
To run an app on a physical device i.e. a phone or tablet, you need to follow these steps –
1. Ensure that USB debugging is ON (on your phone/tablet)
You need to have USB debugging enabled on your device. By default, it is disabled. To enable USB debugging, go to Settings>About device and tap Build number 7 times. This enables the Developer options option in Settings. Now, go to Settings>Developer options and set USB debugging ON.
2. Connect your device to your computer
When you connect your device to your computer for the first time after enabling USB debugging, it (your device) asks whether you want to allow USB debugging for that computer. If you confirm, your device will authorize your computer for USB debugging and whenever you connect it in future, USB debugging will automatically be enabled. Please Note that you can revoke this authorization future by going to Settings>Developer options and tapping on Revoke USB debugging authorization.
3. Run the app from Android Studio
Open an existing project in Android Studio and click on Run>Run ‘app’ or press SHIFT+F10. The Select Deployment Target window is displayed. This window lists all your connected physical devices and virtual devices. Select your device and click on OK to start running the app.
How to run an app on the Android emulator
To run an app on the emulator program that comes with Android Studio, you need to follow these steps –
1. Create a new virtual device (if you haven’t created one yet)
Open an existing project in Android Studio and go to Tools>Android>AVD Manager. Alternatively, you can also click on AVD Manager icon in the toolbar. The Android Virtual Device Manager window is displayed. Click on Create Virtual Device button.
The next three windows will let you set up the hardware and software configuration of the virtual device. The first of these windows lets you choose a hardware profile for the virtual device. There are several pre-defined hardware profiles, which imitate the configuration of some common real-world devices. You can also create a new hardware profile. In this case, I have chosen Nexus 5X which has a screen size of 5.2″ and screen resolution of 1080*1920.
The next window lets you choose the system image to be used for this virtual device. In simple terms, a system image is basically a specific version of Android OS. Here, I have chosen Android Nougat (API 25) because it is the latest version of Android right now. You may choose another system image if you want to create a virtual device for an old version of Android.
In the last window, you can verify the hardware and software configuratio of the new virtual device and modify it, if needed. Click on Finish to finish the creation of the virtual device.
2. Start the virtual device in the emulator
Now, start the newly created virtual device from the Android Virtual Device Manager window (by clicking on the triangle shaped button).
After the emulator has finished loading the virtual device, it looks the below image. As you can see, it looks exactly like a physical device.
3. Run the app in the emulator
To run the app in the emulator, press SHIFT+F10 or go to Run>Run ‘app’. The Select Deployment Target window is displayed. Now, select the virtual device you want to use and click OK (In this case, there is only one virtual device).
- When you develop an Android app, you should test it on as many types of devices (physical or virtual) as possible. Why? To ensure that your app will run properly on all your target devices. In other words, if you want your app to run well on devices ranging from a 2.4″ phone to a 10″ tablet, you need to test your app on all of them. You also need to test your app on different versions of Android.
- A typical Android developer has only a few physical Android devices available (I have three phones and one 7″ tablet). The number of test configurations is usually much more than that. Therefore, most of the testing tasks need to be performed on emulators.
- A virtual device is NOT equivalent to a physical device, because even though the specs may be the same theoretically, the real world performance (e.g. speed) is different. Therefore, a physical device is more reliable indicator of performance than a virtual device.
- Most smartphone manufacturers modify certain parts of Android OS before installing it on their devices. Therefore, certain features of Android may work differently on devices of different brands. For example, you might have noticed that some apps, which work perfectly on HTC/LG/other phones, do not work well on Samsung phones. To overcome this issue, you should (ideally) test your app on devices from various manufacturers.
- One of the most important points is that most Android users have low-end devices i.e. devices with low-end hardware configurations (slow CPU, less RAM etc.). In fact, one of the main reasons Android is more popular than iOS is that most Android devices are pretty cheap, and hence have more widespread usage. As a developer, you must keep this is mind. Why? You need to ensure that your app works well even on low-end devices. If you test your app only on high-end devices, many (if not most) of your users may get poor performance on their low-end to medium-grade devices. In other words, you should test your app on all types of devices (from low-end to high-end).
I hope this article helped you understand how to run an app using Android Studio on physical and virtual devices. In What is Gradle, I’ve discussed Gradle and the tasks it performs in Android Studio.