Skip to content

Instantly share code, notes, and snippets.

@YuanLiou
Created January 11, 2021 06:39
Show Gist options
  • Save YuanLiou/b851eb85018ef01e103ffaffb090fc35 to your computer and use it in GitHub Desktop.
Save YuanLiou/b851eb85018ef01e103ffaffb090fc35 to your computer and use it in GitHub Desktop.
how to apply local.properties to project
fun Project.getLocalOrDefaultProperties(): Map<String, Any?> {
return getEnvOrDefaultProperties("local.properties")
}
private fun Project.getEnvOrDefaultProperties(path: String): Map<String, Any?> {
return rootProject.file(path)
.takeIf { it.canRead() }
?.let { readProperties(it).toMap() as Map<String, Any?> }
?: properties.toMap()
}
private fun Project.readProperties(file: File): Properties {
return Properties().apply {
load(FileInputStream(file))
}
}
// use it via gradle.kts
val envProps = getLocalOrDefaultProperties()
val properties01: String by envProps
val properties02: String by envProps
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment