Create different build configuration to control runtime settings for different environments

In general enterprise mobile applications have more than one deployment scenarios, each of which requires the same code base, but different application settings.

The main three environments are development, stage, and production. You can read more info about environments here. We can define Conditional Compilation symbols, and load different application settings depending upon which build configuration is running.

Set up build configurations

Imagine that our project will name Tritan.

Go to Options → Configuration on solution Xamarin project:

screenshot

We will create configurations for Android, iPhone, and iPhoneSimulator platforms.

Create a new Build Configuration named DEV for Any CPU platform, and copy settings from the default Release configuration. Also set Create configuration for all solution items which create a config for all projects:

screenshot

Create the same DEV configuration for iPhone platform, and copy settings from the default Release iPhone configuration:

screenshot

Create the same DEV configuration for iPhoneSimulator platform, and copy settings from the default Release iPhoneSimulator configuration:

screenshot

Then repeat these steps to create STAGE configurations for all platforms:

screenshot

Set configuration mappings

Go to the next tab and set Configuration Mappings settings:

screenshot

Map config according to video.

If we build Tritan.Android we don’t need to build Tritan.iOS project. And vice versa, for Tritan.iOS we don’t need to build Tritan.Android project.

Edit config for Tritan.Android

Now we should remove iPhone and iPhoneSimulator configurations from the Tritan.Android project.

Go to Options → Configuration on Tritan.Android project:

screenshot

And remove above-mentioned config:

screenshot

Note!

Remove only for Android project (uncheck Delete also configuration in solution items):

screenshot

It should look like this:

screenshot

Edit config for Tritan.iOS

The same for Tritan.iOS project we need to remove Android configuration.

Go to Options → Configuration on Tritan.iOS project:

screenshot

And remove above-mentioned config:

screenshot

Note!

Remove only for Android project (uncheck Delete also configuration in solution items):

screenshot

It should look like this:

screenshot

Define conditional compilation symbols for each build configuration

Now we need to define Conditional Compilation symbols for each build configuration. Open the Project Properties window from the solution explorer on the Core (Tritan) project, and select the Compiler tab. In the dropdown list of available configurations, you will now see the default Debug and Release options, as well as your newly defined DEV and STAGE configurations. First, we will select the DEV configuration, and add a Conditional Compilation symbol in the space provided:

screenshot

The same for iPhone and iPhoneSimulator platforms:

Save config by pressing OK.

Add STAGE for Stage configuration for all platforms.

After that, we need to do the same for Tritan.iOS and Tritan.Android projects.

Android dev:

screenshot

iOS dev (for iPhone and iPhomeSimulator platforms):

screenshot

iOS stage (for iPhone and iPhomeSimulator platforms):

screenshot

Save config by pressing OK.

Define general application settings

Let`s try our configs for some environment settings in Constant class. Define for all environments different URL and select, for example, DEV iPhone configuration:

screenshot

And voila we can see that active config is DEV (all others are disabled). You may try to check another config.

References

  1. Visual Studio: Use Conditional Compilation to Control Runtime Settings for Different Deployment Scenarios
Written on June 14, 2019
comments powered by Disqus