Hello Xamarin World – first steps

This time I want to say “Hello” to Xamarin world.

First thing that needs to be done before starting Xamarin development, is obviously the installation of all necessary components.

In this post I want to share my feelings about the installation (not without issues of course), configuration of my Xamarin Android solution and deployment process to the device.

Xamarin installation into Visual Studio 2015

I’ve created my account at xamarin.com, logged in and chose Download Xamarin for free to download the installation package. There are generally two IDEs that can be used for Xamarin development: Visual Studio or Xamarin Studio. I’ve already had VS 2015 installed and decided I don’t want to explore a new IDE, so I downloaded installer for Visual Studio. What’s more, following Xamarin’s website, “Xamarin Studio is no longer included” in the Windows installer and they “encourage Windows developers to move to Visual Studio”. Well, that’s understandable, Microsoft acquired Xamarin, didn’t they? 😉

BTW, when downloading Xamarin I was obliged to fill the “Company” field – does anyone know why is that?

 Android SDK and emulator images size

Interesting parts of the installation (if there could by any at all?) are Android Emulator installation which is then integrated into Visual Studio as well as Android SDK installation – fortunately it’s possible to change the location of Android SDK installation, because right after the setup it takes an enormous amount of disk space (with mandatory tools mentioned in the installation guide and two Android APIs):

Android SDK size - 41.6 GBs
Android SDK size

I’ve examined a bit more and it turns out that system images take the most of this space. Those are used only by emulators to be able to create a virtual device and debug on a simulator instead of deploying the app to a physical device every rime.

When I used to write some Android apps in Java and Android Studio long time ago, I found it very hard and complex to debug/test anything using emulator – it was simply very, very slow to load and work. This time I’ll see if emulator built in Visual Studio will do the job – if yes, why not using it ? I will of course also deploy directly to the device for tests, but it also takes time. I remember that when I wanted to test some small adjustments done “on the fly” (e.g. UI modifications/alignments) I dreamt about doing it quicker then deploying app to the phone every time. We’ll see, if emulator won’t do the job – I will clean up those folders.

 First issue in DSP

I had to create a separate paragraph for that 🙂 Of course, it must have finally happened – first issue on my DSP way! Installation error, of course – “Installation of ‘Xamarin’ failed with more then one exception (attempt 3). It was not possible to complete an automatic installation.”

I expected the installation to terminate and evening spent on looking for a solution, exploring StackOverflow threads, but to my surprise a quite familiar-looking screen appeared explaining what to do next, i.e. how to install all the components manually. I had to download and install Java JDK, Android SDK and Xamarin.

After the next 30 minutes everything seemed to be installed. Just two last configuration steps in Visual Studio as described here. Finally, to be able to run Android emulator Android Virtual Device Manager needs to be configured, as this tutorial describes.

Android Solution Creation

I cloned my Git repository, opened Visual Studio and created MoneyBack solution 😉 I chose the project of type Blank App (Android). Here’s what VS produced:

VS Android Xamarin Solution
VS Android Xamarin Solution

Android Xamarin solution contains the following elements out-of-the-box:

  • Properties – here AssemblyInfo.cs is probably well-known to all .NET developers (for the others: this file is .NET assembly’s metadata file, containing basic data about our application, like its name, version, company etc.), but there is also AndroidManifest.xml, which contains details and requirements for Android application, e.g. app’s name, supported SDKs versions numbers and required permissions for which the end user is asked when installing the app
  • References – contains other assemblies required for the application to build; in case of Xamarin Android solution, it references both .NET assemblies like System or System.Core and Mono.Android assembly
  • Components – stores Xamarin development packages that can be retrieved from Components Store (something like Xamarin-specific Nuget, I guess 😉 )
  • Assets – allows to store additional files needed by the app (e.g. text files) available easily from the code
  • Resourcescontains strings, images and layouts of the application

MainActivity

Apart from above-mentioned folders, solution contains an Activity class (MainActivity.cs) which is decorated with ActivityAttribute:

[Activity(Label = "MoneyBack", MainLauncher = true, Icon = "@drawable/icon")]

We’ll get into details of activities soon. For now, let’s just assume that:

MainLauncher = true

means that this class is the starting point of the application. It contains only one method:

protected override void OnCreate(Bundle bundle)
{
   base.OnCreate(bundle);

   // Set our view from the "main" layout resource
   SetContentView (Resource.Layout.Main);

}

Again, we’ll dive into Android activity’s lifecycle very soon, but we can already imagine that “OnCreate” method is called when the activity is created.

MoneyBack on the phone

 

I plugged my Huawei P8 to the laptop, hit F5 and hoped to see my app running on the device. Instead, two components were installed first as separate apps – Mono Shared Runtime and Xamarin.Android API-25. Those two are used in debug build mode to allow debugging stuff. It would be fine if I didn’t have to re-plug my phone after installation of each of these two, as phone was doing nothing after.

 Finally,  MoneyBack appeared on Huawei’s screen with sad, black empty canvas. I added some well-known sentence to become a real Xamarin-newbie:

MoneyBack - Hello world deployed to the phone
MoneyBack – Hello world deployed to the phone

MoneyBack on the emulator

I couldn’t initially manage to deploy my application to Android Emulator device. I tried creating few emulator devices – firstly I had issues with very strange resolution on the emulated device, then I created another one and when I tried to deploy on it I got an error saying “2>Emulator is not ready to be used”. What I came up to is creating new Android Emulator Device, trying to start it (without deployment) and having “android” text on the device’s screen for the last 20 minutes. Then I found this emulation acceleration guide, so I installed HAXM and it helped A LOT. Emulator is now starting within 30 seconds and I managed to deploy my app on it. Cool.

Summary

Automatic installation of Xamarin on Windows with Visual Studio didn’t work, so I had to install all the packages manually, however it wasn’t problematic. Deployment to the phone was OK, but the installation of two additional components (Mono Shared Runtime and Xamarin.Android API) forced me to re-plug the device twice. I hope I won’t have this case very often, because if every deployment looks like that I’ll never finish this app…

I recommend everyone who wants to work with Android device emulator installing HAXM – it really boosts the emulation.

Get ready for a deep dive into Xamarin soon 😎

 

Leave a Reply

Your email address will not be published.