Android Studio:根据构建类型切换 URL?(用于调试/发布中的测试) [英] Android Studio: Switching a URL depending on buildtype? (used for testing in debug /release)

查看:27
本文介绍了Android Studio:根据构建类型切换 URL?(用于调试/发布中的测试)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读有关变体和构建类型的内容,我不知道我是否理解正确,但我想存储一个用于 locahost(测试)和一个用于生产(互联网上的实时站点)的 URL.

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).

我需要根据构建类型来切换它们.这是正确的方法吗?还是有另一种选择?

And I need to switch them depending on which buildtype. Is this the right way to do this ? or is there another alternative ?

有人举个小例子吗?

有没有办法将这些信息存储在我不需要提交到源代码控制的文件中?我正在使用 gradle.properties 文件来存储一些 gradle 用于签名的密码.这很好用,因为我不在版本控制中共享这个文件.

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 提供不同的 URL

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/"'
    }
}   

每次将项目与 gradle 文件同步时,都会自动生成 BuildConfig.在您的代码中,您可以像这样访问 URL:

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

如果您不想提交这些 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 Studio:根据构建类型切换 URL?(用于调试/发布中的测试)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