I know what you’re thinking, “Another debugging article?!”. Well, that’s only half right. This time, we’re going to learn how to build an apk file for an Ionic Android app, run it on a USB-connected Android device, and see all the logs of the phone in your terminal window. It may sound like a lot, but with just a few simple steps, you’ll be up and running in no-time at all!
Why Not Just Emulate?
When working with Android apps, you have the option of running the app on an emulator on your computer. This is especially useful when you don’t have an appropriate, or any, Android test devices available. Maybe the phone you do have is running on a version of Android that’s incompatible with your app. In this case, you’d want to emulate instead. When you emulate, however, it must be said that Android Studio eats up a whole heap of resources, making your computer noticeably slower in some cases. This isn’t always efficient, and can make it difficult for you to do anything other than emulate at the time. Rather than rely on your computer’s resources for emulation, you can actually build your apk and run it on a USB-connected device, if you have one available. This way, the app is using the phone’s resources, leaving your computer free for you to do other things. We’ll get to why USB Debugging might be a good option later, when we talk about logs.
Sounds Fancy, What Do I Need?
For this, you’ll need any Android Device. Ensure the device is running in Developer Mode and that USB Debugging is enabled. Doing this varies across devices, but it’s not at all difficult to find and is usually configurable via the Settings app. The steps needed to enable Developer Options and USB Debugging are described in this article:
Next, and most importantly, you’ll need to download the correct OEM (original equipment manufacturer) USB device driver. This driver is needed for you to perform USB Debugging of Android devices. The OEM USB drivers for most Android devices can be found at the following link, as well as instructions on how to install them:
Uh, Did It Work?
If you have your Android device connected, and installed the correct driver from the link above, then you can run the following command in the terminal window to see if your device is listed as an ADB (Android Debug Bridge) device:
adb devices -l
You should then see your device listed as an adb device.
Okay, What Now?
Now that your device is connected, you’re ready to build your Ionic Android app and run it. To do this, run the following command in the terminal within your app’s project folder:
ionic cordova run android --device
This command builds the apk, then attempts to install and run it on a connected adb device. Even if another version of your app is already installed on the device, it will uninstall it and install the new apk for you.
What About Logs?
Having your app built and running on a USB device gives you the ability to view Android “LogCat” logs of that device/app on the connected computer. To do so, simply run the following command in the terminal window:
That will display all the Android logs of the connected device in your terminal window. A fair warning, though, the logs are constantly being written and it can get a bit difficult for you to keep up with it. It’s for that reason, I often write them to a file, as this is persistent and can be easily read/parsed. To write your Logcat logs to a file, run the following command in the terminal:
adb logcat -d > logcat.txt
Lastly, to clear the Logcat logs, as they do build up while the device is connected, run:
adb logcat -c
LogCat provides you with all the logs of your device, which can be especially useful when debugging things, like why push notifications aren’t showing or whether or not they are being received.
Happy USB-Debugging! Until next time…