Visual Studio 2015 integrates Xamarin platform so users can write Android apps in C#. I took advantage of some free time after work, got Visual Studio 2015 Enterprise, and made a simple Android app. In this article I will talk about the first steps in making Android apps from VS 2015.
After installing VS 2015 with Xamarin components, a Xamarin license is required to make Android apps. You can get a free trial from Xamarin, or, if you are a student, get a DreamSpark subscription to obtain a free license.
A good tutorial to get started on Android in VS 2015 can be found on this MSDN article. The following is how I made my Android app.
The first thing I noticed was the different Java coding styles in native Android and Xamarin Android. Class libraries from different frameworks, different cases, properties in one that are implemented as methods in the other, etc. For example, take a look at the my code to create an image file:
private Java.IO.File createImageFile() { // Create an image file name string timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").Format(new Java.Util.Date()); string imageFileName = "JPEG_" + timeStamp + "_"; Java.IO.File storageDir = Android.OS.Environment.GetExternalStoragePublicDirectory( Android.OS.Environment.DirectoryPictures); Java.IO.File image = Java.IO.File.CreateTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents string mCurrentPhotoPath = "file:" + image.AbsolutePath; return image; }
And the code used by native android from developer.android.com:
private File createImageFile() throws IOException { // Create an image file name String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); String imageFileName = "JPEG_" + timeStamp + "_"; File storageDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES); File image = File.createTempFile( imageFileName, /* prefix */ ".jpg", /* suffix */ storageDir /* directory */ ); // Save a file: path for use with ACTION_VIEW intents mCurrentPhotoPath = "file:" + image.getAbsolutePath(); return image; }
Notice the differences between method signatures, method names (GetExternalStoragePublicDirectory vs. getExternalStoragePublicDirectory), and class references (for example, in Xamarin, File has to be replaced by Java.IO.File to avoid confusion with System.IO.File).
Other than this, Xamarin android development is much similar to native. What is truly amazing is the ability to use .NET as the sole logic core for cross-platform apps. That being said, if an application is logic-driven, Xamarin will make cross-platform development a breeze; if, however, an app is presentation-driven, native development is probably much more powerful.
To make this application, I went over the following steps:
1. Subscribe to Microsoft’s Face API.
2. In VS 2015, start a new Android project, add reference to Client library provided by Face API.
3. Design UI in Main.axml and add corresponding code to MainActivity.cs. Code in MainActivity.cs gets a Bitmap from a chooser or the camera, scales it to proper size, uses Face API to detect human faces then mark them with a Paint and a Canvas. Notice all of these are Android functions, but the Face API library that it uses is 100% .NET.
4. For debugging and testing, I use the emulator that came with Xamarin Android, which you can choose to install when installing VS 2015. If you want to use an actual Android phone to debug, here is a tutorial.
You can download the source code (Android part) of my project here and check out a video demo here.