Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get properties defined in "gradle.properties" in JAVA STATEMENT?

I defined a property in gradle.properties file like below:

user.password=mypassword

Can I use it as a variable value in my java statement?

like image 511
Desmond Yao Avatar asked Jul 29 '15 03:07

Desmond Yao


2 Answers

Yes you can, however this is not a good idea nor a good practice. gradle.properties file is meant to keep gradle's properties itself, e.g. JVM args used at build time.

If you need to store user/pass pair in properties file, it should be placed under src/main/resources or other appropriate folder and separate from gradle.properties.

Side note: Not sure if keeping properties file in mobile application is safe in general.

like image 153
Opal Avatar answered Nov 17 '22 06:11

Opal


You would have to read the properties file and extract the property first.

Properties prop = new Properties();
InputStream input = null;

try {

    input = new FileInputStream("gradle.properties");

    // load a properties file
    prop.load(input);

    // get the property value and print it out
    System.out.println(prop.getProperty("user.password"));
} catch (IOException ex) {
    ex.printStackTrace();
}

You can find the detailed tutorial here

like image 10
Padawan Avatar answered Nov 17 '22 05:11

Padawan