Building a Xamarin Android app in Visual Studio Team Services and deploying to App Center

In the old days, we use to build our Xamarin apps with Visual Studio and find a way to get the resulting .apk file to the end user for them to install.  Then came Hockey App.  It was and sort of still is (in transition) a distribution platform for Xamarin apps.  It was awesome.  Now of course, we have App Center, a completely modern and full fledged mobile app management and distribution service by Microsoft.  Since Microsoft purchased Xamarin, App Center was the next logical step and is among the many benefits of the acquisition.  Not surprisingly, using Microsoft’s Visual Studio Team Services (VSTS) you get integration with App Center.  Namely, you can configure build definitions in VSTS that allow you to deploy right to App Center.   Before we take that for granted let’s pause and let that sink in for a moment.  What this means is that when I check in my code, it triggers the build definition in VSTS which builds the code in the cloud on a hosted Xamarin agent, then deploys the resulting .apk file to App Center.   I can wash my hands of the mundane, boring task of building in Visual Studio, and manually updating App Center forever!  Dev Ops is all the rage right now, and I must admit, although building a single build and deploy definition is merely dabbing my feet in the water, this stuff can get pretty addictive.  Its like life hacking but with code.  The more I can automate my CI/CD (cringe) pipeline the better.

To get started, I’m going to assume you have a VSTS subscription and a project with a code repository set up.  I’ll assume you have an App Center account and an established Android application.  You can get help with those Here and Here.

To get started with your build definition, navigate to your project and select builds.  Then click the button to create a new definition.

 

Select the type and location of the source code you’re going to build.

 

Now we’re given the option to select a template.  Here we’ll search for Xamarin and select the Xamarin Android template.

 

Now the template is loaded and ready for us to configure as our build definition.  It contains steps that will execute in the order they are listed from the top down.  Templates are great for newbies like me in that they guide us on the right path.  Notice the first step that sets the cloud build environment to use Nuget 4.4.1.  This is a requirement and is already set up for you.  The next step is the Nuget restore step and will pull down the any nuget packages for your code.  I’ve actually set a custom nuget feed in addition to the regular official nuget to that pulls down some extra packages from a private source.   This is set up as a custom Service Endpoint and not covered here.

Next, configure the build step to your liking.  The defaults for this step pretty much work.  Make sure you use JDK version 8 and JDK architecture x64.

 

In the next step, upload the Keystore File that will be used to sign your app and enter the alias and password.

 

The default settings for the “publish artifact: drop” step should already be ok. After that, we’ll configure the deploy step that will send our app on over to App Center.  I’m skipping the optional ‘Test with Visual Studio App Center’ step that allows you to queue and run unit tests for the deployed code in App Center’s Test Cloud.

The App Center distribute step requires you to set up a service endpoint (pictured below) connection to App Center.  Follow the link above to set that up.

 

Go back to the App Center distribute step and configure the following:

– The App Center app slug. The app slug is how the build step locates your app in App Center. This is how you find out what your app slug is in App Center as described by Team Services:

“The app slug is in the format of {username}/{app_identifier}. To locate {username} and {app_identifier} for an app, click on its name from https://appcenter.ms/apps, and the resulting URL is in the format of https://appcenter.ms/users/{username}/apps/{app_identifier} . If you are using orgs, the app slug is of the format {orgname}/{app_identifier}.”

– Enter your desired release notes. You can direct the step to use a specially formatted release file here. I haven’t messed around with this specifically as I trigger the builds manually anyway, and I just fill in the release notes by hand.

– Select an App Center distribution group to publish to by filling in the group ID. If you select none, the default group will be used. To find the ID of a distribution group in App Center, go to the group and select the gear icon to change the group settings. The ID will be displayed there.

Save the definition and you’re ready to go! You just hacked life and saved time by letting VSTS do some work for you.

Using Azure Maps with a Xamarin Forms app

The new Azure Maps service is available, offering APIs and services that enable several common scenarios for location aware apps, all in one convenient place.   This is actually an impressive set of services, namely Search, Maps, Geocoding, Traffic, Routing, and even time zones!

Azure Maps Offerings

 

As soon as I saw this I thought why not build a Xamarin Forms app and use the Maps API to get POI data based on your location and display the results on a map?  So that’s what I did.

Its a simple proof of concept, one screen mobile app for Android.  The screen contains a Map and a search bar.  I used Xamarin.Forms.Maps Nuget package to display the map.  When you enter a search term, I query the Azure Maps Fuzzy Search REST API endpoint to sesrch for Points of Interest using the term you enter into the search box.   To get results local to you, I pass in the Lat and Long of your current location which I obtain by using the Geolocator Plugin.  Since the results of the API can be returned as JSON, I parse the JSON into my model, which I created using QuickType IO.  Then I simply loop through the results and add each of them to the map as a map Pin.  When you tap on a map pin, it shows you the venue name and address.  So that’s it, it was actually pretty easy to get started!  Here’s a screenshot from my Android Emulator with a simulated location.