Xamarin.Android Publishing your Xamarin.Android APK Preparing your APK in the Visual Studio


Example

You finished your app, tested on debug mode and it is working perfect. Now, you want to prepare it to publish in the Google Play Store.

Xamarin documentation provides good informations in here:

https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_application/


Android Manifest

First, in Visual Studio, right-click your Xamarin.Android project in the Solution Explorer and select Properties. Then, go to the Android Manifest tab, to see this screen:

Android Manifest Configuration

Unlike in Android Studio or Eclipse, you don't need the set the AndroidManifest.xml file by writing; Xamarin and Visual Studio do that for you. Activities, BroadcastReceivers and Services are inserted into Android Manifest by declaring specific attributes in their classes.

In this screen, the options are:

  • Application name: This is the app name that will be visible for the user.
  • Package name: This is the package name. It must be unique, meaning that it must not use the same package name of other apps in the Google Play Store.
  • Application Icon: This is the icon that will be visible to the user, equivalent to the @drawable/ic_launcher used in Android Studio or Eclipse projects.
  • Version number: The version number is used by Google Play for version control. When you want to publish an APK for an updated version of your app, you must add 1 to this number for each new upgrade.
  • Version name: This is the version name that will be displayed to the user.
  • Install location: This determines where your APK will be installed, in the device storage or SD Card.
  • Required permissions: Here you determine which permissions are necessary for your app.

Android Options

In the screen below, you can configure the compiler options. Using the right options here can reduce a lot your APK size and also prevent errors.

Android Options Configuration

  • Configuration: Active (Release).
  • Platform: Active (Any CPU). These are necessary to build your APK for the Google Play Store. If the Configuration is set to Debug, it will not be accepted by Google Play.
  • Use Shared Runtime: false. If you set it to true, the APK will use Mono Runtime to execute. The Mono Runtime is installed automatically when debugging through USB, but not in the Release APK. If Mono Runtime is not installed in the device and this option is set to true in the Release APK, the app will crash.
  • Generate one package (.apk) per selected ABI: false. Create your APK for as many platforms as possible, for compatibility reasons.
  • Enable Multi-Dex: true, but you can set it to false if your app is not very complex (that is, has less than 65536 methods, see here).
  • Enable Proguard: true. This enables the Proguard tool that obfuscates Java code in your app. Note that it does not apply to .NET code; if you want to obfuscate .NET code, you must use Dotfuscator. More information on Proguard for Xamarin.Android can be found here.
  • Enable developer instrumentation (debugging and profiling): false for Release APK.
  • Linking: SDK and User Assemblies. This will make the Xamarin Linker to remove all unused classes from SDK and your code, reducing the APK size.

Important

Xamarin.Linker may sometimes remove classes that are not seemed to be used by your code, especially if they are in the project's Core (PCL library). To avoid that, you can either set the Linking to "Sdk Assemblies Only" or use the Preserve attribute in your classes, example:

PreserveAttribute.cs

namespace My_App_Core.Models
{
    public sealed class PreserveAttribute : System.Attribute
    {
        public bool AllMembers;
        public bool Conditional;
    }
}

In a class:

using System;

namespace My_App_Core.Models
{
    [Preserve(AllMembers = true)]
    public class ServiceException : Exception
    {
        public int errorCode;

        [Preserve(AllMembers = true)]
        public ServiceException() { }

        [Preserve(AllMembers = true)]
        public ServiceException(int errorCode)
        {
            this.errorCode = errorCode;
        }
    }
}
  • Supported architectures: Select all, for compatibility reasons.

After configuring everything, Rebuild the Project to make sure that it builds successfully.


Creating the APK for Release mode

You finished configuring your Android project for Release. The tutorial below shows how to generate the APK in Visual Studio. A full tutorial from Xamarin documentation can be found here:

https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/publishing_an_application/part_2_-_signing_the_android_application_package/

To create the APK file, right-click the Xamarin.Android project in the Solution Explorer and select Archive...

Solution Explorer -> right-click Xamarin.Android project -> Archive...

This will open the Archive manager and begin archiving the project, preparing to create the APK file.

Archiving project

When it finishes archiving the project, click in Distribute... to proceed.

Distribute...

The Distribute screen will present you two options: Ad-hoc and Google Play. The first will create an APK and save it in your computer. The second will directly publish the app in Google Play.

Choosing the first is recommended, so you can test the APK in other devices if you want.

Distribution Channels

In the following screen, an Android Key Store is needed to sign the APK. If you already have one, you can use it by clicking in Import...; if you don't, you can create a new Android Key Store by clicking in +.

Signing Identity

Creating a new Android Key Store screen:

Creating new Android Key Store

To create the APK, click in Save As. You may be prompted to type the Key Store password.

Save As

Save As -> Locate folder

Enter Key Store password

When it completes, you can click in Open Folder on the Archives screen to see your generated APK file.

Open Folder

APK in folder