radistao
radistao

Reputation: 15504

Gradle plugin to read secret (api-key, password) properties (like buildConfigField from Android plugin)

Is there any gradle function/plugin to read/bind some secret properties (api keys, passwords) from special config/properties files?

For instance, in android plugin you can set properties in ~/.gradle/gradle.properties file:

apiKey=SUPER_SECRET_VALUE

than in project build.gradle:

android {
    buildTypes {
        debug {
            buildConfigField "String", "API_KEY", apiKey
        }
    }    
}

And in project itself you use BuildConfig.API_KEY string constant.

Is there any similar solutions in gradle for other (java) projects?

Or may be this approach is wrong and some other workaround should be used?

Upvotes: 3

Views: 2159

Answers (2)

John Zeringue
John Zeringue

Reputation: 725

As an alternative to my prior answer, you can write a custom Gradle task that uses CodeModel. Here's an example.

Upvotes: 0

John Zeringue
John Zeringue

Reputation: 725

You can always filter files. You pass resources or source files through a copy command and replace tokens like @API_KEY@ (with filter and ReplaceTokens) or ${API_KEY} (using expand) with Gradle variables.

For example:

processResources {
    filter(ReplaceTokens, tokens: [API_KEY: apiKey])
}

Would replace the substring @API_KEY@ with your actual API key in any of your resource files.

Upvotes: 1

Related Questions