简体   繁体   English

在原生 iOS 和 Android 中获取 --dart-define 环境变量

[英]Get --dart-define environment variables in native iOS and Android

I am trying to find a way to access the environment variables provided by --dart-define inside native iOS and Android code.我正在尝试找到一种方法来访问本机 iOS 和 Android 代码中--dart-define提供的环境变量。

Is there any way to do this?有没有办法做到这一点?

I have tried the guide explained in https://binary-studio.com/2020/06/23/flutter-3/ but that doesn't work as what gets written to the generated .xcconfig is not separated by = but instead by %3D .我已经尝试过https://binary-studio.com/2020/06/23/flutter-3/中解释的指南,但这不起作用,因为写入生成的.xcconfig的内容不是由=分隔,而是由%3D And I get the error我得到了错误

error: .../ios/Flutter/DEFINEEnvironment.xcconfig:2: expected a ‘=’, but found % (in target 'Runner' from project 'Runner')

The file DEFINEEnvironment.xcconfig gets generated with the following content MY_VAR%3DMY_VALUE instead of MY_VAR=MY_VALUE使用以下内容生成文件DEFINEEnvironment.xcconfig MY_VAR%3DMY_VALUE而不是MY_VAR=MY_VALUE

UPDATE更新

This seems to be happening because when reading from the args passed to --dart-define it must be encoding = into %3D .这似乎正在发生,因为从传递给--dart-define的 args 读取时,它必须将 encoding =转换为%3D And I don't know how prevent that from happening而且我不知道如何防止这种情况发生

It seems that the script has been changed and now includes the necessary code to decode the strings.脚本似乎已更改,现在包含解码字符串的必要代码。

The post-action script that needs to be added to Scheme is:需要添加到 Scheme 的 post-action 脚本是:

function urldecode() { : "${*//+/ }"; echo "${_//%/\\x}"; }

IFS=',' read -r -a define_items <<< "$DART_DEFINES"


for index in "${!define_items[@]}"
do
    define_items[$index]=$(urldecode "${define_items[$index]}");
done

printf "%s\n" "${define_items[@]}" > ${SRCROOT}/Flutter/DEFINEEnvironment.xcconfig

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

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