Getting Started

Setting up the Development Environment

To develop cross platform mobile apps with Xamarin you will need both software and hardware.

Hardware Requirements

If you are going to develop for iOS from Windows, you will need access to a Macbook in order to build your project. iOS applications can not be built on a non-Mac hardware.

If you would like to use to use Visual Studio on a Macbook, you will need to run Windows in a Parallels Virtual Machine and connect it back to the Macbook in order to build the projects.

  • Macbook Pro running Parallels software
  • Windows machine with access to a Macbook
  • iPhone/iPad (Optional)
  • Android Phone/Tablet(optional)

If you want to test on a real device, you will need access to a phone or tablet. These are not required as the software development kit comes with emulators.

Software Requirements

  • Visual Studio on Windows
  • Xamarin Studio for both Mac and Windows
  • Android Studio for developing Layouts
  • XCode for the iOS SDKs and building UIs

Note

Android Studio is needed only for building the Android layouts since both Visual Studio and Xamarin Studio don’t have full auto-complete support and realtime preview.

You can alternatively use XCode for building complex UIs, since you can open the Xamarin Storyboard files in XCode. Xamarin Studio and Visual Studio support for auto-layout is not that great.

Installing Xamarin Studio on Mac

The Xamarin Studio Universal Installer makes installation easy and straight forward.

Step 1 - Download Xcode

Go to the App Store on the Mac and download the latest version of XCode Xcode

Step 2 - Download the installer from Xamarin Website

Xamarin Universal Installer

Step 3 - Run the downloaded installer and accept to continue.

Run Installer

Step 4 - Accept the alert dialog

Alert Dialog

Step 5 - Choose the products to install

Xamarin Studio will detect the currently installed products. Choose the product you want to install. Untick Xamarin for Mac we wont need it.

Product Selection The installer will install a private copy of the Android SDK, Xamarin.Android, Xamarin.iOS and Xamarin Community Studio

Step 6 - Configure where the SDK

Xamarin installs its won private copy of the SDK but can point it to the same SDK as Android Studio and don’t have to download another copy of the SDK.

Configure SDK Location

Step 7 - Installation Progress

The installation will continue and download the required software. If anything fails at this stage, you will get a log and will be required to and manually download the software and put it at the right locations.

Installation Progress

Step 8 - Installation Complete

If there were no issues you will get to installation complete screen, otherwise you will be required to manually download the software.

Installation Complete

Step 9 - Register for an Account

Open Xamarin Studio and register for an account. You will need an account to install from the Xamarin Component Store.

Register an Account

Installing Xamarin for Visual Studio

Xamarin is now included with Visual Studio 2015 Update 2.

To install follow the step by step guide on the Microsoft Website.

Alternative Xamarin Plugin Install for Visuals Studio

You can alternatively install Xamarin plugin for Visual Studio on its own.

Step 1 - Download Xamarin for Visual Studio

