简体   繁体   English

我可以在JAVA STATEMENT中获取“gradle.properties”中定义的属性吗?

[英]Can I get properties defined in “gradle.properties” in JAVA STATEMENT?

I defined a property in gradle.properties file like below: 我在gradle.properties文件中定义了一个属性,如下所示:

user.password=mypassword

Can I use it as a variable value in my java statement? 我可以在java语句中将它用作变量值吗?

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 你可以在这里找到详细的教程

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, eg JVM args used at build time. gradle.properties文件本身就是保留gradle的属性,例如在构建时使用的JVM args。

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 . 如果需要在属性文件中存储用户/传递对,则应将其放在src/main/resources或其他相应的文件夹下,并与gradle.properties分开。

Side note: Not sure if keeping properties file in mobile application is safe in general. 附注:不确定在移动应用程序中保留属性文件是否通常是安全的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM