10

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

dlucci
  • 729
  • 4
  • 9
  • 24

2 Answers2

37

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Would this also work within the build file as a property extension (like it can be done for product flavors) ? So I could use the buildConfigField "SERVER_URL" in my build script? – AgentKnopf Feb 02 '15 at 10:33
  • @Zainodis: Sorry, but I do not recognize the term "property extension". I have not looked into using `buildConfigField` in a product flavor definition, so I don't know if that's a supported option. – CommonsWare Feb 02 '15 at 12:09
  • 2
    the basic idea of those extensions is, to give flavors additional properties you use during the build, like versionName etc. but custom. I tried using the code you posted and then use them in the gradle build, rather than inside my app code, but the properties like SERVER_URL were not recognized in the gradle build file when accessing them as buildTypes.SERVER_URL so I am guess this is not an option for in-build properties? – AgentKnopf Feb 02 '15 at 12:12
  • @Zainodis: Again, I have not researched this area, sorry. – CommonsWare Feb 02 '15 at 12:45
  • but when you reverse engineer your apk you can see SEVER_URL in java code via jd-gui, whats the better way to prevent from decompiling?? – Sumit Aug 07 '17 at 10:42
  • 1
    @Sumit: Don't write an Android app. In fact, since users of Web browsers can see server URLs, do not write Web apps either. And don't write desktop software, as it suffers from the same problem as do Android apps. Otherwise, by the nature of how HTTP/HTTPS works, it is not difficult for somebody to find out what server you are connecting to. – CommonsWare Aug 07 '17 at 10:45
5

To just use a field independently from build types and product flavors, you could add it to your defaultConfig by:

defaultConfig {
    ...
    buildConfigField "String", "OS", '"android"'
}

Then the BuildConfig looks like this:

public final class BuildConfig {
    public static final boolean DEBUG = Boolean.parseBoolean("true");
    public static final String APPLICATION_ID = "com.example.app";
    public static final String BUILD_TYPE = "debug";
    public static final String FLAVOR = "";
    public static final int VERSION_CODE = 1;
    public static final String VERSION_NAME = "1.0";
    // Fields from default config.
    public static final String OS = "android";

}

Graham
  • 7,431
  • 18
  • 59
  • 84
mbo
  • 4,611
  • 2
  • 34
  • 54