Go the []Xamarin Download](https://www.xamarin.com/download) page and download the Xamarin Visual Studio Plugin

Download Visual Studio Plugin

Step 2 - Run the Installer

Run the installer and choose all the products you need to installer.

Step 3 - Install Compete

Once the install is complete you can now configure the Android SDK and connect to a remote Mac machine and also regsiter your account

Go to Tools -> iOS -> Xamarin Account to configure the Mac agent for building iOS applications.

Visual Studio Xamarin Tools

Step 4 - Enable Remote Login on the Mac

Use spotlight on the mac and search for Remote Login

Spotlight Remote Login Search

Step 5 - Enable Remote Login for all Users or a Single User

Choose the users you would like to access the Mac remotely.

![Enable Remote Login]((/images/windows-installer/remote-login-for-all-users.png)

Step 6 - Connect the Mac Agent in Visual Studio

Go to Tools -> Options -> Xamarin -> iOS Settings .

Macs enable for remote login

Step 7 - Choose the Mac

Choose the Mac

Add the Mac

If your Mac does not appear and you are certain you have enabled remote login and its on the same network, then you can manually add the Mac by using its IP Address.

Step 8 - Login with your SSH credentials

Login with your Mac username and password. Remember your mac username is all lowercase and does not contain spaces.

Login to the mach

Finding your Mac username

Open spotlight and search for users and groups. You will find your username there.

Step 9 - Verify Connection to Mac

If you have successfully connected to the Mac, the toolbar will change and have additional options

Options on toolbar

Installing Genymotion Emulator

Genymotion is an incredibly fast, memory-efficient VM that runs the Android OS in a more accurate manner than even the official emulator. Many Android developers do all their device testing using this emulator especially when Google Play services is concerned. In addition, the official Android emulator is plagued with a lot of bugs (i.e. intermittent network loss) that Genymotion is usually a far more reliable option.

To setup your genymotion emulator sign up and follow the installation guide.

Installation

  1. Sign up for an account on the Genymotion Website
  2. Install the latest VirtualBox, a powerful free virtualization software for Genymotion to run.
    • If you already have VirtualBox installed, be sure to open and upgrade to the latest version.
  3. Download Genymotion Emulator v2.7.0 or higher for your platform.
  4. Install the Genymotion Emulator
    • Windows: Run the MSI installer
    • Mac: Open the dmg and drag both apps to Applications directory
  5. Go to the Genymotion ADB settings and set the path to your SDK directory (i.e. for Mac OSX, the directory is /Users/[username]/Library/Android/sdk. For Windows, all SDK files should be by default in C:\Documents and Settings\<user>\AppData\Local\Android\sdk) image
  6. (PC’s only) You need to reboot and enable Intel Virtualization Technology or Intel VT-x on the BIOS. Typically you need to reboot and hit F1, Esc, or F10 to enter this mode. See these instructions for more information.

Configuration

  1. Run the Genymotion application
  2. Sign in and add your first virtual device (i.e. Nexus 4 - 4.4.4 - API 19, Nexus 4 - 5.0.0. - API 21).
    • You must use an image running 4.3, 4.4.4, or 5.0.0. Other versions may not work.
    • Do not start your emulator yet!
  3. From within Android Studio, click the genymobile icon Genymobile and click “Start” on your virtual device.
    • You can start the emulator through the Android Studio plugin, or can launch the Genymotion application separately.
  4. Wait for device to boot up into a useable state

Note: if starting Genymotion you see the following error:

Unable to start the virtual device
VirtualBox cannot start the virtual device
To find out the cause the problem, start the virtual device from VirtualBox.

Try opening VirtualBox and opening the image directly. If you see an VERR_PDM_DRIVER_NOT_FOUND error, try disabling the audio settings for the virtual image in VirtualBox:

  1. Select Android VM
  2. Click Settings on top.
  3. Go to Audio and Uncheck Enable Audio Checkbox

See this link for more information.

Note: On Ubuntu/Linux, make sure to 3D acceleration mode by launching VirtualBox and going to Settings -> Display to fix. VirtualBox appears to prone to memory leaks, so you may find yourself killing the process from time to time. To avoid large CPU consumption by the compiz window manager and swapping in general, try increasing the video memory allocation and Base Memory (found in Settings -> System).

Note: Are you getting an error when starting the emulator? Error Failed to load VMMR0.r0? Follow the advice here. In short, go to virtual box page and download and install VirtualBox 4.3.6 Oracle VM VirtualBox Extension Pack.

Note: If you get Failed to load OVI error when re-adding the emulator, you need to use a new name for the same device. For example, “Nexus 4 - 4.4.4 - API 19” might be called “Nexus 4 - 4.4.4 - API 19 New”.

Setup Google Play Services

NOTE: These steps need to be followed only if you want to be able to use Google services such as maps and push messaging on your Genymotion device. For basic testing, these steps can be safely skipped.

Check out this handy youtube video for a guided step-by-step of enabling play services in Genymotion. You may want to use a newer version of these files based on the desired emulator as found below.

  1. Download the ARM Translation Installer v1.1 and drag and drop the zip file onto the running Genymotion emulator device. The ARM emulator is only needed for apps that trigger a INSTALL_FAILED_CPU_ABI_INCOMPATIBLE error.

    • Note: If you get Files successfully copied message, you need to make sure there are no spaces in the filename. Remove any spaces from the name of your zip file before dragging to ensure the file is detected as flashable.
  2. You must close and restart the emulator fully before continuing.

  3. The Google Play APK package needed is specific to the Android emulator version. You must use the corresponding Google Play Service package. Otherwise, you may notice problems with Google Play not having Internet connectivity or other strange issues.

Version APK Link
Android 7.0 Download from GApps (Untested)
Android 6.0 1) gapps-L-4-21-15.zip 2) benzo-gapps-M-20151011-signed-chroma-r3.zip (See instructions below)
Android 5.0 Google Play Services APK - requires Genymotion 2.4.0+
Android 4.4.4 Google Play Services APK
Android 4.3 Google Play Services APK
  1. Drag and drop the zip file onto the running Genymotion emulator device Installing Google Apps APK

