gpt4 book ai didi

android - 在 gradle 中读取系统环境变量

转载 作者:行者123 更新时间:2023-12-04 23:49:04 47 4
gpt4 key购买 nike

我有点好奇我们如何在 Android build.gradle 中从系统环境变量 (Windows/Mac/Linux) 加载私有(private)凭据或任何其他 gradle 配置文件。
可能有一些方法或方法可以实现这一目标,但实际上是在寻找方向。

Couple of things I already explored but didn't work for me are:

Note: I tried on Windows Environment and getting null.

In Gradle, is there a better way to get Environment Variables?

https://javabydeveloper.com/gradle-system-properties-via-command-line-step-by-step-example/

How to give System property to my test via Gradle and -D

https://dzone.com/articles/specifying-gradle-build

https://discuss.gradle.org/t/how-should-i-be-handling-passing-system-properties-from-gradle-to-my-tests/7171/3

最佳答案

您可以访问一个名为 MY_VAR 的变量。使用 System.getenv('MY_VAR')在 Gradle 脚本中。如果变量不存在,则 null被退回。这应该在所有平台上同样有效。
例如,使用 build.gradle脚本

def myVar = System.getenv('MY_VAR')
println(myVar)
Gradle 命令 MY_VAR='it works!' ./gradlew projects在控制台上输出以下(以及更多)(在带有 Gradle 7.2 的 Linux shell 中测试):
> Configure project :
it works!

关于android - 在 gradle 中读取系统环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69566928/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com