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

14,924

Solution 1

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.

Solution 2

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

Share:
14,924
Desmond Yao
Author by

Desmond Yao

Android Engineer

Updated on July 23, 2022

Comments

  • Desmond Yao
    Desmond Yao almost 2 years

    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?

  • Desmond Yao
    Desmond Yao almost 9 years
    Thank you:) I'll be care of it!
  • Prasad
    Prasad over 6 years
    but my Config file need to store access key id and secret key. Where Config.Java have only string and integers to fetech the values through the app, but access key id and secretkey have use in Config file to create Authenticate Object. I have warning from Google Play Developer Console as it open to view, it may misuse.
  • Demetrio Guilardi
    Demetrio Guilardi over 5 years
    WRONG! Private keys, passwords and personal data MUST be in gradle.properties
  • Rakesh N
    Rakesh N over 4 years
    If user/pass is present in resources, it needs to be shipped to customers. Ideally, they should be in gradle.properties only which is not shipped. If someone wants to use it, they would need to add the required user/pass in their own gradle.properties. AFAIK, that is the right approach
  • Abandoned Cart
    Abandoned Cart almost 3 years
    Wouldn't the gradle.properties file cease to exist in a compiled application?