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:
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:
Create the same DEV configuration for iPhone platform, and copy settings from the default Release | iPhone configuration: |
Create the same DEV configuration for iPhoneSimulator platform, and copy settings from the default Release | iPhoneSimulator configuration: |
Then repeat these steps to create STAGE configurations for all platforms:
Set configuration mappings
Go to the next tab and set Configuration Mappings settings:
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:
And remove above-mentioned config:
Note!
Remove only for Android project (uncheck Delete also configuration in solution items
):
It should look like this:
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:
And remove above-mentioned config:
Note!
Remove only for Android project (uncheck Delete also configuration in solution items
):
It should look like this:
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:
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:
iOS dev (for iPhone and iPhomeSimulator platforms):
iOS stage (for iPhone and iPhomeSimulator platforms):
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: |
And voila we can see that active config is DEV (all others are disabled). You may try to check another config.