Skip to content

Instantly share code, notes, and snippets.

@Flying--Dutchman
Last active January 21, 2023 13:49
Show Gist options
  • Save Flying--Dutchman/bda88268ef890c50ed6432bff189c82c to your computer and use it in GitHub Desktop.
Save Flying--Dutchman/bda88268ef890c50ed6432bff189c82c to your computer and use it in GitHub Desktop.
Easily read a property from gradle.properties and local.properties
// From my Codeberg page: https://codeberg.org/FlyingDutchman/GradleReadProperty/src/branch/main/getSetting
// Simply copy following code in your gradle.build.kts file
// Also use the following imports: java.io.File and import java.util.*
fun getSetting(key: String) = project.getSetting(key)
fun Project.getSetting(key: String): String {
// Check if local properties
val localProperty = rootProject.file("local.properties")
if (localProperty.isFile) {
val props = Properties().apply { load(localProperty.inputStream())}
if (props.containsKey(key))
return props[key].toString()
}
// Not in local, check gradle properties
if (!project.properties.containsKey(key))
throw Exception("Key $key not found in gradle.properties")
return project.properties[key].toString()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment