Debugging allows us to see our app's flow and to find and fix errors. Let’s see how to do this with Xamarin on an Android device.
When we are developing an application, we usually need to use debug mode, either because we have an error and want to see what exactly is happening or just because we want to see the internal behavior of some action .🤷
Thanks to this tool, we are able to see the flow of the application in real time step by step. But how do we do this with Xamarin for Android in Visual Studio? In this post we’ll be learning just that! The topics will be divided in the following points:
🔹 What is a debugger?
🔹 Set up a device for development
🔹 Preparing the Android device
🔹 Installing USB drivers
🔹Testing the device on the computer
What Is a Debugger ? 🤔
It’s a tool included in most IDEs that helps us find and fix errors in program code. With this tool we can see, step by step, all the internal execution of methods inside our application to perform a certain action, allowing us to see even the value that each variable is getting at a given time while the debugger is running.
Set Up Device for Development
The best option we have to test an app is to test it on a physical device. For this, we just need to follow two main steps to apply a specific setting in our device and computer.
Prepare the Android device
Install USB drivers (This step is only for Windows. MacOS users don’t need this.)
Knowing the basic steps to complete, let’s start detailing them one by one!
Preparing the Android Device
The first that we have to do is to enable debug mode in our Android device. We have two ways to do this, depending on the Android version of the device:
For Android 4.0 to Android 4.1
For Android 4.2 and higher
Installing USB Drivers
Once our device is to enabled to debug, we have to install USB drivers.
⚠ It’s important to know that this step is only for Windows—you don't need it on Mac.
Step 1: Running the android.bat application
By default, the Android installer puts the Android SDK in following location on a Windows computer:
⚠ Please take into account that in some cases, some of these folders are hidden and you will have to show hidden folders in Windows.
Step 2: Downloading USB drivers
All Android devices should use the USB driver from their respective manufacturer, with the exception of Google Nexus devices, which require specific drivers for their use, found here.
To install the Google USB Driver package, you have to start the SDK manager, expand the “Extra” folder, check the Google USB Driver option, and finally push the Install button.
Step 3: Installing USB drivers
To install the driver, you must apply the following points:
- Connect the device to your computer.
- Right-click on My Computer from your desktop or Windows Explorer and then select Manage.
- Select Devices in the left pane.
- Expand Other Devices in the right panel.
- Right-click in the device name and select Update Driver Software. After this step, the Hardware Update Wizard will launch.
- Select Browse my computer for driver software and click Next.
- Click Browse and locate the USB driver folder (the Google USB driver is located in [Android SDK install path]\extras\google\usb_driver).
- Click Next to install the driver.
Testing the Device on the Computer
First of all, you have to be sure that ADB is installed on your computer. But if it’s not installed, you can download it here.
Let’s learn how to do it:
Step 1: Enter the letter Y in response to “Do you want to install ADB and Fastboot?”
Step 2: Enter the letter Y in response to “Install ADB system-wide?”
Step 3: Enter the letter Y in response to “Do you want to install device drivers?”
Step 4: And finally an installation wizard will appear. You click Next until finished.
And now, we can start testing our device! 😎
We have two ways to do it: USB Cable or WiFi.
➖ Connecting with the USB Cable: Just connect your device to your computer and it will be available to debug! (Your device gets this as a default way.)
➖ Connecting with WiFi: Another way is to connect through a Wifi network, which is very useful because we don’t have to have the cell phone connected to a USB cable, and it allows us to have a further distance between the device and the computer. To do this, you must follow the following steps:
- Go to Settings
- Tap WiFi option them select the WiFi network that the device is connected to.
- From the information that displays, we’ll save the IP Address.
- Connect your Android device to your computer via USB.
- Next, restart ADB so that it uses TCP on port 5555. From a command prompt, type the following command:
adb tcpip 5555.
- Disconnect the USB cable from your device. (It can’t look for devices that are connected via USB.)
- Configure ADB so that it will connect to your Android device on the port that was specified in step 5 above:
adb connect XXX.XXX.X.XXX:5555 (Your_IPAdress:5555)
And done! Our device has gotten WiFi connection! 😎
Finally, our debug mode is enabled both Visual Studio For Mac and for Windows. It should look like this:
That’s all for today! I hope this article is useful for you!
Thanks for reading! 💚