Skip to content

Instantly share code, notes, and snippets.

@mirland
Last active June 5, 2020 08:45
Show Gist options
  • Save mirland/6277791f7356df04d73e42056195fd63 to your computer and use it in GitHub Desktop.
Save mirland/6277791f7356df04d73e42056195fd63 to your computer and use it in GitHub Desktop.
Script to manage android secret keys
apply from: rootProject.file('read_secrets.gradle')
apply plugin: 'com.android.application'
....
android {
defaultConfig {
buildConfigField 'String', 'SOMETHING_SECRET', getEnvVariable('SOMETHING_SECRET')
}
signingConfigs {
prod_release {
storeFile file(getEnvVariable('KEYSTORE_FILE_PATH'))
storePassword getEnvVariable('KEYSTORE_STORE_PASSWORD')
keyAlias getEnvVariable('KEYSTORE_ALIAS')
keyPassword getEnvVariable('KEYSTORE_ALIAS_PASSWORD')
}
}
....
buildTypes {
release {
buildConfigField 'String', 'LIBRARY_RELEASE_SECRET', getEnvVariable('LIBRARY_RELEASE_SECRET')
....
}
}
productFlavors {
//noinspection GroovyMissingReturnStatement
dev {
buildConfigField 'String', 'LIBRARY_DEV_API_KEY', getEnvVariable('LIBRARY_DEV_API_KEY')
....
}
prod {
buildConfigField 'String', 'LIBRARY_PROD_API_KEY', getEnvVariable('LIBRARY_PROD_API_KEY')
signingConfig signingConfigs.prod_release
....
}
}
}
# Keysore setup
KEYSTORE_FILE_PATH=something.jks
KEYSTORE_STORE_PASSWORD=something
KEYSTORE_ALIAS=something
KEYSTORE_ALIAS_PASSWORD=something
# Secret api keys
SECRET_API_KEY="something"
final def PROPERTY_FILE = file(rootProject.file('app/keys.properties'))
final def PROPERTIES = new Properties()
if (PROPERTY_FILE.exists()) {
PROPERTY_FILE.withInputStream { PROPERTIES.load(it) }
}
ext.getEnvVariable = { key, defaultValue = null ->
def value = PROPERTIES[key]
if (value != null) {
return value
}
value = project.hasProperty(key) ? project.getProperty(key) : System.getenv(key)
value = value?.trim() ? value : null
if (value == null && defaultValue == null) {
logger.warn("Variable '$key' is not defined.")
}
return value ?: defaultValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment