XamarinT2 Light_1200x303

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.

  1. Prepare the Android device

  2. 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

Apply the following steps: Go to Settings -> Select Developer option -> Check USB debugging option and done!

For Android 4.2 and higher

Apply the following steps: Go to Settings -> Go to About device option -> Tap Build number item *seven times* -> Go to Developer options -> Check USB debugging options

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.

Let’s start!

Step 1: Running the android.bat application

By default, the Android installer puts the Android SDK in following location on a Windows computer:

C:\Users\[username]\AppData\Local\Android\android-sdk

⚠ 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.

Google USB Driver Package is highlighted in teh Android SDK Manager.

Step 3: Installing USB drivers

To install the driver, you must apply the following points:

  1. Connect the device to your computer.
  2. Right-click on My Computer from your desktop or Windows Explorer and then select Manage.
  3. Select Devices in the left pane.
  4. Expand Other Devices in the right panel.
  5. Right-click in the device name and select Update Driver Software. After this step, the Hardware Update Wizard will launch.
  6. Select Browse my computer for driver software and click Next.
  7. Click Browse and locate the USB driver folder (the Google USB driver is located in [Android SDK install path]\extras\google\usb_driver).
  8. 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?”

Do you want to install ADB and Fastboot?

Step 2: Enter the letter Y in response to “Install ADB system-wide?”

Install ADB system-wide?

Step 3: Enter the letter Y in response to “Do you want to install device drivers?”

Do you want to install device drivers?

Step 4: And finally an installation wizard will appear. You click Next until finished.

The familiar look of an install wizard.

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:

  1. Go to Settings
  2. Tap WiFi option them select the WiFi network that the device is connected to.
  3. From the information that displays, we’ll save the IP Address.
  4. Connect your Android device to your computer via USB.
  5. Next, restart ADB so that it uses TCP on port 5555. From a command prompt, type the following command: adb tcpip 5555.
  6. Disconnect the USB cable from your device. (It can’t look for devices that are connected via USB.)
  7. 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:

Visual Studio demo when the code is debugged

Wrap-up

That’s all for today! I hope this article is useful for you!

Thanks for reading! 💚

References:
https://docs.microsoft.com/en-us/xamarin/android/get-started/installation/set-up-device-for-development


LeomarisReyes
About the Author

Leomaris Reyes

Leomaris Reyes is a Software Engineer from the Dominican Republic, with more than 5 years of experience. A Xamarin Certified Mobile Developer, she is also the founder of  Stemelle, an entity that works with software developers, training and mentoring with a main goal of including women in Tech. Leomaris really loves learning new things! 💚💕 You can follow her: Twitter, LinkedIn , AskXammy and Medium.

Related Posts

Comments

Comments are disabled in preview mode.