Apps built with ArcGIS Runtime SDK for .NET require an ArcGIS Runtime deployment. The ArcGIS Runtime deployment contains the minimum set of ArcGIS Runtime components required to support the functionality in your app, from rendering beautiful maps to performing complex GIS analysis. Likewise, excluding unneeded components produces focused apps with a smaller footprint, containing bits only for the capabilities you need.
Define ArcGIS Runtime components to deploy
An ArcGIS Runtime deployment is created for you automatically when you build your project. The Xamarin.Android build process puts everything together and generates an .apk for execution on Android devices. Learn more about the Android build process in the Microsoft docs.
You will need an ArcGIS Runtime deployment for every target platform for your app. The ArcGIS Runtime NuGet packages will automatically detect the build target for the project and build an ArcGIS Runtime with the appropriate architecture.
Deploying specialized symbols
You may need to deploy specialized symbols, such as military symbol dictionaries, for use with your ArcGIS Runtime SDK app. In ArcGIS Runtime SDK for .NET versions prior to 100.0, these symbols were included in the SDK install and could simply be copied to a deployment folder for your app. To reduce the size of the setup (especially with the increased number of supported platforms), these symbols are not included in the ArcGIS Runtime SDK for .NET NuGet packages.
You have a couple of options for including these symbols in your deployment.
- The ArcGIS for Defense group hosts the following ArcGIS Online items that contain .stylx files with military symbols. Use the support matrix to find and download the supported stylx file for your version of ArcGIS Runtime SDK. Once downloaded, you can include the files in your app deployment. Logic in your app, of course, would be required to locate and use the included files as needed.
- Add logic to your app that prepares the app for offline use by downloading the files and storing them locally. You can access these resources when your user is online either by programmatically downloading them (using System.Net.Http.HttpClient, for example) or accessing them as portal items using the ArcGIS Runtime API, reading their contents, and storing them locally. The following example shows how to access some of the hosted military symbol items using ArcGIS Runtime code.
// Get the ArcGIS Online portal (default if no URI is provided) ArcGISPortal agol = await ArcGISPortal.CreateAsync(); // Get the military symbol items (use their unique item IDs) PortalItem itemMil2525c = await PortalItem.CreateAsync(agol, "ef95f95470db437f80ea764a9d05203b"); PortalItem itemMilStd2525d = await PortalItem.CreateAsync(agol, "c78b149a1d52414682c86a5feeb13d30"); // Read the data from the items var dataMil2525c = await itemMil2525c.GetDataAsync(); var dataMilStd2525d = await itemMilStd2525d.GetDataAsync(); // ... Code here to read the data and write it to a local file ...
Reducing your APK size
An Android Package (APK) is a compressed package file format used by the Android operating system to install applications on mobile devices. The internal packaged contents of the APK file can be viewed in a zip/unzip compression application like: WinZip, 7-Zip, WinRAR, etc. As per Google, to deploy a custom APK file on the Google Play Store, the APK must remain below 100 MB in size. Keeping a custom APK below 100 MB requires a few considerations when developing an ArcGIS Runtime application via Visual Studio with Xamarin.
First, Android supports a few different instruction set architectures. It is possible to bundle multiple platform architectures into a single APK. The more architectures added to the APK, the larger the APK will be. It is recommended to limit each APK to a single architecture.
The Android architectures available in Visual Studio with Xamarin are: armeabi, armeabi-v7a, x86, x86_64, and arm64-v8a. To change which Android architectures are available when creating a deployable APK in Visual Studio, click the Project > [ProjectName] Properties… menus to open the project properties tab. Then click on the Android Options in the side bar, scroll towards the bottom and click the Advanced button to open the Advanced Android Options dialog.
In the Advanced Android Options dialog, click the Supported architectures dropdown and choose which architecture(s) will be generated for the APK. Again it is recommended to choose only one architecture per APK to limit its size.
Second, whenever functionality is added to an application, the APK size increases because of the dependent assemblies that are required for that functionality. Therefore creating an application that consumes the ArcGIS Runtime binaries (ex: Esri.ArcGISRuntime.dll, Esri.ArcGISRuntime.Hydrography.dll, Esri.ArcGISRuntime.Xamarin.Forms.dll, and others) will increase the size of the APK. Plus there could be assemblies from other 3rd party binaries that increase the APK size. For example creating a simple mapping application that uses the MapView control to display a base map layer (which requires adding the Esri.ArcGISRuntime.dll) to create an Android APK could result in exceeding the 100 MB size limit for the Google Play Store.
Fortunately, the Microsoft Visual Studio with Xamarin compiler has options to decrease the size of the APK based upon the actual functionality that is used from a particular assembly linking. Linking allows the Visual Studio with Xamarin compiler to shrink the size of the dependency binaries placed in the APK to only those pieces of functionality that are needed. There are several options that can be chosen when compiling APK that are well documented by Microsoft in the Linking on Android document. In tests performed by the ArcGIS Runtime for .NET team, excellent success can be achieved in minimizing the size of the APK by choosing the 'Sdk Assemblies Only' option in the Linking section of the Project properties pane.
Other APK considerations
- When defining when architecture to use for creating Android APKs, it should be noted that x86 and x86_64 versions should not be used for Google Play Store deployments. These architectures are for testing and debugging the device emulators on the PC.
- Google has specific recommendations for creating APKs in the Android Developers Blog.
- To deploy an APK for use on the Google Play Store or to side-load onto a device/emulator, right click on the project name in the Visual Studio Solution Explorer pane and choose Archive… This will automatically start process of generating an APK file.
- Remember to use the Release build option to create smaller binaries. The Debug build option will be much larger and have slower execution time.
Here is a tip on how to side-load an APK onto a Windows device emulator:
-- Start the emulator of choice by using the Visual Studio menus of: Tools > Android > Android Device Manager…
-- Open a ‘Developer Command from for Visual Studio’ and navigate to where your Android SDK platform tools are installed (ex: C:\Program Files (x86)\Andorid\android-sdk\platform-tools).
-- Type ‘adb devices’ to connect to the running emulator. You should see ‘List of devices attached’ and ‘emulator-XXXX device’, where XXXX is the device number that is running.
-- Type ‘adb install “[PathAndFileNameOfTheAPK]”’ to side-load the APK onto the emulator. For example “[PathAndFileNameOfTheAPK]” could be something like “C:\MyApps\MyAndroidMapApp\release\MyAndroidMapApp-Signed.apk”.