Using Gradle to change your API's endpoint per build

Posted on 11 Aug 2014 by Eric Oestrich

Forgetting to change your Android application’s API endpoint when publishing to the Play Store is something I did at least three times when building SmartChat. They were all caught quickly and it was only an alpha app, so it wasn’t ever a big issue. However, this is something I would like to avoid.

I searched a bit and found that you could set BuildConfig variables per release type. This lets you never forget to change it again.

MyProject/build.gradle
android {
  buildTypes {
    debug {
      buildConfigField "String", "API_URL", "\"http://192.168.1.2:5000/\""
    }

    release {
      buildConfigField "String", "API_URL", "\"http://example.com/\""
    }
  }
}

With this setup you can access your API endpoint via BuildConfig.API_URL. This will be set at build time and you won’t accidentally forget to change it back when building for the Play Store.

I also found being able to set it different during development was handy so I ended up with this method in my API client.

public String getRootURL() {
  if (BuildConfig.DEBUG) {
    // Change me to whatever you want, this will only matter in development
    return BuildConfig.API_URL;
  }
  return BuildConfig.API_URL;
}
comments powered by Disqus
Creative Commons License
This site's content is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License unless otherwise specified. Code on this site is licensed under the MIT License unless otherwise specified.