I found a way to add API credentials / keys to gradle.properties through Circle CI. It allows Android projects to reference gradle.properties for credentials in the same way for local and CircleCI assemblies.
In the first step, save your credentials in the Circle CI project settings as environment variables that are guaranteed to be private. In the Circle CI GUI, navigate to your project, then select “Project Settings” in the upper right corner. In the menu on the left, click "Environment Variables", which are under the heading "Tweaks". Here you can add your credentials as a pair of name values.
Then create a bash script in an Android project that will write Circle CI environment variables to the local gradle.properties file. I wrote such a script and posted it here as a gist . Here is the way that does the job:
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 Circle CI builds, not during local builds. Call this script as a pre-process dependency in the circle.yml file, so your gradle.properties will be written before the gradle build starts:
dependencies: pre: - source environmentSetup.sh && copyEnvVarsToGradleProperties
You will continue to access the API keys in the build.gradle file as always:
buildConfigField("String", "THIS_TEST_API_KEY", "\"" + TEST_API_KEY + "\"")
Kio krofovitch
source share