Read Amazon S3 credentials from ~/.gradle/ file?

One of my projects is an Android library which when built, pushes artifacts to my S3 account, and my app projects that depend on it download it from that S3 location. This setup works great on my local machine, of course. Because I have my ~/.gradle/ file set with my AWS credentials.

Is there any way to add my AWS credentials to ~/.gradle/ file? CircleCI reads from ~/.aws/credentials which is the way AWS works but Gradle does not look in there.

Gradle team is allegedly not going to merge a PR that indeed reads the credentials from ~/.aws/credentials file
Ref -

I think if CircleCI let us add properties in ~/.gradle/ that will solve this problem.

1 Like

I have found a way to add credentials / API Keys to via Circle CI. It allows Android projects to reference 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 file. I have written such a script and posted it here as gist.

This script should only be called during a Circle CI build. Invoke this script as a pre-process dependency in your circle.yml file, so that your is written before the actual gradle build begins:
dependencies: pre: - source && 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 + "\"")

1 Like

Thanks @KioKrofovitch I guess it took me almost a year to get back to solving this issue again :slight_smile:

I am not very used to bash scripting but I keep getting error that the file doesn’t exist. Pasting the console log here

source && copyEnvVarsToGradleProperties
Gradle Properties should exist at /home/ubuntu/.gradle/
Gradle Properties does not exist
Creating Gradle Properties file...
touch: cannot touch ‘/home/ubuntu/.gradle/’: No such file or directory
Writing AWS_CREDENTIALS_ACCESS_KEY_ID to line 37: /home/ubuntu/.gradle/ No such file or directory
Writing AWS_CREDENTIALS_SECRET_ACCESS_KEY to line 40: /home/ubuntu/.gradle/ No such file or directory

source && copyEnvVarsToGradleProperties returned exit code 1

Action failed: source && copyEnvVarsToGradleProperties

Any idea why it is not creating a gradle properties file?