1

By referencing the code about .properties loading in build.gradle, I'd like to know how to ignore when the file is not existed.

Currently, below build.gradle file throws FileNotFound exception and cannot proceed compile.

def Properties properties = new Properties()
try{
    properties.load(project.rootProject.file("developer.properties").newDataInputStream())
}

android {
    defaultConfig {
        buildConfigField 'boolean', 'PrintLog', properties.getOrDefault("print.log", "false")
    }
}

What I want is to make compile whether developer.properties is existed or not. How can I achieve this?

Youngjae
  • 24,352
  • 18
  • 113
  • 198

1 Answers1

2

You can use simple try-catch block to achieve it as follows:

android {
    ....
    def developerPropertiesFile = file(getRootDir().getPath() + '\\developer.properties')
    def developerProperties = new Properties()
    try {
        developerProperties.load(new FileInputStream(developerPropertiesFile))
    } catch (FileNotFoundException e) {
        developerPropertiesFile = null;
    }
    ...
}

Before usage you can check:

if (developerPropertiesFile != null) {
     ...
 }
Sagar
  • 23,903
  • 4
  • 62
  • 62