Last active
April 5, 2017 05:31
-
-
Save sideffect0/c87983ff83c224f7338593f42bcefe7f to your computer and use it in GitHub Desktop.
load gradle build variables or properties from properties file or system environment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply from: "envutils.gradle" | |
// loading to data map | |
def data = loadFromEnvOrProp() | |
buildTypes { | |
release { | |
minifyEnabled false | |
buildConfigField "String", "API_URL", data["api.url"] | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Environment { | |
def data | |
def getProperty(String name){ | |
if(data[name]){ | |
return data[name] | |
} | |
return System.getenv(name) | |
} | |
} | |
ext.loadFromEnvOrProp = { prop_file -> | |
def data = [:] | |
if(prop_file == null){ | |
prop_file = "gradle.properties" | |
} | |
if (project.rootProject.file(prop_file).exists()) { | |
Properties properties = new Properties() | |
properties.load(project.rootProject.file(prop_file).newDataInputStream()) | |
properties.propertyNames().each { name -> | |
data[name] = properties.getProperty(name) | |
} | |
} | |
return new Environment(data:data) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment