Forum Xamarin.Forms

Using environment variables from app center in the appliaction

gvuksagvuksa RSMember ✭✭
edited April 2018 in Xamarin.Forms

Hi all,

I need to use different api endpoints based on if the app is for testing or production. I am using app center and have configured environment variables there, with required values.

The question is, how can I assign the value of variable to be used in the application?

Best Answer

Answers

  • ionixjuniorionixjunior USMember ✭✭✭

    Ah, and if you have a new idea for custom build script, share for us in this open source project :smile:

  • gvuksagvuksa RSMember ✭✭
    edited April 2018

    Hi @ionixjunior, this works for shared projects, but I can't make it work with .net standard library.

    I have a bash script in the root of android project with namespace "Root.Droid"

    I have a configuration class called Configuration.cs in the .net standard library with default namespace "Root" and in the Helpers folder of that library. So, the namespace of Configuration.cs is Root.Helpers. I have also tried adding helpers folder with Configuration.cs file in the Android project.

    #!/usr/bin/env bash
    
    if [ ! -n "$API_URL" ]
    then
        echo "You need define the API_URL variable in App Center"
        exit
    fi
    
    APP_CONSTANT_FILE=$APPCENTER_SOURCE_DIRECTORY/Helpers/Configuration.cs
    
    echo "$APP_CONSTANT_FILE"
    
    if [ -e "$APP_CONSTANT_FILE" ]
    then
        echo "Updating ApiUrl to $API_URL in Configuration.cs"
        sed -i '' 's#ApiUrl = "[a-z:./]*"#ApiUrl = "'$API_URL'"#' $APP_CONSTANT_FILE
    
        echo "File content:"
        cat $APP_CONSTANT_FILE
    else
        echo "$APP_CONSTANT_FILE is not found"
    fi
    

    I have made this small change to the bash script to echo "$APP_CONSTANT_FILE is not found", and it always echoes this. Do you know how can I point to the Configuration.cs file in this case?

  • gvuksagvuksa RSMember ✭✭

    Sorry, if someone needs I have resolved this. There are three folders before the .net standard library from the solution url. So proper app constant file is:

    APP_CONSTANT_FILE=$APPCENTER_SOURCE_DIRECTORY/Root/Root/Root/Helpers/Configuration.cs
    

    where "Root" is Root namespace of your project / folder structure from .sln file.

  • ionixjuniorionixjunior USMember ✭✭✭

    Nice! :smile:

  • martinfletcherw2gdmartinfletcherw2gd Member
    edited September 2018

    You can use the Microsoft.Extensions.Configuration framework for this - That way you don't need to rebuild:

    IFileProvider configurationFileProvider = new PlatformSpecificFileProvider();
    var configurationBuilder = new ConfigurationBuilder();
    
    // General config
    configurationBuilder.AddJsonFile(configurationFileProvider, "appsettings.json", false, false);
    
    var configuration = configurationBuilder.Build();
    
    // Environment config
    var environment = configuration["Environment"];
    
    configurationBuilder.AddJsonFile(configurationFileProvider, $"appsettings.{environment}.json", true, false);
    
    configuration = configurationBuilder.Build();
    
  • NirajBhattNirajBhatt Member ✭✭
    edited October 2018

    Hi @martinfletcherw2gd
    Just need some clarification. is it necessary to add configuration files? I just want to read custom environment variable that's passed from Build settings of App in App Center. I don't want to use any custom build script.

    I am trying following code but it's not working.
    EnvironmentVariablesConfigurationProvider env = new EnvironmentVariablesConfigurationProvider();
    env.TryGet("AppClientID", out string appclientId);

    here appClientId is null.

    could you please help?

  • gvuksagvuksa RSMember ✭✭

    Hi @NirajBhatt

    As far as I know you use EnvironmentVariablesConfigurationProvider only in asp.net Core.

    Anyway, environment variables should be extracted before the build process, I don't think that you can access them after the build if you haven't added their values to one of your project files.

    App center documentation only mentions build scripts, xml files (with example to connect to private NuGet feed), and build gradle for Android.
    https://docs.microsoft.com/en-us/appcenter/build/custom/variables/

    I haven't had a chance to try Microsoft.Extensions.Configuration as mentioned by @martinfletcherw2gd , but simply updating variables using bash as provided in the accepted answer works great. Just pay attention that the regex part in sed is set properly for specific variable that you need.

  • NirajBhattNirajBhatt Member ✭✭

    Sorry for late reply @gvuksa ,
    Thanks for information. I ended up using Mobile.BuildTools (https://github.com/dansiegel/Mobile.BuildTools)

Sign In or Register to comment.