Wednesday, March 22, 2017

Xamarin - getting started

Nothing to do with WPF except they both use XAML. I finally figured out how to get the whole emulator thing working. It's ridiculously difficult.

I installed Visual Studio 2017 Professional when Microsoft released it. It has better integration of Xamarin Studio than 2015 had. This made the process of creating my first Android App in Visual Studio much easier - but still complicated.

I also broke down and bought a book "Xamarin 4.x Cross-Platform Application Development - Third Edition" from Amazon.com. It's not a great book but it has more detail than the online examples I could find.

So here is the list of things I had to do to get my first app working.

I got my copy of VS2017 through my MSDN subscription. You should be able to get a free community version from Microsoft.

Setting up Visual Studio for the first time.

Start Visual Studio 2017 (any version)
Click on Tools -> Android -> Android SDK Manager.



Make sure that the following are installed or checked...
Tools/Android SDK Tools
Tools/Android SDK Platform-tools
Tools/Android SDK Build-tools (as many versions as you want - recommend 23.0.3)
Android 7.1.1 (API 25)/SDK Platform
Android 7.1.1 (API 25)/System images for all devices you want to emulate
Extras/Android Support Repository
Extras/Google USB driver
Extras/Intel Hardware Accelerated Execution Manager

Click [Install packages]

Close Visual Studio.

Now you need to install the hardware accelerator. In File Manager browse to C:\Program Files (x86)\Android\android-sdk\extras\intel\Hardware_Accelerated_Execution_Manager and run intelhaxm-android.exe.

Open Visual Studio again. Now we are ready to develop.

Click on Tools -> Android -> Android Emulation Manager



You may see a different list of virtual devices.

Select "VisualStudio_android-23_arm_phone" and click [Start...]. This launches an Android phone emulator. You will see a "Launch Options" popup. Click [Launch].

You will now see a "Starting Android Emulator" popup. Make sure there are no errors listed. In the example below the only error concerns the audio which I don't care about. You can close it once the green bar is full.



At the same time the emulator will popup and once it has initialized it will look like a phone. It can take as long as five minutes to initialize. While it is initializing it shows the word "android".



When you run your program it will connect to the emulator and run there. It's quicker to have the emulator running before you build. However, the build will start an emulator if you don't have one running but it takes forever.

While the emulator is initializing we can start developing.

In the Visual Studio menu click File -> New -> Project. You will see the "New Project" popup.



In the left panel under Templates select Visual C# and then Android. In the right panel select Single-View App (Android). Enter a project name and click [OK].

This creates a single-view application with some minimal functionality that is useful for us but which will be removed most of the time. After Visual Studio creates the app your screen will look something like this.



You can explore the solution if you want or you can just build and run the application (hit F5). Remember we have the emulator running already showing the clock, icons, etc. If the emulator is not initialized (looking like the image above) you should wait until it is.

The process of connecting takes some time. Your output window and the status bar at the bottom of Visual Studio will show the progress. The status bar will show the following messages as the deployment progresses.

Starting deploy...
Deploying...
Android application is debugging
Ready

It takes fully two minutes for the emulator to start showing this program on my computer and this is a trivial program with the emulator already running. However, eventually, it looks like this.



Click in [HELLO WORLD, CLICK ME!] to test the application.

You have to understand how incredibly slow Xamarin is in its default configuration. I think a lot of the problems I was having is that I'm used to Visual Studio only taking a few seconds from hitting F5 to running even on massive solutions.

Are you old enough to remember the bad old days of batch compiles and builds when we had to wait five or ten minutes before we could start debugging? I'm talking about the HP3000 COBOL compiler and build engine. Xamarin has taken us back to the old days.

No comments:

Post a Comment