Write once, run everywhere is the ultimate goal when writing software. As well it should be. The more code you have to maintain, the more expensive, time consuming and error prone that code will become as time progresses. Lucky for us there are various multi-platform solutions out there at our disposal. Some free and some not so much. Just recently Microsoft acquired one of the largest of those companies, Xamarin.
Xamarin takes native .NET code and converts it to the equivalent on all major mobile platforms. You maintain your code in one location, and you can publish to IOS, Android and Windows Mobile devices with that single code base. And because of Microsoft's acquisition, everything now runs directly on Visual Studio. So today I will be running through the initial processes of setting up a Xamarin project in .NET. I will only be targeting Android in this example, but will cover IOS and Windows on a separate post. I'll say this now, just setting up has taken me several days and several laptops. And here is the process and what I learned along the way.
Before You Begin
While Visual Studio 2015 nowadays requires some hefty resources in order to run properly, that is nothing compared to Xamarin. While creating and working on a Xamarin on a project isn't a huge resource hog, testing said project can bring your machine to a halt, as it did with me. Emulating an Android device, or any device really, will require a good amount of RAM and it would also help to have a decent GPU. My current configuration is as follows and my machine does indeed struggle to keep up.
- ASUS
- CPU: i5
- RAM: 6GB
- Hard Drive: 5400RPM
- Graphics: Embedded
I also tried the following:
- ASUS
- CPU: i5
- RAM: 8GB
- Hard Drive: 7200RPM
- Graphics: UMA
This configuration did a little better, but it froze my machine more often than the previous one. Just a few things to consider before starting on the mobile app journey.
Installing Xamarin
The easiest way to install Xamarin is to update your Visual Studio to the latest version. That installer can be located here. During the installation you will be asked which SDK's you will want to download. To be safe, I selected all of the ones available, at a very hefty cost to my storage space I will add.
Already Installed?
If you already have Visual Studio 2015 Community installed, you can check if you have the latest version of Xamarin by going to Tools -> Options -> Xamarin -> Other.
Install HAXM
You're going to need this for improved performance. This can take running your projects from 10 minutes to 20 seconds. The Intel "Hardware Accelerated Execution Manager", or HAXM for short, is a hypervisor that adds hardware acceleration to Intel images in the SDK. Before installing it, you need to make sure that your chipset will support it. And if not, you're probably going to need better hardware I'm afraid. You can download HAXM from the following url.
https://software.intel.com/en-us/android/articles/intel-hardware-accelerated-execution-manager
You also have the option of installing HAXM through the SDK Manager in Visual Studio.
I however, found that unsuccessful, as I had an older version of HAXM on my machine and Visual Studio insisted on selecting that one before any others. So just to be safe, remove any version of HAXM on your computer, then do a fresh install on your machine. In the SDK Manager you should then see the following:
Create New Project
Now our dev environment should be ready for work. Your process might go smoother and just work, but mine did not. So hopefully this first half covers any issues that others may face.
And that should have generated our default Android project, complete with everything needed to make it run. So let's take a look at some of the more important tools that Visual Studio provides for our Android development. Looking at the screenshot below, you will see the AVD button. This will fire up the Android Virtual Device Manager. This is where we define the virtual devices that we want to target.
Check Your AVD's
In order to run your projects you are going to need to set up an Android Virtual Device. By default, there are a few that are already set up in Visual Studio that are ready to be launched.
Before you launch it however, click on edit after selecting your primary device, and be sure to select the Use Host GPU option near the bottom.
This will ensure a faster process overall. You can also configure many other aspects of the AVD, such as the amount of RAM to use and the amount of simulated SD storage on the device. This will more than likely become more important as you push applications live and will need to test on all devices.
Run The Emulator
At this point, we should have our AVD configured and our project should be good to deploy.
Troubleshooting
This is where the fun begins. As I mentioned above, it took me a few days to get everything up and running. For one, this kept happening whenever I created a new project:
And from what I could tell, missing SDK's in my configuration were causing this issue. So open up the SDK manager and install any missing libraries you may need.
Another issue that I ran into dealt with running the emulator. Certain Virtual Devices will require more resources than others. For example, if you're target device is the latest Galaxy phone you're going to need more than 512MB of RAM to run.
If you've read this far, then that means you are interested in learning more about Xamarin and Visual Studio. So fret not, as I am working on Part II of this tutorial, which will cover the more programmatic aspects of building apps with Xamarin. Coming really soon. For any questions/comments/concerns about today's post however, feel free to send me a message down below.