gpt4 book ai didi

android - Android 项目中的环境变量

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

有没有办法在 Android 项目中设置环境变量?例如,如果您想在开发和生产环境中将您的应用程序与不同的网络服务器一起使用,那么您可以在哪里放置不同的 URL?

最佳答案

For example, in a situation where you want to use your app with different webservers in development and production, where could you place the different URLs for this?



如果您使用的是 Android Studio,则可以通过配置构建类型来处理。

在您的 android关闭,你可以有一个 buildTypes定义要添加到 BuildConfig 的常量的闭包类(class):
buildTypes {
debug {
buildConfigField "String", "SERVER_URL", '"http://test.this-is-so-fake.com"'
}

release {
buildConfigField "String", "SERVER_URL", '"http://prod.this-is-so-fake.com"'
}
}

现在,在我的 Java 代码中,我可以引用 BuildConfig.SERVER_URL .一个 debug build 会给我调试 URL;一个 release build 会给我发布 URL。您也可以为更复杂的场景创建自己的自定义构建类型。

关于android - Android 项目中的环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28787422/

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