简体   繁体   English

如何在 Xamarin.Forms (Android) 中读取 Microsoft App Center 环境变量?

[英]How to read Microsoft App Center Environment Variables in Xamarin.Forms (Android)?

I have an app deployed in appcenter.ms that has some "secret" fields.我在 appcenter.ms 中部署了一个应用程序,它有一些“秘密”字段。 I added them in the "Environment variables" section like MYSTRING_STR value and I'm trying to read it in a Xamarin.Forms project like var myStr = Environment.GetEnvironmentVariable("MYSTRING_STR");我将它们添加到“环境变量”部分,如MYSTRING_STR value ,我试图在 Xamarin.Forms 项目中读取它,如var myStr = Environment.GetEnvironmentVariable("MYSTRING_STR"); but no value is retured.但没有返回值。 What's the proper way to read them in a release build?在发布版本中阅读它们的正确方法是什么?

I was quite stupid.我很愚蠢。 Of course it's not going to work because the app doesn't run on the system it's compiled so the Environment Variables are not going to get copied.当然它不会工作,因为应用程序不在它编译的系统上运行,所以环境变量不会被复制。 I'm leaving this here for anyone who might need this in the future.我将把它留在这里给将来可能需要它的任何人。 Tl;Dr, I have my app on github and App Center just compiles from there. Tl;Dr,我在 github 上有我的应用程序,而 App Center 只是从那里编译。 This is how I managed to get "secret" fields on there without leaking information on github: You'll need the Mobile.BuildTools nuget package to be installed in your project.这就是我设法在不泄露 github 信息的情况下获得“秘密”字段的方法:您需要在项目中安装Mobile.BuildTools nuget 包。 Afterwards, you create a file called secrets.json in the root of your project with the information you need.然后,您在项目的根目录中创建一个名为secrets.json的文件,其中包含您需要的信息。 I created one locally with placeholder values like:我在本地创建了一个占位符值,例如:

{
  "String1": "%VALUE1%",
  "String2": "%VALUE2%"
}

Because it's a json, it won't read the values but it's ok.因为它是一个 json,所以它不会读取值,但没关系。 We only want it tracked on github for the next part.我们只希望在下一部分中在 github 上跟踪它。 After commiting it to the repo I add it to .gitignore and remove the cache for it so it won't get tracked when we put real values in it.将它提交到 repo 后,我将它添加到 .gitignore 并删除它的缓存,这样当我们将实际值放入其中时它就不会被跟踪。 When you hit compile, Mobile.BuildTools will automatically generate a class called Secrets .当您点击 compile 时,Mobile.BuildTools 将自动生成一个名为Secrets的类。 You can access the variables in code with Secrets.String1 etc, so you just use those where you need them.您可以使用Secrets.String1等访问代码中的变量,因此您只需在需要它们的地方使用它们。 After that, you set the Environment Variables as usual in App Center, then you'll need a build script to write them to the json file.之后,您像往常一样在 App Center 中设置环境变量,然后您需要一个构建脚本将它们写入 json 文件。 In the folder <MyProject>.Android (or iOS, or both if you're supporting multiple platforms), I created the script called appcenter-post-clone.sh with the following content:在文件夹<MyProject>.Android (或 iOS,或两者,如果您支持多个平台)中,我创建了名为appcenter-post-clone.sh的脚本, appcenter-post-clone.sh包含以下内容:

#!/usr/bin/env bash

npm install node-jq --save

jq -n \
            --arg str1 "$STRING1" \
            --arg str2 "$STRING2" \
            '{String1: $str1, String2: $str2}' > $APPCENTER_SOURCE_DIRECTORY/<Project>/secrets.json

After that you're done.之后你就完成了。 Sorry for the long post but I couldn't really find any source explaining this so I'm just leaving it here in case anyone else is as lost as I was.很抱歉这篇很长的帖子,但我真的找不到任何解释这一点的来源,所以我只是把它留在这里,以防其他人和我一样迷路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM