Skip to content

Instantly share code, notes, and snippets.

@igor-brishkoski
Last active May 4, 2017 00:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igor-brishkoski/2d97afa867351a83e2a868cfea7d0898 to your computer and use it in GitHub Desktop.
Save igor-brishkoski/2d97afa867351a83e2a868cfea7d0898 to your computer and use it in GitHub Desktop.
Extract the keys from build.gradle
//build.gradle
android{
...
productFlavors{
staging{
//use in java code
buildConfigField "String", "SERVICE_THAT_I_NEED", getProps("service_secret_key")
buildConfigField "String", "MY_BACKEND_URL", getProps("my_url")
//use in xml, like AndroidManifest.xml
resValue "string", "google_maps_key", getProps("google_maps_key")
}
production{
//use in java code
buildConfigField "String", "SERVICE_THAT_I_NEED", getProps("service_secret_key")
buildConfigField "String", "MY_BACKEND_URL", getProps("my_url")
//use in xml, like AndroidManifest.xml
resValue "string", "google_maps_key", getProps("google_maps_key")
}
}
}
//We're going to use this method to read the secrets that
//we stored in out secrets.properties file
def getProps(String propName) {
def propsFile = rootProject.file('secrets.properties')
if (propsFile.exists()) {
def props = new Properties()
props.load(new FileInputStream(propsFile))
return props[propName]
} else {
return "";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment