有没有用在Java类中的变量的gradle方式 [英] is there a way to use a gradle variable in Java class

查看:243
本文介绍了有没有用在Java类中的变量的gradle方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个团队,对使用一个RESTful API的Andr​​oid应用工作的一部分(使用的改造 1.9),我们正在寻找各种方法来建立一个配置文件,这样我们就不必从Live服务器的连接字符串(API URL)来改变我们自己的开发环境中,我们要添加或更改应用程序的东西任何时候。

I am part of a team that works on an Android app that uses a RESTful api(using retrofit 1.9), and we are looking for ways to set up a config file, so that we wouldn't have to change the connection string(API URL) from the live server to our own dev environment, any time that we want to add or change something in the app.

有没有办法使用摇篮本的方法吗?换句话说,有没有为我们创造的Gradle一个配置,在此我们声明一个变量并使用该变量的项目(API URL)?一个办法

Is there a way to use Gradle for this? in other words, is there a way for us to create a gradle config, in which we declare a variable and use that variable in the project(the API URL)?

还有什么其他选择呢,我们有?

what other options do we have?

正如一个方面说明,这是我们正在做的第一个Android项目,所以我很抱歉,前期这是不是一个很好的问题(上改善任何暗示将AP preciated)。
先谢谢了。

Just as a side note, This is the first Android project that we are doing, So I apologize upfront if this is not a very good question(any hints on improvement would be appreciated). Thanks in advance.

推荐答案

当然,则可以使用生成类型为添加 buildConfigField ,这将然后在 BuildConfig可用。[VARIABLENAME]

Sure, you can use the build types for that to add a buildConfigField, which will then be available in BuildConfig.[variableName].

buildTypes {
    debug {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "API_URL", "[apiURL]"
        buildConfigField "Boolean", "DEBUG_MODE", "true"
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        buildConfigField "String", "API_URL", "[apiURL]"
        buildConfigField "Boolean", "DEBUG_MODE", "false"
        signingConfig signingConfigs.config
    }

这篇关于有没有用在Java类中的变量的gradle方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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