NOTE: For Android 6.0, you need to first flash the first .ZIP file. Then you need to sign-in to your Google account and then flash the second file. See these instructions for more details.

  1. When asked to flash the device, make sure to proceed with the installation.

    • At this point, ‘Google Apps Services’ will crash frequently with the message “google play services has stopped working”.
    • Note: If you get Files successfully copied message, you need to make sure there are no spaces in the filename. Remove any spaces from the name of your zip file before dragging to ensure the file is detected as flashable.
  2. You must close and restart the emulator so that Google Play Store can be installed.

  3. After restart, open the “Play Store” app on your emulator and sign in with a google account.

    • If you can’t find Google Play, try updating the Google Hangouts app to trigger an update to the Play Store.
    • You may see messages that the Google+ app needs to be updated. You will need to go to the Google Play Store and click “Update”.
  4. Android 5.0.0 only: If you are using Android 5.0.0, you will be prompted to update to the latest version of Google+ app. In addition, make sure to upgrade to at least Genymotion 2.4.0.

  5. Make sure to update to the latest version of Google Play Services by opening the “Play Store” app and then the “Maps” app to verify play services is running correctly.

Note: If you see errors, be sure you installed the correct package above that matches the device version in the emulator. For example, if you install Samsung Galaxy S4 API 18 for Genymotion emulator and install Google Services APK for Android 4.3, Google Play should work without any issue. The important thing is to match the google play API version and Genymotion emulator device version.

Enable GPS on Emulator

Next, if you don’t have the emulator started yet, be sure to boot the genymotion emulator from within the Android Studio plugin:

Emulator

Now we need to enable the GPS location on the emulator by manually selecting a location on the map:

Emulator

Troubleshooting

Can’t run app on Genymotion emulator

If you are encountering issues with a Genymotion device not being detected inside Android Studio, try the following steps. First, close all emulators and unplug any devices. Next, open up the Terminal application on your computer.

cd ~/Library/Android/sdk/platform-tools/
./adb kill-server
killall -9 adb
./adb kill-server

Open up the Genymotion application and open Settings => ADB and then select “Custom SDK tools”. Enter your SDK path into the text field: “/Users/[USERNAME]/Library/Android/sdk”:

image

Finally, go back to the terminal and run:

cd ~/Library/Android/sdk/platform-tools/
./adb start-server
./adb devices

Keep running ./adb devices until you see a device show up in that list:

List of devices attached
192.168.57.101:5555	device

Connecting to VPN sites

If you are trying to use a VPN client to connect to an internal web site, open VirtualBox and select the emulator image. Click on Settings and make sure Adapter 1 is disabled.

Verify that Adapter 2 is selected for NAT, which will force connections to be routed through your machine.

That’s it! Try running the app again.

Running on an Android Device

When building an Android app, it’s important that you always test your application on a real device in addition to emulators. This page describes how to set up your development environment and Android-powered device for testing and debugging on the device.

If you want an ideal SIM-unlocked phone to test on, then you might consider a Nexus phone.

Connect your Phone to Computer

Plug in your device to your computer with a USB cable. If you’re developing on Windows, you might need to install this universal ADB USB driver or find your specific USB driver for your device.

Enable USB Debugging

The next step is to enable USB debugging so your phone can interact with your computer in a developer mode.

The following steps are needed:

  1. (Windows Only) Install this ADB Driver
  2. Plug-in your Android Device to Computer via USB
  3. Open the “Settings” App on the Device
  4. Scroll down to bottom to find “About phone” item
  5. Scroll down to bottom to find “Build number” section
  6. Tap on “Build Number” 7 times in quick succession
  7. You should see the message “You are now a developer!”
  8. Go back to main “Settings” page
  9. Scroll down bottom to find “Developer options” item
  10. Turn on “USB Debugging” switch and hit “OK”
  11. Unplug and re-plug the device
  12. Dialog appears “Allow USB Debugging?”
  13. Check “Always allow from this computer” and then hit “OK”

Watch this video tutorial for a visual guide to getting USB debugging enabled.

Running your App

Now, we can launch apps from Android Studio onto our device:

  1. Select one of your projects and click “Run” from the toolbar.
  2. In the “Choose Device” window that appears, select the “Choose a running device” radio button, select the device, and click OK.

Once Gradle finishes building, Android Studio should install the app on your connected device and start it.

Troubleshooting

Not seeing your device in the “Choose Device” window? Try the following:

  • Unplug your device from the USB port on the computer
  • Restart the device by powering off and back on
  • Verify that Settings => Developer options => USB Debugging is enabled
  • Quit and re-launch Android Studio
  • Force restart ADB from the “Android Device Monitor”
  • Plug your device back into the USB port on the computer
  • Unlock the device and press “OK” on any dialog displayed

Now the phone should work as a debugging device as expected!

Still Not Working?

If after plugging the device into the computer and you don’t see any message about authorizing the device, then you may need to purchase another USB cable. Not all USB cables are enabled for data transfer. If there’s a chance that your cable may be a charging only cable, you can purchase a USB-C cable for Nexus 6P or the micro-USB cable for Nexus 6 and prior.

References