安卓:管理的开发和发布不同的服务器URL [英] Android: Managing different server Url for development and release

查看:249
本文介绍了安卓:管理的开发和发布不同的服务器URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,通过的REST API服务器进行交互。很显然,我需要使用不同的URL进行开发和发布版本。注释和取消注释code是非常繁琐和错误代词。

I am developing an Android application that interacts with server via REST APIs. Obviously I need to use different URL for development and release builds. Commenting and un-commenting code is very tedious and error pron.

这是处理这种情况的最好方法是什么?在摇篮文件中使用不同的构建类型是它可以自动的过程,但我不知道这是否是正确的道路要走。

Which is the best way to handle this situation? Using different build types in gradle file is one which could automate the process, but I am not sure if this is the right way to go.

还有增加构建类型即数量的可能性。测试,内部释放等。

There is also a possibility of increase in number of build types viz. test, internal-release etc.

推荐答案

如果您使用的是Android的工作室,使用 buildConfigField 自定义字段添加到您的 BuildConfig 类。

If you are using Android Studio, use buildConfigField to add custom fields to your 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"'
        }

        mezzanine.initWith(buildTypes.release)

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

在这里,我有三个版本类型:标准调试发布,再加上自定义夹层之一。每个定义了一个 SERVER_URL 字段 BuildConfig

Here, I have three build types: the standard debug and release, plus a custom mezzanine one. Each defines a SERVER_URL field on BuildConfig.

然后,在Java中code,你只要参照 BuildConfig.SERVER_URL 。根据你使用的是什么版本的类型来构建应用程序的特定版本的该领域将有一个值。

Then, in Java code, you just refer to BuildConfig.SERVER_URL. That field will have a value based on what build type you used to build that particular edition of the app.

这篇关于安卓:管理的开发和发布不同的服务器URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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