Using String Constants Generated by Gradle Build Configurations

September 23, 2014

If you’ve ever wanted to use different String constants in your Android project for debug and release builds, there is a way.

Gradle allows developers to insert different constants into their Java code and XML resources with different build configurations.

Google Maps API V2 Example

The Google Maps API V2 requires that your Maps API key be placed in your AndroidManifest.xml file, like this:

<meta-data
 android:name="com.google.android.maps.v2.API_KEY"
 android:value="(YOUR MAPS API KEY)"/>

Since this is in the manifest, you don’t have the option to change this API key at runtime. If you wanted to use a different Maps API key for your development and production build versions, you would have to change this entry in the manifest each time you built a release version of your app.

With Gradle however, you can specify different XML string resources for your debug and release builds. In this case, we can make a string resource for our Maps API key in our build.gradle file:

android {

 ...

 buildTypes {
 debug {
 ...

 resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", "(your development Maps API key)"
 }
 release {
 ...

 resValue "string", "GOOGLE_MAPS_ANDROID_API_KEY", "(your production Maps API key)"
 }
 }
}

The string resource “GOOGLE_MAPS_ANDROID_API_KEY” will be generated during a debug or release build. Now you can refer to this string in your manifest:

<meta-data
 android:name="com.google.android.maps.v2.API_KEY"
 android:value="@string/GOOGLE_MAPS_ANDROID_API_KEY"/>

Your IDE may complain about this string resource not existing, but it will build just fine.

String Constants in Your Java Code

Gradle can also insert String constants into your Java code. Let’s say we want to use different URL endpoints depending on whether this is our debug or release version. This can be done in your build.gradle file.

android {

 ...

 buildTypes {
 debug {
 ...

 buildConfigField "String", "URL_ENDPOINT", "\"http://your.development.endpoint.com/\""
 }
 release {
 ...

 buildConfigField "String", "URL_ENDPOINT", "\"http://your.production.endpoint.com/\""
 }
 }
}

The escaped double-quotes (\”) around the string itself are necessary in order for the String constants to have the double-quotes in the generated code.

Now we can refer to this String constant in our code.

import com.yourpackage.name.BuildConfig;

private void doSomethingWithUrlEndpoint() {
 String urlEndpoint = BuildConfig.URL_ENDPOINT;

 ...
}
Joseph Kreiser
Joseph Kreiser
Software Developer

Looking for more like this?

Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.

Simplifying the 4 forces of AI
Business

Simplifying the 4 forces of AI

April 9, 2024

Artificial Intelligence (AI) is becoming more prevalent, but less understood. Through examples of organizations leading the charge in each of these areas, I hope to empower you to make better decisions for your enterprise and career.

Read more
To RFP or not to RFP?
Business Process

To RFP or not to RFP?

January 19, 2024

Selecting the right digital product partner is not just about ticking boxes in a request for proposal (RFP). It’s more important to engage in a meaningful discovery process to find a collaborator who can turn your vision into a successful digital reality. Here are three things to watch out for as you search for the perfect digital collaborator.

Read more
Business Model Canvas: Helping business leaders create meaningful digital products
Business Process

Business Model Canvas: Helping business leaders create meaningful digital products

January 17, 2024

When it comes to custom software, it’s easy to feel stuck between needing to deliver value quickly, while also ensuring meaningful, long-lasting results. Learn why the Business Model Canvas can help you identify the right digital product for your business needs.

Read more
View more articles