We are currently working on a Xamarin Forms (.NET Core) application. We use Microsofts DevOps for the code repo, building and deployment.
Since we want to test newly developed features in a safe testing environment, we need to configure the app before building. At the bare minimum we need to swap out the backend uri (e.g. one for testing and one for production).

We thought about using pre-compiler flags, but we would like to keep the configuration away from the code if possible.

Is there an elegant solution or a best practice for handling that?

  • is your build creating the android/iphone package? are you using config files to store these variables or something else?
    – Ewan
    Aug 21, 2019 at 7:57
  • Yes the build creates the respective Android/iPhone/Uwp package. For now the variables are static strings in the code. Aug 21, 2019 at 7:59

1 Answer 1


It's been a while since I did Xamarin, but I believe the approach I used was to have a configuration file as an asset.

Your build steps can modify the configuration file as required pre build and package

Your code then parses the asset text and reads out the configuration values.

This isn't really any different from compiler macros though. Maybe a bit neater if you have large blocks of config.

Alternately, If you app logs on, you can have the login api return settings for the user. Although obviously you now have a hardcoded login url.

  • After a bit more research we came to the conclusion that your idea is probably the closest we could get to having proper environment variables. The only issue are the Pre-Compiler Flags, they are a bit cumbersome to handle, but better than everything else. So we based our approach on your idea, Thank you! Sep 26, 2019 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.