So far, you’ve tested out the app on an Android emulator. That’s nice and all but probably not why you’re learning Android development. You want to make apps that run on real Android devices!
During development, I always run and test my app on an emulator then once in a while if there’s a need to, I’ll test the app on a real device.
However, you do need to run your creations on a real device in order to test them properly. There are some things the Android emulator simply cannot do.
For example, let’s say you’re creating a car racing game and you need to use the accelerometer to control the steering.
Your laptop probably doesn’t have an accelerometer and even if it does, imagine how weird it would look if you moved it around like a steering wheel while developing the app.
In this scenario your app needs the device’s accelerometer and you have no choice but to test that functionality on an actual device.
Let’s give it a shot!
I’ll show you how to run your app using a USB cable and also how to run it over a wifi connection. Okay, so I’m gonna start by running the app on my Android device using a USB cable connected to my computer.
To connect your device via USB and run the app, you first need to enable USB Debugging. To do this, your phone must be setup for development. Do note that the exact process here will change slightly between different phones and versions of Android, but the basic steps should still remain the same.
Okay, let’s do that now.
- Open the Settings app.
- Scroll to the bottom and select About phone.
- Then select “Software Information”
- Locate the Build number.
- Then tap it seven times. You’ll see a message that says: “Developer mode has been enabled”
- I have developer mode previously enabled so you see the message telling us that’s there no need to enable it again.
- Head back to the settings screen, scroll to the bottom, and tap Developer options.
- In the Developer options window, scroll down and enable USB debugging.
When you enable it, a dialog would pop up, then you just tap OK.
Once you’re done, go back to the home screen on the device. Then connect your Android device to your computer using a USB cable if you haven’t already.
Your phone will prompt you with a dialog to allow USB debugging for your computer. If this is your computer, feel free to check the “Always allow from this computer” option so that you no longer get this message each time you plug in your phone.
Your device is now ready to run your app.
Back in Android Studio, click on the device selection dropdown button beside the run button. You’ll now be able to see your Android device. Go ahead and select it. Now before we hit the run button, remember that you wont be able to see the application since its running on a real device beside me. So how do I get to show you the running app?
Well Android Studio’s Device Mirroring to the rescue. Android Studio Electric Eel and up introduced the Device Mirroring feature which lets you connect and interact with a physical device right from your development machine. Isn’t that cool? Alright, let’s enable it.
Head over to the Android Studio menu above. Then open up the settings window. Go to tools, then select the “Device Mirroring” item. Then select “Enable mirroring of physical Android devices” checkbox. Tap OK to close the settings window.
Make sure your device is still selected in devices list. Then click on the run button. Android Studio installs and starts up Bullseye on your connected device.
Bullseye is now running on a real phone. I can also move the slider with the mouse even though its actually running on my phone. And you’re able to see it here because of Android Studio is currently mirroring my real device.
Okay, I’ll go ahead and test out the game by playing some rounds.
Everything looks fine and Bullseye is now a finished Android app that runs on a real device.
Android Studio Bumblebee and up also allows you to run your apps over Wifi. With this, there’s no need to connect your phone using a usb cable. You only need to make sure that your Android phone and computer are connected to the same wireless network. But this feature only works on Android 11 and up.
Let’s do that now. Since my screen is still mirrored on Android Studio, I’ll show you the steps right from my device.
For this you need to enable debugging over wifi.
- Head over to developer options
- Scroll down to the debugging section then turn on “Wireless Debugging”
- Select allow on the popup to allow wireless debugging on this network. If this is your computer you can select the always allow option.
That’s the setup for your phone to get things working.
Next, head over to Android Studio. Click on the device selection dropdown button. And you can see a network connected version of our devices is already on the list. If its not listed then you select “Pair Devices Using Wi-Fi.” A window opens up and in here, you can pair using QR code or using a pairing code.
At the bottom of the window, you can see where the QR code scanner is located on your Android device. Let’s head back to developer options on our device.
Then click on wireless debugging. And you can see both options for pairing. I’ll select pair device with QR code. It launches the QR code scanner. I’ll head back to Android Studio to scan the QR code. And you should also do that.
And instantly, the devices are paired. You can see it was successful. Alright, lets close this window.
And if you check the list of devices, you can see our network connected phone is automatically selected. You can see tcp at the end of the device name to show it’s the version that is paired over a network.
I’ll hit the run button one last time.
We now have Bullseye up and running on our device connected over wifi. Play a few rounds if you want.
I’ll go ahead and disconnect my USB cable. And guess what, Device mirroring still works but this time around, it is connected over wifi. Isn’t that cool?
Wow!!! I just got a perfect score!!! Isn’t that an amazing way to end this course?
You now have your first Android app running on a real device.