0

Where can I put my credentials in gradle without making them public? I would like to know some best pratics, and to understand well the difference between inserting them in gradle.properties or as environment variable. It's important to me know how call them in build script, too.

Thanks in advance

1 Answers1

0

You can put credentials in local.properties if you want to have it locally. You can retrieve them like this:

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def local = properties.getProperty('localVar')

and having local variable in local.properties:

localVar=helloWorld

The best practice would be putting it in an environment variable, in this way, you are able to run your gradle scripts anywhere even in a CI environment.

In build.gradle you can get environment variables by using

System.getenv("ENVIRONMENT_VARIABLE")
gumil
  • 126
  • 4
  • I will go for enviroment variables, because other ways aren't clear. Infact if i put gradle.properties.kts in users/me/.gradle i can't call the variable in build, i don't know where is the mistake – Mariano Caldara Oct 07 '19 at 20:36
  • 2
    Many people uses the Kotlin DSL. But the properties file is still called `gradle.properties` here, just as in Groovy - i.e. not with a `.kts` extension. The reason is that it is not a Kotlin file (but actually a Java properties file). – Bjørn Vester Oct 08 '19 at 15:36