Home Ask Login Register

Developers Planet

Your answer is one click away!

Marian Pa┼║dzioch February 2016

Add build machine signature to gradle generated variable

I'd like to stamp some variable generated from gradle (in my case it's User Agent used later with HTTP requests) to later be able to distinguish which developer build the app (for example if some developer made a mistake and his app is DDoSing the server).

So for now I can distinguish release from debug with:

buildTypes {
    debug {
        buildConfigField "String", "USER_AGENT", "\"Android-debug\""
    release {
        buildConfigField "String", "USER_AGENT", "\"Android-release\""

But for the debug I'd like to add something to know who built the app instance, it may be git login, machine name, or something else.


Doug Stevenson February 2016

A gradle build file is actually Groovy code, and you're free to put whatever you want in it. You just have to make sure that the code runs before it would be used in the DSL that describes the build. So if you want to grab something from the system, just write the Groovy code to do that. Groovy is a lot like Java, and you have the full JDK to work with at runtime, so it should be easy to get started.

If you want to access things about the build machine and environment, you might have to shell out to different commands in order to gather that data. Populate some variables with that data. Then use buildConfigField as you already are to drop those values into BuildConfig.java.

Bear in mind that you might want to provide some value in both debug and release so they both generate the same BuildConfig symbols. Otherwise your app might not compile in one config or the other.

BTW. You can tell the difference between debug and release with properties that are already added to BuildConfig, so you don't need to add anything more to tell the difference. Lines like these will always appear (look in the generated BuildConfig.java to see for yourself):

public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String BUILD_TYPE = "debug";

Post Status

Asked in February 2016
Viewed 1,965 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life