Android的工作室:切换取决于buildtype一个网址? (用于测试调试/发布) [英] Android Studio: Switching a URL depending on buildtype? (used for testing in debug /release)
问题描述
我一直在阅读一些有关的变体和buildtypes,我不知道如果我理解正确的,但我想存储的URL locahost(测试),一个用于生产(在互联网上活动站点)。
I have been reading something about variants and buildtypes and I don't know if I am understanding it right but I would like to store a URL for locahost (testing) and one for production (live site on the internet).
和我需要的,这取决于buildtype切换他们。这是正确的方式做到这一点?或者是有其他选择吗?
And I need to switch them depending on which buildtype. Is this the right way to do this ? or is there another alternative ?
有没有人有一个小例子?
Does anyone have a small example ?
有没有存储在我并不需要承诺到源代码管理的文件信息的一种方式?我现在用的是gradle.properties文件来存储一些密码的摇篮的引援。这伟大的工程,因为这文件,我没有在版本控制共享使用。
Is there a way of storing this information in a file that I do not need to commit to source control ? I am using the gradle.properties file for storing some passwords that gradle uses for the signings.. This works great as this file I do not share in version control.
我有点糊涂了正确的方法使用和如何实现它。
I am a little confused of the correct method to use and how to implement it.
任何想法?
推荐答案
您可以使用BuildConfig为每个BuildType提供不同的网址
You can use the BuildConfig for supplying different URLs for each BuildType
buildTypes {
debug {
buildConfigField "String", "SERVER_URL", '"http://someurl/"'
}
release{
buildConfigField "String", "SERVER_URL", '"http://someotherurl/"'
}
}
该BuildConfig将在每次同步的摇篮文件中的项目时,自动生成。 在您的code,可以访问网址是这样的:
The BuildConfig will be autogenerated each time you sync your project with the gradle file. In your code, you can access the URL like this:
BuildConfig.SERVER_URL
如果你不希望提交这些网址,你可以将它们在你的gradle.properties存储就像你的密码,这样并引用他们在build.gradle。
If you don't want to commit these URLs, you can store them in your gradle.properties just like your password and such and reference them in the build.gradle.
buildConfigField "String", "SERVER_URL", serverurl.debug
这篇关于Android的工作室:切换取决于buildtype一个网址? (用于测试调试/发布)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!