I have found a way to add credentials / API Keys to gradle.properties
via Circle CI. It allows Android projects to reference gradle.properties
for credentials in the same way for local and CircleCI builds.
First step, store your credentials in your Circle CI project settings as environment variables, which are guaranteed to be private. In the Circle CI GUI, go to to your project, then select "Project Settings" in the upper right hand corner. In the menu on the left side click "Environment variables" which is under the "Tweaks" header. Here you can add your credentials as a name value pair.
Next, create a bash script in your Android project which will write your Circle CI environment variables to a local gradle.properties
file. I have written such a script and posted it here as gist. Here is the method which does the work:
function copyEnvVarsToGradleProperties {
GRADLE_PROPERTIES=$HOME"/.gradle/gradle.properties"
export GRADLE_PROPERTIES
echo "Gradle Properties should exist at $GRADLE_PROPERTIES"
if [ ! -f "$GRADLE_PROPERTIES" ]; then
echo "Gradle Properties does not exist"
echo "Creating Gradle Properties file..."
touch $GRADLE_PROPERTIES
echo "Writing TEST_API_KEY to gradle.properties..."
echo "TEST_API_KEY=$TEST_API_KEY_ENV_VAR" >> $GRADLE_PROPERTIES
fi
}
This script is only called during a Circle CI build, and not during local builds. Invoke this script as a pre-process dependency in your circle.yml file, so that your gradle.properties
is written before the actual gradle build begins:
dependencies:
pre:
- source environmentSetup.sh && copyEnvVarsToGradleProperties
You will continue to access API keys in build.gradle just as you always have:
buildConfigField("String", "THIS_TEST_API_KEY", "\"" + TEST_API_KEY + "\"")