在Android上最好的保留api url之类的设置的地方 [英] On Android best place to keep settings like api url
问题描述
在我的android应用中,我需要保留api url的设置,该设置根据构建类型而有所不同(我将使用一个进行调试/暂存和发布).将此文件保存在strings
文件中感觉不对,因为此文件夹具有本地化的字符串,并且没有关于api url的本地化.
In my android app, I need to keep the settings for the api url, that varies based on the type of build (I will have one for debug/staging and release). Keeping this into the strings
file does not feel right since this folder has localized strings and there is nothing to localize about the api url.
保留此网址的最佳位置是什么?
What is the best place to keep this url in?
推荐答案
您可以使用BuildConfig
类,例如:
android {
buildTypes {
debug {
buildConfigField 'String', 'API_URL', 'http://...'
}
release {
buildConfigField 'String', 'API_URL', 'http://...'
}
}
}
或者,如果您不想将URL存储在build.gradle
中,则可以使用debug
和release
文件夹来创建用于存储URL的特殊类:
Or, if you don't want to store the URL's in build.gradle
, you can use the debug
and release
folders to create a special class which stores the URL:
-
debug\src\java\com\myapp\ApiParam.java
-
release\src\java\com\myapp\ApiParam.java
debug\src\java\com\myapp\ApiParam.java
release\src\java\com\myapp\ApiParam.java
现在,在您的main
项目中,您可以引用ApiParam
类.
Now, in your main
project, you can reference the ApiParam
class.
这篇关于在Android上最好的保留api url之类的设置的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!