Issue #109

People advise against storing keys inside build.gradles. We should store them on 1Password and populate our gradle.properties, so don’t track this file in git. Here is .gitignore file

*.iml

/build
/gradle.properties
/local.properties

.gradle
.idea

There are several ways to help BuddyBuild know about our gradle.properties

1. Using Environment variables

But when configuring the project on BuddyBuild, it complains about key not found. The solution is to use Environment variables

key

Then in your build.gradle, you can

buildConfigField 'String', 'MY_KEY', System.getenv("MY_KEY") ?: MY_KEY

This is because gradle does not know about environment variables. The System.getenv("MY_KEY") is for BuddyBuild, and the default MY_KEY is for gradle.properties.

Next is to remove this duplication. We can use Groovy Binding. build.gradle does the import import groovy.lang.Binding automatically for us

String environmentKey(variable) {
    for (Object var : binding.variables) {
        if (var.value == variable) {
            return System.getenv(var.key) ?: variable
        }
    }
    return ""
}
buildConfigField 'String', 'MY_KEY', environmentKey(MY_KEY)

2. Using Secured File 👍

BuddyBuild allows us to define Secured File, here we can upload our gradle.properties

secure files

And we can use Prebuild script to copy this secured file to our project. BuddyBuild suggests using buddybuild_prebuild.sh but then build fails in Build file '/tmp/sandbox/workspace/app/build.gradle'

So, create a script called buddybuild_postclone.sh

#!/usr/bin/env bash

cp $BUDDYBUILD_SECURE_FILES/gradle.properties $BUDDYBUILD_WORKSPACE/gradle.properties