Gradle中的构建类型的自定义字段 [英] Custom fields for a build type in gradle

查看:107
本文介绍了Gradle中的构建类型的自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在build.gradle文件中嵌入几个服务器地址,但我不确定如何执行此操作。我知道,在maven中,你可以写成:

 < content.host> http://test.mysite.com< / content.host> 

我可以在我的Android应用程序中使用content.host。在Gradle中,我知道你可以通过创建一个构建类型。

$ p $ build $ {
testBuild.initWith(buildTypes.debug)
testBuild {/ *测试服务器信息在这里* /}
}

但我不确定如何使用相同的方法在gradle中定义content.host。有没有另外一种方法来定义gradle中的content.host,还是有一种方法可以将自定义属性添加到buildTypes中?



干杯,

Derril

解决Gradle for Android提供 buildConfigField ,允许您将任意数据成员添加到代码生成的 BuildConfig

  buildTypes {
debug {
buildConfigFieldString,SERVER_URL, 'http://test.this-is-so-fake.com'
}

发布{
buildConfigFieldString,SERVER_URL,'http ://prod.this-is-so-fake.com'
}

mezzanine.initWith(buildTypes.release)

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

在你的Java代码中,你可以引用 BuildConfig.SERVER_URL ,它将被填充该字符串基于您在编译时选择的构建类型。

I am trying to embed a few server addresses in my build.gradle file but I am unsure about how to do this. I know that in maven, you can write

      <content.host>http://test.mysite.com</content.host>

and I can use content.host in my Android application. In gradle, I know you can create a build type by

      buildTypes {
        testBuild.initWith(buildTypes.debug)
        testBuild{ /*test server info goes here*/ }
      }

But I'm not sure how I can define content.host in gradle using that same method. Is there another way to define content.host in gradle or is there a way to add a custom property to buildTypes?

Cheers,

Derril

解决方案

Gradle for Android offers buildConfigField, allowing you to add arbitrary data members to the code-generated 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"'
    }
}

In your Java code, you can refer to BuildConfig.SERVER_URL, and it will be populated with the string based on the build type you choose at compile time.

这篇关于Gradle中的构建类型的自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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