Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set constant value with gradle

Tags:

android

gradle

I just moved to the android gradle build system and it is not clear for me how to set constant values.

e.g. I have

API.java which contains

BASE_URL = "http://debug.server.com"

but when gradle builds release I need BASE_URL to be "http://release.server.com"

How to replace this value at the build time?

like image 769
wnc_21 Avatar asked Oct 20 '14 10:10

wnc_21


People also ask

How do I declare a variable in Gradle?

Local variables are declared with the def keyword. They are only visible in the scope where they have been declared. Local variables are a feature of the underlying Groovy language. Local variables are declared with the val keyword.

What is projectDir in Gradle?

description String The description of the project. projectDir File The directory containing the build script. The value is read-only. buildDir File The directory with the build name in the directory, containing the build script. rootDir File The directory of the project at the root of a project structure.

What is Buildscript in Gradle?

Gradle builds a script file for handling two things; one is projects and other is tasks. Every Gradle build represents one or more projects. A project represents a library JAR or a web application or it might represent a ZIP that is assembled from the JARs produced by other projects.

What is def in Gradle?

Keyword def comes from Groovy and means that variable has local scope. Using ext. outDir means that you add property outDir to ExtraPropertiesExtension, think of it like project has a map of properties with name ext , and you put your property in this map for later access.


2 Answers

Gradle generates a class called BuildConfig which contains static members (such as the boolean DEBUG, which is set to true for debug variants).

You could either query this in java like so:

if (BuildConfig.DEBUG) {
    BASE_URL = "http://debug.server.com"
} else {
    BASE_URL = "http://release.server.com"
}

or the same thing as a one-liner:

BASE_URL = BuildConfig.DEBUG ? "http://debug.server.com" : "http://release.server.com"

OR

You could actually set the BASE_URL inside the the BuildConfig class using gradle like so:

android {
    buildTypes {
        debug {
            buildConfigField "String", "BASE_URL", '"http://debug.server.com"'
        }

        release {
            buildConfigField "String", "BASE_URL", '"http://release.server.com"'
        }
    }
}

Note the single and double quotations around the value in gradle, as others have mentioned in the comments. This way, the double quotes become part of the value.

As a result, the static reference BuildConfig.BASE_URL would point to the corresponding URL (i.e. "debug.server.com" for debug, "release.server.com" for release)

like image 69
Fifer Sheep Avatar answered Sep 23 '22 12:09

Fifer Sheep


Answer of fifer-sheep is correct. Just wanted to leave a solution for more than two environments.

Two flavors for staging and live.

productFlavors {
    staging {
        ...
    }
    production {
        ...
    }
}

Whole app config relies on the current ENV. Using:

public static String ENV_DEVELOPMENT = "development";
public static String ENV_STAGING = "staging";
public static String ENV_PRODUCTION = "production";

ENV = BuildConfig.DEBUG ? ENV_DEVELOPMENT : BuildConfig.FLAVOR;

I can switch between all different ENVs while testing locally but force staging/live settings if released.

like image 25
everyman Avatar answered Sep 21 '22 12:09

everyman