Gradle get LocalProperties
def getLocalProperties() {
Properties localProperties = new Properties()
File file = project.rootProject.file('local.properties')
if (file.exists()) {
//localProperties.load(file.newDataInputStream()) //
localProperties.load(file.newReader("UTF-8")) // gradle properties
}
return localProperties
}
// get Local Properties
Properties localProperties = getLocalProperties()
println localProperties.get("key1")
println localProperties.get("key2")
gradle properties中国語の文字化けしを解決する
改善された方法:
private Properties localProperties
private Properties readPropertiesIfExist(File propertiesFile) {
Properties result = new Properties()
if (propertiesFile.exists()) {
propertiesFile.withReader('UTF-8') { reader -> result.load(reader) }
}
return result
}
private String resolveProperty(Project project, String name, String defaultValue) {
if (localProperties == null) {
localProperties = readPropertiesIfExist(new File(project.projectDir.parentFile, "local.properties"))
}
String result
if (project.hasProperty(name)) {
result = project.property(name)
}
if (result == null) {
result = localProperties.getProperty(name)
}
if (result == null) {
result = defaultValue
}
return result
}