Reputation: 15504
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
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
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