Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

android - Custom fields for a build type in gradle

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...