androidgitbuild-serveradobecreativesdk

How to not include a class file with API strings in git, but still get it on a build server?


I am currently working on my final project as a last year IT student, for which I am creating an Android application.

Somewhere in my app I allow my users to edit a picture right after it was taken from inside my app. Since this is not a core functionality in the app I use a library which does the job perfectly. For this I use the Adobe Creative SDK.

I followed their guidelines on how to integrate the required API keys and all, and currently everything is working as intended locally. On my automatic build server on the other hand, my builds are failing because it can't get the API keys since the class file containing them is not added in the git repository from which the server builds.

How can I provide my build server with those API keys without exposing them in git?

FYI: in my application class, this is how the keys are retrieved.

private static final String CREATIVE_SDK_CLIENT_ID      = Keys.CSDK_CLIENT_ID;
private static final String CREATIVE_SDK_CLIENT_SECRET  = Keys.CSDK_CLIENT_SECRET;
private static final String CREATIVE_SDK_REDIRECT_URI   = Keys.CSDK_REDIRECT_URI;
private static final String[] CREATIVE_SDK_SCOPES       = Keys.CSDK_SCOPES;

Thanks in advance!


Solution

  • I managed to resolve this issue making use of environment variables.

    The build server would build the applications with Jenkins so I managed to configure environment variables in the Jenkins project settings. For more info on how to do that, also where I found how to do it: http://blog.zuehlke.com/en/configure-your-android-project-on-jenkins/

    Now we set the environment variables on the build server, but of course our application still needs to be able to build from our developers' machines. To be able to do that we need to set those environment variables on the those machines as well.

    You can find a lot of guides out there for how to do that on a specific platform. This page however explains it in debt for multiple platforms: https://superuser.com/questions/284342/what-are-path-and-other-environment-variables-and-how-can-i-set-or-use-them

    Once you have set the environment variables on the required machines, you need to get them in your application. To read the environment variables when the application get's build we make use of gradle. In your app.gradle you should do the following:

    buildTypes {
    
        buildTypes.each {
            it.buildConfigField 'String', 'ENV_VAR_ALIAS', "$System.env.ENV_VAR_KEY"
        }
    
    }
    

    ENV_VAR_ALIAS is the alias we give to the environment variable to access it later on in our code, for example an Application class or Activity class.

    ENV_VAR_KEY is the name of the environment variable we want to get. If you configured the environment variable like this for example:
    Key: MY_SECRET_KEY and Value: MY_SECRET_VALUE
    ENV_VAR_KEY should be MY_SECRET_KEY

    Once that is done we can now access the value of the environment variable in code using BuildConfig like so:

    private final static String MY_ENV_VAR_VALUE = BuildConfig.ENV_VAR_ALIAS;